]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_hir_analysis/src/errors.rs
Auto merge of #105416 - nnethercote:more-linting-tweaks, r=cjgillot
[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_drop_impl_on_wrong_item, code = "E0120")]
56 pub struct DropImplOnWrongItem {
57     #[primary_span]
58     #[label]
59     pub span: Span,
60 }
61
62 #[derive(Diagnostic)]
63 #[diag(hir_analysis_field_already_declared, code = "E0124")]
64 pub struct FieldAlreadyDeclared {
65     pub field_name: Ident,
66     #[primary_span]
67     #[label]
68     pub span: Span,
69     #[label(previous_decl_label)]
70     pub prev_span: Span,
71 }
72
73 #[derive(Diagnostic)]
74 #[diag(hir_analysis_copy_impl_on_type_with_dtor, code = "E0184")]
75 pub struct CopyImplOnTypeWithDtor {
76     #[primary_span]
77     #[label]
78     pub span: Span,
79 }
80
81 #[derive(Diagnostic)]
82 #[diag(hir_analysis_multiple_relaxed_default_bounds, code = "E0203")]
83 pub struct MultipleRelaxedDefaultBounds {
84     #[primary_span]
85     pub span: Span,
86 }
87
88 #[derive(Diagnostic)]
89 #[diag(hir_analysis_copy_impl_on_non_adt, code = "E0206")]
90 pub struct CopyImplOnNonAdt {
91     #[primary_span]
92     #[label]
93     pub span: Span,
94 }
95
96 #[derive(Diagnostic)]
97 #[diag(hir_analysis_trait_object_declared_with_no_traits, code = "E0224")]
98 pub struct TraitObjectDeclaredWithNoTraits {
99     #[primary_span]
100     pub span: Span,
101     #[label(alias_span)]
102     pub trait_alias_span: Option<Span>,
103 }
104
105 #[derive(Diagnostic)]
106 #[diag(hir_analysis_ambiguous_lifetime_bound, code = "E0227")]
107 pub struct AmbiguousLifetimeBound {
108     #[primary_span]
109     pub span: Span,
110 }
111
112 #[derive(Diagnostic)]
113 #[diag(hir_analysis_assoc_type_binding_not_allowed, code = "E0229")]
114 pub struct AssocTypeBindingNotAllowed {
115     #[primary_span]
116     #[label]
117     pub span: Span,
118 }
119
120 #[derive(Diagnostic)]
121 #[diag(hir_analysis_typeof_reserved_keyword_used, code = "E0516")]
122 pub struct TypeofReservedKeywordUsed<'tcx> {
123     pub ty: Ty<'tcx>,
124     #[primary_span]
125     #[label]
126     pub span: Span,
127     #[suggestion(style = "verbose", code = "{ty}")]
128     pub opt_sugg: Option<(Span, Applicability)>,
129 }
130
131 #[derive(Diagnostic)]
132 #[diag(hir_analysis_value_of_associated_struct_already_specified, code = "E0719")]
133 pub struct ValueOfAssociatedStructAlreadySpecified {
134     #[primary_span]
135     #[label]
136     pub span: Span,
137     #[label(previous_bound_label)]
138     pub prev_span: Span,
139     pub item_name: Ident,
140     pub def_path: String,
141 }
142
143 #[derive(Diagnostic)]
144 #[diag(hir_analysis_unconstrained_opaque_type)]
145 #[note]
146 pub struct UnconstrainedOpaqueType {
147     #[primary_span]
148     pub span: Span,
149     pub name: Symbol,
150     pub what: &'static str,
151 }
152
153 pub struct MissingTypeParams {
154     pub span: Span,
155     pub def_span: Span,
156     pub span_snippet: Option<String>,
157     pub missing_type_params: Vec<Symbol>,
158     pub empty_generic_args: bool,
159 }
160
161 // Manual implementation of `IntoDiagnostic` to be able to call `span_to_snippet`.
162 impl<'a> IntoDiagnostic<'a> for MissingTypeParams {
163     #[track_caller]
164     fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
165         let mut err = handler.struct_span_err_with_code(
166             self.span,
167             rustc_errors::fluent::hir_analysis_missing_type_params,
168             error_code!(E0393),
169         );
170         err.set_arg("parameterCount", self.missing_type_params.len());
171         err.set_arg(
172             "parameters",
173             self.missing_type_params
174                 .iter()
175                 .map(|n| format!("`{}`", n))
176                 .collect::<Vec<_>>()
177                 .join(", "),
178         );
179
180         err.span_label(self.def_span, rustc_errors::fluent::label);
181
182         let mut suggested = false;
183         // Don't suggest setting the type params if there are some already: the order is
184         // tricky to get right and the user will already know what the syntax is.
185         if let Some(snippet) = self.span_snippet && self.empty_generic_args {
186             if snippet.ends_with('>') {
187                 // The user wrote `Trait<'a, T>` or similar. To provide an accurate suggestion
188                 // we would have to preserve the right order. For now, as clearly the user is
189                 // aware of the syntax, we do nothing.
190             } else {
191                 // The user wrote `Iterator`, so we don't have a type we can suggest, but at
192                 // least we can clue them to the correct syntax `Iterator<Type>`.
193                 err.span_suggestion(
194                     self.span,
195                     rustc_errors::fluent::suggestion,
196                     format!(
197                         "{}<{}>",
198                         snippet,
199                         self.missing_type_params
200                             .iter()
201                             .map(|n| n.to_string())
202                             .collect::<Vec<_>>()
203                             .join(", ")
204                     ),
205                     Applicability::HasPlaceholders,
206                 );
207                 suggested = true;
208             }
209         }
210         if !suggested {
211             err.span_label(self.span, rustc_errors::fluent::no_suggestion_label);
212         }
213
214         err.note(rustc_errors::fluent::note);
215         err
216     }
217 }
218
219 #[derive(Diagnostic)]
220 #[diag(hir_analysis_manual_implementation, code = "E0183")]
221 #[help]
222 pub struct ManualImplementation {
223     #[primary_span]
224     #[label]
225     pub span: Span,
226     pub trait_name: String,
227 }
228
229 #[derive(Diagnostic)]
230 #[diag(hir_analysis_substs_on_overridden_impl)]
231 pub struct SubstsOnOverriddenImpl {
232     #[primary_span]
233     pub span: Span,
234 }
235
236 #[derive(LintDiagnostic)]
237 #[diag(hir_analysis_unused_extern_crate)]
238 pub struct UnusedExternCrate {
239     #[suggestion(applicability = "machine-applicable", code = "")]
240     pub span: Span,
241 }
242
243 #[derive(LintDiagnostic)]
244 #[diag(hir_analysis_extern_crate_not_idiomatic)]
245 pub struct ExternCrateNotIdiomatic {
246     #[suggestion(
247         style = "short",
248         applicability = "machine-applicable",
249         code = "{suggestion_code}"
250     )]
251     pub span: Span,
252     pub msg_code: String,
253     pub suggestion_code: String,
254 }
255
256 #[derive(Diagnostic)]
257 #[diag(hir_analysis_expected_used_symbol)]
258 pub struct ExpectedUsedSymbol {
259     #[primary_span]
260     pub span: Span,
261 }
262
263 #[derive(Diagnostic)]
264 #[diag(hir_analysis_const_impl_for_non_const_trait)]
265 pub struct ConstImplForNonConstTrait {
266     #[primary_span]
267     pub trait_ref_span: Span,
268     pub trait_name: String,
269     #[suggestion(applicability = "machine-applicable", code = "#[const_trait]")]
270     pub local_trait_span: Option<Span>,
271     #[note]
272     pub marking: (),
273     #[note(adding)]
274     pub adding: (),
275 }
276
277 #[derive(Diagnostic)]
278 #[diag(hir_analysis_const_bound_for_non_const_trait)]
279 pub struct ConstBoundForNonConstTrait {
280     #[primary_span]
281     pub span: Span,
282 }
283
284 #[derive(Diagnostic)]
285 #[diag(hir_analysis_self_in_impl_self)]
286 pub struct SelfInImplSelf {
287     #[primary_span]
288     pub span: MultiSpan,
289     #[note]
290     pub note: (),
291 }
292
293 #[derive(Diagnostic)]
294 #[diag(hir_analysis_linkage_type, code = "E0791")]
295 pub(crate) struct LinkageType {
296     #[primary_span]
297     pub span: Span,
298 }