]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_typeck/src/errors.rs
Rollup merge of #97499 - est31:master, r=Dylan-DPC
[rust.git] / compiler / rustc_typeck / src / errors.rs
1 //! Errors emitted by typeck.
2 use rustc_errors::{error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed};
3 use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
4 use rustc_middle::ty::Ty;
5 use rustc_session::{parse::ParseSess, SessionDiagnostic};
6 use rustc_span::{symbol::Ident, Span, Symbol};
7
8 #[derive(SessionDiagnostic)]
9 #[error(code = "E0062", slug = "typeck-field-multiply-specified-in-initializer")]
10 pub struct FieldMultiplySpecifiedInInitializer {
11     #[primary_span]
12     #[label]
13     pub span: Span,
14     #[label = "previous-use-label"]
15     pub prev_span: Span,
16     pub ident: Ident,
17 }
18
19 #[derive(SessionDiagnostic)]
20 #[error(code = "E0092", slug = "typeck-unrecognized-atomic-operation")]
21 pub struct UnrecognizedAtomicOperation<'a> {
22     #[primary_span]
23     #[label]
24     pub span: Span,
25     pub op: &'a str,
26 }
27
28 #[derive(SessionDiagnostic)]
29 #[error(code = "E0094", slug = "typeck-wrong-number-of-generic-arguments-to-intrinsic")]
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(SessionDiagnostic)]
40 #[error(code = "E0093", slug = "typeck-unrecognized-intrinsic-function")]
41 pub struct UnrecognizedIntrinsicFunction {
42     #[primary_span]
43     #[label]
44     pub span: Span,
45     pub name: Symbol,
46 }
47
48 #[derive(SessionDiagnostic)]
49 #[error(code = "E0195", slug = "typeck-lifetimes-or-bounds-mismatch-on-trait")]
50 pub struct LifetimesOrBoundsMismatchOnTrait {
51     #[primary_span]
52     #[label]
53     pub span: Span,
54     #[label = "generics-label"]
55     pub generics_span: Option<Span>,
56     pub item_kind: &'static str,
57     pub ident: Ident,
58 }
59
60 #[derive(SessionDiagnostic)]
61 #[error(code = "E0120", slug = "typeck-drop-impl-on-wrong-item")]
62 pub struct DropImplOnWrongItem {
63     #[primary_span]
64     #[label]
65     pub span: Span,
66 }
67
68 #[derive(SessionDiagnostic)]
69 #[error(code = "E0124", slug = "typeck-field-already-declared")]
70 pub struct FieldAlreadyDeclared {
71     pub field_name: Ident,
72     #[primary_span]
73     #[label]
74     pub span: Span,
75     #[label = "previous-decl-label"]
76     pub prev_span: Span,
77 }
78
79 #[derive(SessionDiagnostic)]
80 #[error(code = "E0184", slug = "typeck-copy-impl-on-type-with-dtor")]
81 pub struct CopyImplOnTypeWithDtor {
82     #[primary_span]
83     #[label]
84     pub span: Span,
85 }
86
87 #[derive(SessionDiagnostic)]
88 #[error(code = "E0203", slug = "typeck-multiple-relaxed-default-bounds")]
89 pub struct MultipleRelaxedDefaultBounds {
90     #[primary_span]
91     pub span: Span,
92 }
93
94 #[derive(SessionDiagnostic)]
95 #[error(code = "E0206", slug = "typeck-copy-impl-on-non-adt")]
96 pub struct CopyImplOnNonAdt {
97     #[primary_span]
98     #[label]
99     pub span: Span,
100 }
101
102 #[derive(SessionDiagnostic)]
103 #[error(code = "E0224", slug = "typeck-trait-object-declared-with-no-traits")]
104 pub struct TraitObjectDeclaredWithNoTraits {
105     #[primary_span]
106     pub span: Span,
107     #[label = "alias-span"]
108     pub trait_alias_span: Option<Span>,
109 }
110
111 #[derive(SessionDiagnostic)]
112 #[error(code = "E0227", slug = "typeck-ambiguous-lifetime-bound")]
113 pub struct AmbiguousLifetimeBound {
114     #[primary_span]
115     pub span: Span,
116 }
117
118 #[derive(SessionDiagnostic)]
119 #[error(code = "E0229", slug = "typeck-assoc-type-binding-not-allowed")]
120 pub struct AssocTypeBindingNotAllowed {
121     #[primary_span]
122     #[label]
123     pub span: Span,
124 }
125
126 #[derive(SessionDiagnostic)]
127 #[error(code = "E0436", slug = "typeck-functional-record-update-on-non-struct")]
128 pub struct FunctionalRecordUpdateOnNonStruct {
129     #[primary_span]
130     pub span: Span,
131 }
132
133 #[derive(SessionDiagnostic)]
134 #[error(code = "E0516", slug = "typeck-typeof-reserved-keyword-used")]
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(SessionDiagnostic)]
145 #[error(code = "E0572", slug = "typeck-return-stmt-outside-of-fn-body")]
146 pub struct ReturnStmtOutsideOfFnBody {
147     #[primary_span]
148     pub span: Span,
149     #[label = "encl-body-label"]
150     pub encl_body_span: Option<Span>,
151     #[label = "encl-fn-label"]
152     pub encl_fn_span: Option<Span>,
153 }
154
155 #[derive(SessionDiagnostic)]
156 #[error(code = "E0627", slug = "typeck-yield-expr-outside-of-generator")]
157 pub struct YieldExprOutsideOfGenerator {
158     #[primary_span]
159     pub span: Span,
160 }
161
162 #[derive(SessionDiagnostic)]
163 #[error(code = "E0639", slug = "typeck-struct-expr-non-exhaustive")]
164 pub struct StructExprNonExhaustive {
165     #[primary_span]
166     pub span: Span,
167     pub what: &'static str,
168 }
169
170 #[derive(SessionDiagnostic)]
171 #[error(code = "E0699", slug = "typeck-method-call-on-unknown-type")]
172 pub struct MethodCallOnUnknownType {
173     #[primary_span]
174     pub span: Span,
175 }
176
177 #[derive(SessionDiagnostic)]
178 #[error(code = "E0719", slug = "typeck-value-of-associated-struct-already-specified")]
179 pub struct ValueOfAssociatedStructAlreadySpecified {
180     #[primary_span]
181     #[label]
182     pub span: Span,
183     #[label = "previous-bound-label"]
184     pub prev_span: Span,
185     pub item_name: Ident,
186     pub def_path: String,
187 }
188
189 #[derive(SessionDiagnostic)]
190 #[error(code = "E0745", slug = "typeck-address-of-temporary-taken")]
191 pub struct AddressOfTemporaryTaken {
192     #[primary_span]
193     #[label]
194     pub span: Span,
195 }
196
197 #[derive(SessionSubdiagnostic)]
198 pub enum AddReturnTypeSuggestion<'tcx> {
199     #[suggestion(
200         slug = "typeck-add-return-type-add",
201         code = "-> {found} ",
202         applicability = "machine-applicable"
203     )]
204     Add {
205         #[primary_span]
206         span: Span,
207         found: Ty<'tcx>,
208     },
209     #[suggestion(
210         slug = "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(SessionSubdiagnostic)]
221 pub enum ExpectedReturnTypeLabel<'tcx> {
222     #[label(slug = "typeck-expected-default-return-type")]
223     Unit {
224         #[primary_span]
225         span: Span,
226     },
227     #[label(slug = "typeck-expected-return-type")]
228     Other {
229         #[primary_span]
230         span: Span,
231         expected: Ty<'tcx>,
232     },
233 }
234
235 #[derive(SessionDiagnostic)]
236 #[error(slug = "typeck-unconstrained-opaque-type")]
237 #[note]
238 pub struct UnconstrainedOpaqueType {
239     #[primary_span]
240     pub span: Span,
241     pub name: Symbol,
242 }
243
244 #[derive(SessionDiagnostic)]
245 #[error(code = "E0632", slug = "typeck-explicit-generic-args-with-impl-trait")]
246 #[note]
247 pub struct ExplicitGenericArgsWithImplTrait {
248     #[primary_span]
249     #[label]
250     pub spans: Vec<Span>,
251     #[help]
252     pub is_nightly_build: Option<()>,
253 }
254
255 pub struct MissingTypeParams {
256     pub span: Span,
257     pub def_span: Span,
258     pub missing_type_params: Vec<String>,
259     pub empty_generic_args: bool,
260 }
261
262 // Manual implementation of `SessionDiagnostic` to be able to call `span_to_snippet`.
263 impl<'a> SessionDiagnostic<'a> for MissingTypeParams {
264     fn into_diagnostic(self, sess: &'a ParseSess) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
265         let mut err = sess.span_diagnostic.struct_span_err_with_code(
266             self.span,
267             rustc_errors::fluent::typeck::missing_type_params,
268             error_code!(E0393),
269         );
270         err.set_arg("parameterCount", self.missing_type_params.len());
271         err.set_arg(
272             "parameters",
273             self.missing_type_params
274                 .iter()
275                 .map(|n| format!("`{}`", n))
276                 .collect::<Vec<_>>()
277                 .join(", "),
278         );
279
280         err.span_label(self.def_span, rustc_errors::fluent::typeck::missing_type_params_label);
281
282         let mut suggested = false;
283         if let (Ok(snippet), true) = (
284             sess.source_map().span_to_snippet(self.span),
285             // Don't suggest setting the type params if there are some already: the order is
286             // tricky to get right and the user will already know what the syntax is.
287             self.empty_generic_args,
288         ) {
289             if snippet.ends_with('>') {
290                 // The user wrote `Trait<'a, T>` or similar. To provide an accurate suggestion
291                 // we would have to preserve the right order. For now, as clearly the user is
292                 // aware of the syntax, we do nothing.
293             } else {
294                 // The user wrote `Iterator`, so we don't have a type we can suggest, but at
295                 // least we can clue them to the correct syntax `Iterator<Type>`.
296                 err.span_suggestion(
297                     self.span,
298                     rustc_errors::fluent::typeck::missing_type_params_suggestion,
299                     format!("{}<{}>", snippet, self.missing_type_params.join(", ")),
300                     Applicability::HasPlaceholders,
301                 );
302                 suggested = true;
303             }
304         }
305         if !suggested {
306             err.span_label(
307                 self.span,
308                 rustc_errors::fluent::typeck::missing_type_params_no_suggestion_label,
309             );
310         }
311
312         err.note(rustc_errors::fluent::typeck::missing_type_params_note);
313         err
314     }
315 }
316
317 #[derive(SessionDiagnostic)]
318 #[error(code = "E0183", slug = "typeck-manual-implementation")]
319 #[help]
320 pub struct ManualImplementation {
321     #[primary_span]
322     #[label]
323     pub span: Span,
324     pub trait_name: String,
325 }
326
327 #[derive(SessionDiagnostic)]
328 #[error(slug = "typeck-substs-on-overridden-impl")]
329 pub struct SubstsOnOverriddenImpl {
330     #[primary_span]
331     pub span: Span,
332 }