1 //! Errors emitted by `rustc_hir_analysis`.
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};
10 #[diag(hir_analysis_unrecognized_atomic_operation, code = "E0092")]
11 pub struct UnrecognizedAtomicOperation<'a> {
19 #[diag(hir_analysis_wrong_number_of_generic_arguments_to_intrinsic, code = "E0094")]
20 pub struct WrongNumberOfGenericArgumentsToIntrinsic<'a> {
30 #[diag(hir_analysis_unrecognized_intrinsic_function, code = "E0093")]
31 pub struct UnrecognizedIntrinsicFunction {
39 #[diag(hir_analysis_lifetimes_or_bounds_mismatch_on_trait, code = "E0195")]
40 pub struct LifetimesOrBoundsMismatchOnTrait {
44 #[label(generics_label)]
45 pub generics_span: Option<Span>,
47 pub where_span: Option<Span>,
48 #[label(bounds_label)]
49 pub bounds_span: Vec<Span>,
50 pub item_kind: &'static str,
55 #[diag(hir_analysis_async_trait_impl_should_be_async)]
56 pub struct AsyncTraitImplShouldBeAsync {
60 #[label(trait_item_label)]
61 pub trait_item_span: Option<Span>,
62 pub method_name: Symbol,
66 #[diag(hir_analysis_drop_impl_on_wrong_item, code = "E0120")]
67 pub struct DropImplOnWrongItem {
74 #[diag(hir_analysis_field_already_declared, code = "E0124")]
75 pub struct FieldAlreadyDeclared {
76 pub field_name: Ident,
80 #[label(previous_decl_label)]
85 #[diag(hir_analysis_copy_impl_on_type_with_dtor, code = "E0184")]
86 pub struct CopyImplOnTypeWithDtor {
93 #[diag(hir_analysis_multiple_relaxed_default_bounds, code = "E0203")]
94 pub struct MultipleRelaxedDefaultBounds {
100 #[diag(hir_analysis_copy_impl_on_non_adt, code = "E0206")]
101 pub struct CopyImplOnNonAdt {
107 #[derive(Diagnostic)]
108 #[diag(hir_analysis_trait_object_declared_with_no_traits, code = "E0224")]
109 pub struct TraitObjectDeclaredWithNoTraits {
113 pub trait_alias_span: Option<Span>,
116 #[derive(Diagnostic)]
117 #[diag(hir_analysis_ambiguous_lifetime_bound, code = "E0227")]
118 pub struct AmbiguousLifetimeBound {
123 #[derive(Diagnostic)]
124 #[diag(hir_analysis_assoc_type_binding_not_allowed, code = "E0229")]
125 pub struct AssocTypeBindingNotAllowed {
131 #[derive(Diagnostic)]
132 #[diag(hir_analysis_typeof_reserved_keyword_used, code = "E0516")]
133 pub struct TypeofReservedKeywordUsed<'tcx> {
138 #[suggestion(style = "verbose", code = "{ty}")]
139 pub opt_sugg: Option<(Span, Applicability)>,
142 #[derive(Diagnostic)]
143 #[diag(hir_analysis_value_of_associated_struct_already_specified, code = "E0719")]
144 pub struct ValueOfAssociatedStructAlreadySpecified {
148 #[label(previous_bound_label)]
150 pub item_name: Ident,
151 pub def_path: String,
154 #[derive(Diagnostic)]
155 #[diag(hir_analysis_unconstrained_opaque_type)]
157 pub struct UnconstrainedOpaqueType {
161 pub what: &'static str,
164 pub struct MissingTypeParams {
167 pub span_snippet: Option<String>,
168 pub missing_type_params: Vec<Symbol>,
169 pub empty_generic_args: bool,
172 // Manual implementation of `IntoDiagnostic` to be able to call `span_to_snippet`.
173 impl<'a> IntoDiagnostic<'a> for MissingTypeParams {
175 fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
176 let mut err = handler.struct_span_err_with_code(
178 rustc_errors::fluent::hir_analysis_missing_type_params,
181 err.set_arg("parameterCount", self.missing_type_params.len());
184 self.missing_type_params
186 .map(|n| format!("`{}`", n))
191 err.span_label(self.def_span, rustc_errors::fluent::label);
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.
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>`.
206 rustc_errors::fluent::suggestion,
210 self.missing_type_params
212 .map(|n| n.to_string())
216 Applicability::HasPlaceholders,
222 err.span_label(self.span, rustc_errors::fluent::no_suggestion_label);
225 err.note(rustc_errors::fluent::note);
230 #[derive(Diagnostic)]
231 #[diag(hir_analysis_manual_implementation, code = "E0183")]
233 pub struct ManualImplementation {
237 pub trait_name: String,
240 #[derive(Diagnostic)]
241 #[diag(hir_analysis_substs_on_overridden_impl)]
242 pub struct SubstsOnOverriddenImpl {
247 #[derive(LintDiagnostic)]
248 #[diag(hir_analysis_unused_extern_crate)]
249 pub struct UnusedExternCrate {
250 #[suggestion(applicability = "machine-applicable", code = "")]
254 #[derive(LintDiagnostic)]
255 #[diag(hir_analysis_extern_crate_not_idiomatic)]
256 pub struct ExternCrateNotIdiomatic {
259 applicability = "machine-applicable",
260 code = "{suggestion_code}"
263 pub msg_code: String,
264 pub suggestion_code: String,
267 #[derive(Diagnostic)]
268 #[diag(hir_analysis_const_impl_for_non_const_trait)]
269 pub struct ConstImplForNonConstTrait {
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>,
281 #[derive(Diagnostic)]
282 #[diag(hir_analysis_const_bound_for_non_const_trait)]
283 pub struct ConstBoundForNonConstTrait {
288 #[derive(Diagnostic)]
289 #[diag(hir_analysis_self_in_impl_self)]
290 pub struct SelfInImplSelf {
297 #[derive(Diagnostic)]
298 #[diag(hir_analysis_linkage_type, code = "E0791")]
299 pub(crate) struct LinkageType {