]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_typeck/src/errors.rs
Rollup merge of #100081 - RalfJung:unused-unsafe-in-unsafe-fn, r=jackh726
[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::{LintDiagnostic, 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(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(SessionDiagnostic)]
20 #[error(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(SessionDiagnostic)]
29 #[error(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(SessionDiagnostic)]
40 #[error(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(SessionDiagnostic)]
49 #[error(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(SessionDiagnostic)]
61 #[error(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(SessionDiagnostic)]
69 #[error(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(SessionDiagnostic)]
80 #[error(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(SessionDiagnostic)]
88 #[error(typeck::multiple_relaxed_default_bounds, code = "E0203")]
89 pub struct MultipleRelaxedDefaultBounds {
90     #[primary_span]
91     pub span: Span,
92 }
93
94 #[derive(SessionDiagnostic)]
95 #[error(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(SessionDiagnostic)]
103 #[error(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(SessionDiagnostic)]
112 #[error(typeck::ambiguous_lifetime_bound, code = "E0227")]
113 pub struct AmbiguousLifetimeBound {
114     #[primary_span]
115     pub span: Span,
116 }
117
118 #[derive(SessionDiagnostic)]
119 #[error(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(SessionDiagnostic)]
127 #[error(typeck::functional_record_update_on_non_struct, code = "E0436")]
128 pub struct FunctionalRecordUpdateOnNonStruct {
129     #[primary_span]
130     pub span: Span,
131 }
132
133 #[derive(SessionDiagnostic)]
134 #[error(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(SessionDiagnostic)]
145 #[error(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(SessionDiagnostic)]
156 #[error(typeck::yield_expr_outside_of_generator, code = "E0627")]
157 pub struct YieldExprOutsideOfGenerator {
158     #[primary_span]
159     pub span: Span,
160 }
161
162 #[derive(SessionDiagnostic)]
163 #[error(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(SessionDiagnostic)]
171 #[error(typeck::method_call_on_unknown_type, code = "E0699")]
172 pub struct MethodCallOnUnknownType {
173     #[primary_span]
174     pub span: Span,
175 }
176
177 #[derive(SessionDiagnostic)]
178 #[error(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(SessionDiagnostic)]
190 #[error(typeck::address_of_temporary_taken, code = "E0745")]
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         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         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(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(SessionDiagnostic)]
236 #[error(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 missing_type_params: Vec<Symbol>,
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> {
254         let mut err = sess.span_diagnostic.struct_span_err_with_code(
255             self.span,
256             rustc_errors::fluent::typeck::missing_type_params,
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
269         err.span_label(self.def_span, rustc_errors::fluent::typeck::label);
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,
287                     rustc_errors::fluent::typeck::suggestion,
288                     format!(
289                         "{}<{}>",
290                         snippet,
291                         self.missing_type_params
292                             .iter()
293                             .map(|n| n.to_string())
294                             .collect::<Vec<_>>()
295                             .join(", ")
296                     ),
297                     Applicability::HasPlaceholders,
298                 );
299                 suggested = true;
300             }
301         }
302         if !suggested {
303             err.span_label(self.span, rustc_errors::fluent::typeck::no_suggestion_label);
304         }
305
306         err.note(rustc_errors::fluent::typeck::note);
307         err
308     }
309 }
310
311 #[derive(SessionDiagnostic)]
312 #[error(typeck::manual_implementation, code = "E0183")]
313 #[help]
314 pub struct ManualImplementation {
315     #[primary_span]
316     #[label]
317     pub span: Span,
318     pub trait_name: String,
319 }
320
321 #[derive(SessionDiagnostic)]
322 #[error(typeck::substs_on_overridden_impl)]
323 pub struct SubstsOnOverriddenImpl {
324     #[primary_span]
325     pub span: Span,
326 }
327
328 #[derive(LintDiagnostic)]
329 #[lint(typeck::unused_extern_crate)]
330 pub struct UnusedExternCrate {
331     #[suggestion(applicability = "machine-applicable", code = "")]
332     pub span: Span,
333 }
334
335 #[derive(LintDiagnostic)]
336 #[lint(typeck::extern_crate_not_idiomatic)]
337 pub struct ExternCrateNotIdiomatic {
338     #[suggestion_short(applicability = "machine-applicable", code = "{suggestion_code}")]
339     pub span: Span,
340     pub msg_code: String,
341     pub suggestion_code: String,
342 }