]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_typeck/src/errors.rs
Auto merge of #102189 - davidtwco:translation-derive-enums, r=compiler-errors
[rust.git] / compiler / rustc_typeck / src / errors.rs
1 //! Errors emitted by typeck.
2 use rustc_errors::IntoDiagnostic;
3 use rustc_errors::{error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed, Handler};
4 use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
5 use rustc_middle::ty::Ty;
6 use rustc_span::{symbol::Ident, Span, Symbol};
7
8 #[derive(Diagnostic)]
9 #[diag(typeck::field_multiply_specified_in_initializer, code = "E0062")]
10 pub struct FieldMultiplySpecifiedInInitializer {
11     #[primary_span]
12     #[label]
13     pub span: Span,
14     #[label(typeck::previous_use_label)]
15     pub prev_span: Span,
16     pub ident: Ident,
17 }
18
19 #[derive(Diagnostic)]
20 #[diag(typeck::unrecognized_atomic_operation, code = "E0092")]
21 pub struct UnrecognizedAtomicOperation<'a> {
22     #[primary_span]
23     #[label]
24     pub span: Span,
25     pub op: &'a str,
26 }
27
28 #[derive(Diagnostic)]
29 #[diag(typeck::wrong_number_of_generic_arguments_to_intrinsic, code = "E0094")]
30 pub struct WrongNumberOfGenericArgumentsToIntrinsic<'a> {
31     #[primary_span]
32     #[label]
33     pub span: Span,
34     pub found: usize,
35     pub expected: usize,
36     pub descr: &'a str,
37 }
38
39 #[derive(Diagnostic)]
40 #[diag(typeck::unrecognized_intrinsic_function, code = "E0093")]
41 pub struct UnrecognizedIntrinsicFunction {
42     #[primary_span]
43     #[label]
44     pub span: Span,
45     pub name: Symbol,
46 }
47
48 #[derive(Diagnostic)]
49 #[diag(typeck::lifetimes_or_bounds_mismatch_on_trait, code = "E0195")]
50 pub struct LifetimesOrBoundsMismatchOnTrait {
51     #[primary_span]
52     #[label]
53     pub span: Span,
54     #[label(typeck::generics_label)]
55     pub generics_span: Option<Span>,
56     pub item_kind: &'static str,
57     pub ident: Ident,
58 }
59
60 #[derive(Diagnostic)]
61 #[diag(typeck::drop_impl_on_wrong_item, code = "E0120")]
62 pub struct DropImplOnWrongItem {
63     #[primary_span]
64     #[label]
65     pub span: Span,
66 }
67
68 #[derive(Diagnostic)]
69 #[diag(typeck::field_already_declared, code = "E0124")]
70 pub struct FieldAlreadyDeclared {
71     pub field_name: Ident,
72     #[primary_span]
73     #[label]
74     pub span: Span,
75     #[label(typeck::previous_decl_label)]
76     pub prev_span: Span,
77 }
78
79 #[derive(Diagnostic)]
80 #[diag(typeck::copy_impl_on_type_with_dtor, code = "E0184")]
81 pub struct CopyImplOnTypeWithDtor {
82     #[primary_span]
83     #[label]
84     pub span: Span,
85 }
86
87 #[derive(Diagnostic)]
88 #[diag(typeck::multiple_relaxed_default_bounds, code = "E0203")]
89 pub struct MultipleRelaxedDefaultBounds {
90     #[primary_span]
91     pub span: Span,
92 }
93
94 #[derive(Diagnostic)]
95 #[diag(typeck::copy_impl_on_non_adt, code = "E0206")]
96 pub struct CopyImplOnNonAdt {
97     #[primary_span]
98     #[label]
99     pub span: Span,
100 }
101
102 #[derive(Diagnostic)]
103 #[diag(typeck::trait_object_declared_with_no_traits, code = "E0224")]
104 pub struct TraitObjectDeclaredWithNoTraits {
105     #[primary_span]
106     pub span: Span,
107     #[label(typeck::alias_span)]
108     pub trait_alias_span: Option<Span>,
109 }
110
111 #[derive(Diagnostic)]
112 #[diag(typeck::ambiguous_lifetime_bound, code = "E0227")]
113 pub struct AmbiguousLifetimeBound {
114     #[primary_span]
115     pub span: Span,
116 }
117
118 #[derive(Diagnostic)]
119 #[diag(typeck::assoc_type_binding_not_allowed, code = "E0229")]
120 pub struct AssocTypeBindingNotAllowed {
121     #[primary_span]
122     #[label]
123     pub span: Span,
124 }
125
126 #[derive(Diagnostic)]
127 #[diag(typeck::functional_record_update_on_non_struct, code = "E0436")]
128 pub struct FunctionalRecordUpdateOnNonStruct {
129     #[primary_span]
130     pub span: Span,
131 }
132
133 #[derive(Diagnostic)]
134 #[diag(typeck::typeof_reserved_keyword_used, code = "E0516")]
135 pub struct TypeofReservedKeywordUsed<'tcx> {
136     pub ty: Ty<'tcx>,
137     #[primary_span]
138     #[label]
139     pub span: Span,
140     #[suggestion_verbose(code = "{ty}")]
141     pub opt_sugg: Option<(Span, Applicability)>,
142 }
143
144 #[derive(Diagnostic)]
145 #[diag(typeck::return_stmt_outside_of_fn_body, code = "E0572")]
146 pub struct ReturnStmtOutsideOfFnBody {
147     #[primary_span]
148     pub span: Span,
149     #[label(typeck::encl_body_label)]
150     pub encl_body_span: Option<Span>,
151     #[label(typeck::encl_fn_label)]
152     pub encl_fn_span: Option<Span>,
153 }
154
155 #[derive(Diagnostic)]
156 #[diag(typeck::yield_expr_outside_of_generator, code = "E0627")]
157 pub struct YieldExprOutsideOfGenerator {
158     #[primary_span]
159     pub span: Span,
160 }
161
162 #[derive(Diagnostic)]
163 #[diag(typeck::struct_expr_non_exhaustive, code = "E0639")]
164 pub struct StructExprNonExhaustive {
165     #[primary_span]
166     pub span: Span,
167     pub what: &'static str,
168 }
169
170 #[derive(Diagnostic)]
171 #[diag(typeck::method_call_on_unknown_type, code = "E0699")]
172 pub struct MethodCallOnUnknownType {
173     #[primary_span]
174     pub span: Span,
175 }
176
177 #[derive(Diagnostic)]
178 #[diag(typeck::value_of_associated_struct_already_specified, code = "E0719")]
179 pub struct ValueOfAssociatedStructAlreadySpecified {
180     #[primary_span]
181     #[label]
182     pub span: Span,
183     #[label(typeck::previous_bound_label)]
184     pub prev_span: Span,
185     pub item_name: Ident,
186     pub def_path: String,
187 }
188
189 #[derive(Diagnostic)]
190 #[diag(typeck::address_of_temporary_taken, code = "E0745")]
191 pub struct AddressOfTemporaryTaken {
192     #[primary_span]
193     #[label]
194     pub span: Span,
195 }
196
197 #[derive(Subdiagnostic)]
198 pub enum AddReturnTypeSuggestion {
199     #[suggestion(
200         typeck::add_return_type_add,
201         code = "-> {found} ",
202         applicability = "machine-applicable"
203     )]
204     Add {
205         #[primary_span]
206         span: Span,
207         found: String,
208     },
209     #[suggestion(
210         typeck::add_return_type_missing_here,
211         code = "-> _ ",
212         applicability = "has-placeholders"
213     )]
214     MissingHere {
215         #[primary_span]
216         span: Span,
217     },
218 }
219
220 #[derive(Subdiagnostic)]
221 pub enum ExpectedReturnTypeLabel<'tcx> {
222     #[label(typeck::expected_default_return_type)]
223     Unit {
224         #[primary_span]
225         span: Span,
226     },
227     #[label(typeck::expected_return_type)]
228     Other {
229         #[primary_span]
230         span: Span,
231         expected: Ty<'tcx>,
232     },
233 }
234
235 #[derive(Diagnostic)]
236 #[diag(typeck::unconstrained_opaque_type)]
237 #[note]
238 pub struct UnconstrainedOpaqueType {
239     #[primary_span]
240     pub span: Span,
241     pub name: Symbol,
242 }
243
244 pub struct MissingTypeParams {
245     pub span: Span,
246     pub def_span: Span,
247     pub span_snippet: Option<String>,
248     pub missing_type_params: Vec<Symbol>,
249     pub empty_generic_args: bool,
250 }
251
252 // Manual implementation of `IntoDiagnostic` to be able to call `span_to_snippet`.
253 impl<'a> IntoDiagnostic<'a> for MissingTypeParams {
254     fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
255         let mut err = handler.struct_span_err_with_code(
256             self.span,
257             rustc_errors::fluent::typeck::missing_type_params,
258             error_code!(E0393),
259         );
260         err.set_arg("parameterCount", self.missing_type_params.len());
261         err.set_arg(
262             "parameters",
263             self.missing_type_params
264                 .iter()
265                 .map(|n| format!("`{}`", n))
266                 .collect::<Vec<_>>()
267                 .join(", "),
268         );
269
270         err.span_label(self.def_span, rustc_errors::fluent::typeck::label);
271
272         let mut suggested = false;
273         // Don't suggest setting the type params if there are some already: the order is
274         // tricky to get right and the user will already know what the syntax is.
275         if let Some(snippet) = self.span_snippet && self.empty_generic_args {
276             if snippet.ends_with('>') {
277                 // The user wrote `Trait<'a, T>` or similar. To provide an accurate suggestion
278                 // we would have to preserve the right order. For now, as clearly the user is
279                 // aware of the syntax, we do nothing.
280             } else {
281                 // The user wrote `Iterator`, so we don't have a type we can suggest, but at
282                 // least we can clue them to the correct syntax `Iterator<Type>`.
283                 err.span_suggestion(
284                     self.span,
285                     rustc_errors::fluent::typeck::suggestion,
286                     format!(
287                         "{}<{}>",
288                         snippet,
289                         self.missing_type_params
290                             .iter()
291                             .map(|n| n.to_string())
292                             .collect::<Vec<_>>()
293                             .join(", ")
294                     ),
295                     Applicability::HasPlaceholders,
296                 );
297                 suggested = true;
298             }
299         }
300         if !suggested {
301             err.span_label(self.span, rustc_errors::fluent::typeck::no_suggestion_label);
302         }
303
304         err.note(rustc_errors::fluent::typeck::note);
305         err
306     }
307 }
308
309 #[derive(Diagnostic)]
310 #[diag(typeck::manual_implementation, code = "E0183")]
311 #[help]
312 pub struct ManualImplementation {
313     #[primary_span]
314     #[label]
315     pub span: Span,
316     pub trait_name: String,
317 }
318
319 #[derive(Diagnostic)]
320 #[diag(typeck::substs_on_overridden_impl)]
321 pub struct SubstsOnOverriddenImpl {
322     #[primary_span]
323     pub span: Span,
324 }
325
326 #[derive(LintDiagnostic)]
327 #[diag(typeck::unused_extern_crate)]
328 pub struct UnusedExternCrate {
329     #[suggestion(applicability = "machine-applicable", code = "")]
330     pub span: Span,
331 }
332
333 #[derive(LintDiagnostic)]
334 #[diag(typeck::extern_crate_not_idiomatic)]
335 pub struct ExternCrateNotIdiomatic {
336     #[suggestion_short(applicability = "machine-applicable", code = "{suggestion_code}")]
337     pub span: Span,
338     pub msg_code: String,
339     pub suggestion_code: String,
340 }
341
342 #[derive(Diagnostic)]
343 #[diag(typeck::expected_used_symbol)]
344 pub struct ExpectedUsedSymbol {
345     #[primary_span]
346     pub span: Span,
347 }