]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_hir_analysis/src/errors.rs
Rollup merge of #107125 - WaffleLapkin:expect_an_item_in_your_hir_by_the_next_morning...
[rust.git] / compiler / rustc_hir_analysis / src / errors.rs
1 //! Errors emitted by `rustc_hir_analysis`.
2
3 use rustc_errors::{error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed, Handler};
4 use rustc_errors::{IntoDiagnostic, MultiSpan};
5 use rustc_macros::{Diagnostic, LintDiagnostic};
6 use rustc_middle::ty::Ty;
7 use rustc_span::{symbol::Ident, Span, Symbol};
8
9 #[derive(Diagnostic)]
10 #[diag(hir_analysis_unrecognized_atomic_operation, code = "E0092")]
11 pub struct UnrecognizedAtomicOperation<'a> {
12     #[primary_span]
13     #[label]
14     pub span: Span,
15     pub op: &'a str,
16 }
17
18 #[derive(Diagnostic)]
19 #[diag(hir_analysis_wrong_number_of_generic_arguments_to_intrinsic, code = "E0094")]
20 pub struct WrongNumberOfGenericArgumentsToIntrinsic<'a> {
21     #[primary_span]
22     #[label]
23     pub span: Span,
24     pub found: usize,
25     pub expected: usize,
26     pub descr: &'a str,
27 }
28
29 #[derive(Diagnostic)]
30 #[diag(hir_analysis_unrecognized_intrinsic_function, code = "E0093")]
31 pub struct UnrecognizedIntrinsicFunction {
32     #[primary_span]
33     #[label]
34     pub span: Span,
35     pub name: Symbol,
36 }
37
38 #[derive(Diagnostic)]
39 #[diag(hir_analysis_lifetimes_or_bounds_mismatch_on_trait, code = "E0195")]
40 pub struct LifetimesOrBoundsMismatchOnTrait {
41     #[primary_span]
42     #[label]
43     pub span: Span,
44     #[label(generics_label)]
45     pub generics_span: Option<Span>,
46     #[label(where_label)]
47     pub where_span: Option<Span>,
48     #[label(bounds_label)]
49     pub bounds_span: Vec<Span>,
50     pub item_kind: &'static str,
51     pub ident: Ident,
52 }
53
54 #[derive(Diagnostic)]
55 #[diag(hir_analysis_async_trait_impl_should_be_async)]
56 pub struct AsyncTraitImplShouldBeAsync {
57     #[primary_span]
58     // #[label]
59     pub span: Span,
60     #[label(trait_item_label)]
61     pub trait_item_span: Option<Span>,
62     pub method_name: Symbol,
63 }
64
65 #[derive(Diagnostic)]
66 #[diag(hir_analysis_drop_impl_on_wrong_item, code = "E0120")]
67 pub struct DropImplOnWrongItem {
68     #[primary_span]
69     #[label]
70     pub span: Span,
71 }
72
73 #[derive(Diagnostic)]
74 #[diag(hir_analysis_field_already_declared, code = "E0124")]
75 pub struct FieldAlreadyDeclared {
76     pub field_name: Ident,
77     #[primary_span]
78     #[label]
79     pub span: Span,
80     #[label(previous_decl_label)]
81     pub prev_span: Span,
82 }
83
84 #[derive(Diagnostic)]
85 #[diag(hir_analysis_copy_impl_on_type_with_dtor, code = "E0184")]
86 pub struct CopyImplOnTypeWithDtor {
87     #[primary_span]
88     #[label]
89     pub span: Span,
90 }
91
92 #[derive(Diagnostic)]
93 #[diag(hir_analysis_multiple_relaxed_default_bounds, code = "E0203")]
94 pub struct MultipleRelaxedDefaultBounds {
95     #[primary_span]
96     pub span: Span,
97 }
98
99 #[derive(Diagnostic)]
100 #[diag(hir_analysis_copy_impl_on_non_adt, code = "E0206")]
101 pub struct CopyImplOnNonAdt {
102     #[primary_span]
103     #[label]
104     pub span: Span,
105 }
106
107 #[derive(Diagnostic)]
108 #[diag(hir_analysis_trait_object_declared_with_no_traits, code = "E0224")]
109 pub struct TraitObjectDeclaredWithNoTraits {
110     #[primary_span]
111     pub span: Span,
112     #[label(alias_span)]
113     pub trait_alias_span: Option<Span>,
114 }
115
116 #[derive(Diagnostic)]
117 #[diag(hir_analysis_ambiguous_lifetime_bound, code = "E0227")]
118 pub struct AmbiguousLifetimeBound {
119     #[primary_span]
120     pub span: Span,
121 }
122
123 #[derive(Diagnostic)]
124 #[diag(hir_analysis_assoc_type_binding_not_allowed, code = "E0229")]
125 pub struct AssocTypeBindingNotAllowed {
126     #[primary_span]
127     #[label]
128     pub span: Span,
129 }
130
131 #[derive(Diagnostic)]
132 #[diag(hir_analysis_typeof_reserved_keyword_used, code = "E0516")]
133 pub struct TypeofReservedKeywordUsed<'tcx> {
134     pub ty: Ty<'tcx>,
135     #[primary_span]
136     #[label]
137     pub span: Span,
138     #[suggestion(style = "verbose", code = "{ty}")]
139     pub opt_sugg: Option<(Span, Applicability)>,
140 }
141
142 #[derive(Diagnostic)]
143 #[diag(hir_analysis_value_of_associated_struct_already_specified, code = "E0719")]
144 pub struct ValueOfAssociatedStructAlreadySpecified {
145     #[primary_span]
146     #[label]
147     pub span: Span,
148     #[label(previous_bound_label)]
149     pub prev_span: Span,
150     pub item_name: Ident,
151     pub def_path: String,
152 }
153
154 #[derive(Diagnostic)]
155 #[diag(hir_analysis_unconstrained_opaque_type)]
156 #[note]
157 pub struct UnconstrainedOpaqueType {
158     #[primary_span]
159     pub span: Span,
160     pub name: Symbol,
161     pub what: &'static str,
162 }
163
164 pub struct MissingTypeParams {
165     pub span: Span,
166     pub def_span: Span,
167     pub span_snippet: Option<String>,
168     pub missing_type_params: Vec<Symbol>,
169     pub empty_generic_args: bool,
170 }
171
172 // Manual implementation of `IntoDiagnostic` to be able to call `span_to_snippet`.
173 impl<'a> IntoDiagnostic<'a> for MissingTypeParams {
174     #[track_caller]
175     fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
176         let mut err = handler.struct_span_err_with_code(
177             self.span,
178             rustc_errors::fluent::hir_analysis_missing_type_params,
179             error_code!(E0393),
180         );
181         err.set_arg("parameterCount", self.missing_type_params.len());
182         err.set_arg(
183             "parameters",
184             self.missing_type_params
185                 .iter()
186                 .map(|n| format!("`{}`", n))
187                 .collect::<Vec<_>>()
188                 .join(", "),
189         );
190
191         err.span_label(self.def_span, rustc_errors::fluent::label);
192
193         let mut suggested = false;
194         // Don't suggest setting the type params if there are some already: the order is
195         // tricky to get right and the user will already know what the syntax is.
196         if let Some(snippet) = self.span_snippet && self.empty_generic_args {
197             if snippet.ends_with('>') {
198                 // The user wrote `Trait<'a, T>` or similar. To provide an accurate suggestion
199                 // we would have to preserve the right order. For now, as clearly the user is
200                 // aware of the syntax, we do nothing.
201             } else {
202                 // The user wrote `Iterator`, so we don't have a type we can suggest, but at
203                 // least we can clue them to the correct syntax `Iterator<Type>`.
204                 err.span_suggestion(
205                     self.span,
206                     rustc_errors::fluent::suggestion,
207                     format!(
208                         "{}<{}>",
209                         snippet,
210                         self.missing_type_params
211                             .iter()
212                             .map(|n| n.to_string())
213                             .collect::<Vec<_>>()
214                             .join(", ")
215                     ),
216                     Applicability::HasPlaceholders,
217                 );
218                 suggested = true;
219             }
220         }
221         if !suggested {
222             err.span_label(self.span, rustc_errors::fluent::no_suggestion_label);
223         }
224
225         err.note(rustc_errors::fluent::note);
226         err
227     }
228 }
229
230 #[derive(Diagnostic)]
231 #[diag(hir_analysis_manual_implementation, code = "E0183")]
232 #[help]
233 pub struct ManualImplementation {
234     #[primary_span]
235     #[label]
236     pub span: Span,
237     pub trait_name: String,
238 }
239
240 #[derive(Diagnostic)]
241 #[diag(hir_analysis_substs_on_overridden_impl)]
242 pub struct SubstsOnOverriddenImpl {
243     #[primary_span]
244     pub span: Span,
245 }
246
247 #[derive(LintDiagnostic)]
248 #[diag(hir_analysis_unused_extern_crate)]
249 pub struct UnusedExternCrate {
250     #[suggestion(applicability = "machine-applicable", code = "")]
251     pub span: Span,
252 }
253
254 #[derive(LintDiagnostic)]
255 #[diag(hir_analysis_extern_crate_not_idiomatic)]
256 pub struct ExternCrateNotIdiomatic {
257     #[suggestion(
258         style = "short",
259         applicability = "machine-applicable",
260         code = "{suggestion_code}"
261     )]
262     pub span: Span,
263     pub msg_code: String,
264     pub suggestion_code: String,
265 }
266
267 #[derive(Diagnostic)]
268 #[diag(hir_analysis_const_impl_for_non_const_trait)]
269 pub struct ConstImplForNonConstTrait {
270     #[primary_span]
271     pub trait_ref_span: Span,
272     pub trait_name: String,
273     #[suggestion(applicability = "machine-applicable", code = "#[const_trait]")]
274     pub local_trait_span: Option<Span>,
275     #[note]
276     pub marking: (),
277     #[note(adding)]
278     pub adding: (),
279 }
280
281 #[derive(Diagnostic)]
282 #[diag(hir_analysis_const_bound_for_non_const_trait)]
283 pub struct ConstBoundForNonConstTrait {
284     #[primary_span]
285     pub span: Span,
286 }
287
288 #[derive(Diagnostic)]
289 #[diag(hir_analysis_self_in_impl_self)]
290 pub struct SelfInImplSelf {
291     #[primary_span]
292     pub span: MultiSpan,
293     #[note]
294     pub note: (),
295 }
296
297 #[derive(Diagnostic)]
298 #[diag(hir_analysis_linkage_type, code = "E0791")]
299 pub(crate) struct LinkageType {
300     #[primary_span]
301     pub span: Span,
302 }
303
304 #[derive(Diagnostic)]
305 #[help]
306 #[diag(hir_analysis_auto_deref_reached_recursion_limit, code = "E0055")]
307 pub struct AutoDerefReachedRecursionLimit<'a> {
308     #[primary_span]
309     #[label]
310     pub span: Span,
311     pub ty: Ty<'a>,
312     pub suggested_limit: rustc_session::Limit,
313     pub crate_name: Symbol,
314 }