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