]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_hir_analysis/src/errors.rs
rustc_hir_typeck: fix paths and partially mv files
[rust.git] / compiler / rustc_hir_analysis / src / errors.rs
1 //! Errors emitted by `rustc_hir_analysis`.
2
3 use rustc_errors::IntoDiagnostic;
4 use rustc_errors::{error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed, Handler};
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(hir_analysis::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(hir_analysis::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(hir_analysis::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(hir_analysis::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 }
147
148 pub struct MissingTypeParams {
149     pub span: Span,
150     pub def_span: Span,
151     pub span_snippet: Option<String>,
152     pub missing_type_params: Vec<Symbol>,
153     pub empty_generic_args: bool,
154 }
155
156 // Manual implementation of `IntoDiagnostic` to be able to call `span_to_snippet`.
157 impl<'a> IntoDiagnostic<'a> for MissingTypeParams {
158     fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
159         let mut err = handler.struct_span_err_with_code(
160             self.span,
161             rustc_errors::fluent::hir_analysis::missing_type_params,
162             error_code!(E0393),
163         );
164         err.set_arg("parameterCount", self.missing_type_params.len());
165         err.set_arg(
166             "parameters",
167             self.missing_type_params
168                 .iter()
169                 .map(|n| format!("`{}`", n))
170                 .collect::<Vec<_>>()
171                 .join(", "),
172         );
173
174         err.span_label(self.def_span, rustc_errors::fluent::hir_analysis::label);
175
176         let mut suggested = false;
177         // Don't suggest setting the type params if there are some already: the order is
178         // tricky to get right and the user will already know what the syntax is.
179         if let Some(snippet) = self.span_snippet && self.empty_generic_args {
180             if snippet.ends_with('>') {
181                 // The user wrote `Trait<'a, T>` or similar. To provide an accurate suggestion
182                 // we would have to preserve the right order. For now, as clearly the user is
183                 // aware of the syntax, we do nothing.
184             } else {
185                 // The user wrote `Iterator`, so we don't have a type we can suggest, but at
186                 // least we can clue them to the correct syntax `Iterator<Type>`.
187                 err.span_suggestion(
188                     self.span,
189                     rustc_errors::fluent::hir_analysis::suggestion,
190                     format!(
191                         "{}<{}>",
192                         snippet,
193                         self.missing_type_params
194                             .iter()
195                             .map(|n| n.to_string())
196                             .collect::<Vec<_>>()
197                             .join(", ")
198                     ),
199                     Applicability::HasPlaceholders,
200                 );
201                 suggested = true;
202             }
203         }
204         if !suggested {
205             err.span_label(self.span, rustc_errors::fluent::hir_analysis::no_suggestion_label);
206         }
207
208         err.note(rustc_errors::fluent::hir_analysis::note);
209         err
210     }
211 }
212
213 #[derive(Diagnostic)]
214 #[diag(hir_analysis::manual_implementation, code = "E0183")]
215 #[help]
216 pub struct ManualImplementation {
217     #[primary_span]
218     #[label]
219     pub span: Span,
220     pub trait_name: String,
221 }
222
223 #[derive(Diagnostic)]
224 #[diag(hir_analysis::substs_on_overridden_impl)]
225 pub struct SubstsOnOverriddenImpl {
226     #[primary_span]
227     pub span: Span,
228 }
229
230 #[derive(LintDiagnostic)]
231 #[diag(hir_analysis::unused_extern_crate)]
232 pub struct UnusedExternCrate {
233     #[suggestion(applicability = "machine-applicable", code = "")]
234     pub span: Span,
235 }
236
237 #[derive(LintDiagnostic)]
238 #[diag(hir_analysis::extern_crate_not_idiomatic)]
239 pub struct ExternCrateNotIdiomatic {
240     #[suggestion_short(applicability = "machine-applicable", code = "{suggestion_code}")]
241     pub span: Span,
242     pub msg_code: String,
243     pub suggestion_code: String,
244 }
245
246 #[derive(Diagnostic)]
247 #[diag(hir_analysis::expected_used_symbol)]
248 pub struct ExpectedUsedSymbol {
249     #[primary_span]
250     pub span: Span,
251 }