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