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