]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_hir_typeck/src/errors.rs
Modify existing bounds if they exist
[rust.git] / compiler / rustc_hir_typeck / src / errors.rs
1 //! Errors emitted by `rustc_hir_typeck`.
2 use rustc_errors::{AddToDiagnostic, Applicability, Diagnostic, MultiSpan, SubdiagnosticMessage};
3 use rustc_macros::{Diagnostic, Subdiagnostic};
4 use rustc_middle::ty::Ty;
5 use rustc_span::{
6     edition::{Edition, LATEST_STABLE_EDITION},
7     symbol::Ident,
8     Span,
9 };
10
11 #[derive(Diagnostic)]
12 #[diag(hir_typeck_field_multiply_specified_in_initializer, code = "E0062")]
13 pub struct FieldMultiplySpecifiedInInitializer {
14     #[primary_span]
15     #[label]
16     pub span: Span,
17     #[label(previous_use_label)]
18     pub prev_span: Span,
19     pub ident: Ident,
20 }
21
22 #[derive(Diagnostic)]
23 #[diag(hir_typeck_return_stmt_outside_of_fn_body, code = "E0572")]
24 pub struct ReturnStmtOutsideOfFnBody {
25     #[primary_span]
26     pub span: Span,
27     #[label(encl_body_label)]
28     pub encl_body_span: Option<Span>,
29     #[label(encl_fn_label)]
30     pub encl_fn_span: Option<Span>,
31 }
32
33 #[derive(Diagnostic)]
34 #[diag(hir_typeck_yield_expr_outside_of_generator, code = "E0627")]
35 pub struct YieldExprOutsideOfGenerator {
36     #[primary_span]
37     pub span: Span,
38 }
39
40 #[derive(Diagnostic)]
41 #[diag(hir_typeck_struct_expr_non_exhaustive, code = "E0639")]
42 pub struct StructExprNonExhaustive {
43     #[primary_span]
44     pub span: Span,
45     pub what: &'static str,
46 }
47
48 #[derive(Diagnostic)]
49 #[diag(hir_typeck_method_call_on_unknown_type, code = "E0699")]
50 pub struct MethodCallOnUnknownType {
51     #[primary_span]
52     pub span: Span,
53 }
54
55 #[derive(Diagnostic)]
56 #[diag(hir_typeck_functional_record_update_on_non_struct, code = "E0436")]
57 pub struct FunctionalRecordUpdateOnNonStruct {
58     #[primary_span]
59     pub span: Span,
60 }
61
62 #[derive(Diagnostic)]
63 #[diag(hir_typeck_address_of_temporary_taken, code = "E0745")]
64 pub struct AddressOfTemporaryTaken {
65     #[primary_span]
66     #[label]
67     pub span: Span,
68 }
69
70 #[derive(Subdiagnostic)]
71 pub enum AddReturnTypeSuggestion {
72     #[suggestion(
73         hir_typeck_add_return_type_add,
74         code = "-> {found} ",
75         applicability = "machine-applicable"
76     )]
77     Add {
78         #[primary_span]
79         span: Span,
80         found: String,
81     },
82     #[suggestion(
83         hir_typeck_add_return_type_missing_here,
84         code = "-> _ ",
85         applicability = "has-placeholders"
86     )]
87     MissingHere {
88         #[primary_span]
89         span: Span,
90     },
91 }
92
93 #[derive(Subdiagnostic)]
94 pub enum ExpectedReturnTypeLabel<'tcx> {
95     #[label(hir_typeck_expected_default_return_type)]
96     Unit {
97         #[primary_span]
98         span: Span,
99     },
100     #[label(hir_typeck_expected_return_type)]
101     Other {
102         #[primary_span]
103         span: Span,
104         expected: Ty<'tcx>,
105     },
106 }
107
108 #[derive(Diagnostic)]
109 #[diag(hir_typeck_missing_parentheses_in_range, code = "E0689")]
110 pub struct MissingParentheseInRange {
111     #[primary_span]
112     #[label(hir_typeck_missing_parentheses_in_range)]
113     pub span: Span,
114     pub ty_str: String,
115     pub method_name: String,
116     #[subdiagnostic]
117     pub add_missing_parentheses: Option<AddMissingParenthesesInRange>,
118 }
119
120 #[derive(Subdiagnostic)]
121 #[multipart_suggestion(
122     hir_typeck_add_missing_parentheses_in_range,
123     style = "verbose",
124     applicability = "maybe-incorrect"
125 )]
126 pub struct AddMissingParenthesesInRange {
127     pub func_name: String,
128     #[suggestion_part(code = "(")]
129     pub left: Span,
130     #[suggestion_part(code = ")")]
131     pub right: Span,
132 }
133
134 #[derive(Diagnostic)]
135 #[diag(hir_typeck_op_trait_generic_params)]
136 pub struct OpMethodGenericParams {
137     #[primary_span]
138     pub span: Span,
139     pub method_name: String,
140 }
141
142 pub struct TypeMismatchFruTypo {
143     /// Span of the LHS of the range
144     pub expr_span: Span,
145     /// Span of the `..RHS` part of the range
146     pub fru_span: Span,
147     /// Rendered expression of the RHS of the range
148     pub expr: Option<String>,
149 }
150
151 impl AddToDiagnostic for TypeMismatchFruTypo {
152     fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
153     where
154         F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
155     {
156         diag.set_arg("expr", self.expr.as_deref().unwrap_or("NONE"));
157
158         // Only explain that `a ..b` is a range if it's split up
159         if self.expr_span.between(self.fru_span).is_empty() {
160             diag.span_note(
161                 self.expr_span.to(self.fru_span),
162                 rustc_errors::fluent::hir_typeck_fru_note,
163             );
164         } else {
165             let mut multispan: MultiSpan = vec![self.expr_span, self.fru_span].into();
166             multispan.push_span_label(self.expr_span, rustc_errors::fluent::hir_typeck_fru_expr);
167             multispan.push_span_label(self.fru_span, rustc_errors::fluent::hir_typeck_fru_expr2);
168             diag.span_note(multispan, rustc_errors::fluent::hir_typeck_fru_note);
169         }
170
171         diag.span_suggestion(
172             self.expr_span.shrink_to_hi(),
173             rustc_errors::fluent::hir_typeck_fru_suggestion,
174             ", ",
175             Applicability::MaybeIncorrect,
176         );
177     }
178 }
179
180 #[derive(Diagnostic)]
181 #[diag(hir_typeck_lang_start_incorrect_number_params)]
182 #[note(hir_typeck_lang_start_incorrect_number_params_note_expected_count)]
183 #[note(hir_typeck_lang_start_expected_sig_note)]
184 pub struct LangStartIncorrectNumberArgs {
185     #[primary_span]
186     pub params_span: Span,
187     pub found_param_count: usize,
188 }
189
190 #[derive(Diagnostic)]
191 #[diag(hir_typeck_lang_start_incorrect_param)]
192 pub struct LangStartIncorrectParam<'tcx> {
193     #[primary_span]
194     #[suggestion(style = "short", code = "{expected_ty}", applicability = "machine-applicable")]
195     pub param_span: Span,
196
197     pub param_num: usize,
198     pub expected_ty: Ty<'tcx>,
199     pub found_ty: Ty<'tcx>,
200 }
201
202 #[derive(Diagnostic)]
203 #[diag(hir_typeck_lang_start_incorrect_ret_ty)]
204 pub struct LangStartIncorrectRetTy<'tcx> {
205     #[primary_span]
206     #[suggestion(style = "short", code = "{expected_ty}", applicability = "machine-applicable")]
207     pub ret_span: Span,
208
209     pub expected_ty: Ty<'tcx>,
210     pub found_ty: Ty<'tcx>,
211 }
212
213 #[derive(Subdiagnostic)]
214 pub enum HelpUseLatestEdition {
215     #[help(hir_typeck_help_set_edition_cargo)]
216     #[note(hir_typeck_note_edition_guide)]
217     Cargo { edition: Edition },
218     #[help(hir_typeck_help_set_edition_standalone)]
219     #[note(hir_typeck_note_edition_guide)]
220     Standalone { edition: Edition },
221 }
222
223 impl HelpUseLatestEdition {
224     pub fn new() -> Self {
225         let edition = LATEST_STABLE_EDITION;
226         if std::env::var_os("CARGO").is_some() {
227             Self::Cargo { edition }
228         } else {
229             Self::Standalone { edition }
230         }
231     }
232 }