1 //! Errors emitted by `rustc_hir_analysis`.
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};
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(hir_analysis::generics_label)]
45 pub generics_span: Option<Span>,
46 pub item_kind: &'static str,
51 #[diag(hir_analysis::drop_impl_on_wrong_item, code = "E0120")]
52 pub struct DropImplOnWrongItem {
59 #[diag(hir_analysis::field_already_declared, code = "E0124")]
60 pub struct FieldAlreadyDeclared {
61 pub field_name: Ident,
65 #[label(hir_analysis::previous_decl_label)]
70 #[diag(hir_analysis::copy_impl_on_type_with_dtor, code = "E0184")]
71 pub struct CopyImplOnTypeWithDtor {
78 #[diag(hir_analysis::multiple_relaxed_default_bounds, code = "E0203")]
79 pub struct MultipleRelaxedDefaultBounds {
85 #[diag(hir_analysis::copy_impl_on_non_adt, code = "E0206")]
86 pub struct CopyImplOnNonAdt {
93 #[diag(hir_analysis::trait_object_declared_with_no_traits, code = "E0224")]
94 pub struct TraitObjectDeclaredWithNoTraits {
97 #[label(hir_analysis::alias_span)]
98 pub trait_alias_span: Option<Span>,
101 #[derive(Diagnostic)]
102 #[diag(hir_analysis::ambiguous_lifetime_bound, code = "E0227")]
103 pub struct AmbiguousLifetimeBound {
108 #[derive(Diagnostic)]
109 #[diag(hir_analysis::assoc_type_binding_not_allowed, code = "E0229")]
110 pub struct AssocTypeBindingNotAllowed {
116 #[derive(Diagnostic)]
117 #[diag(hir_analysis::typeof_reserved_keyword_used, code = "E0516")]
118 pub struct TypeofReservedKeywordUsed<'tcx> {
123 #[suggestion_verbose(code = "{ty}")]
124 pub opt_sugg: Option<(Span, Applicability)>,
127 #[derive(Diagnostic)]
128 #[diag(hir_analysis::value_of_associated_struct_already_specified, code = "E0719")]
129 pub struct ValueOfAssociatedStructAlreadySpecified {
133 #[label(hir_analysis::previous_bound_label)]
135 pub item_name: Ident,
136 pub def_path: String,
139 #[derive(Diagnostic)]
140 #[diag(hir_analysis::unconstrained_opaque_type)]
142 pub struct UnconstrainedOpaqueType {
148 pub struct MissingTypeParams {
151 pub span_snippet: Option<String>,
152 pub missing_type_params: Vec<Symbol>,
153 pub empty_generic_args: bool,
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(
161 rustc_errors::fluent::hir_analysis::missing_type_params,
164 err.set_arg("parameterCount", self.missing_type_params.len());
167 self.missing_type_params
169 .map(|n| format!("`{}`", n))
174 err.span_label(self.def_span, rustc_errors::fluent::hir_analysis::label);
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.
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>`.
189 rustc_errors::fluent::hir_analysis::suggestion,
193 self.missing_type_params
195 .map(|n| n.to_string())
199 Applicability::HasPlaceholders,
205 err.span_label(self.span, rustc_errors::fluent::hir_analysis::no_suggestion_label);
208 err.note(rustc_errors::fluent::hir_analysis::note);
213 #[derive(Diagnostic)]
214 #[diag(hir_analysis::manual_implementation, code = "E0183")]
216 pub struct ManualImplementation {
220 pub trait_name: String,
223 #[derive(Diagnostic)]
224 #[diag(hir_analysis::substs_on_overridden_impl)]
225 pub struct SubstsOnOverriddenImpl {
230 #[derive(LintDiagnostic)]
231 #[diag(hir_analysis::unused_extern_crate)]
232 pub struct UnusedExternCrate {
233 #[suggestion(applicability = "machine-applicable", code = "")]
237 #[derive(LintDiagnostic)]
238 #[diag(hir_analysis::extern_crate_not_idiomatic)]
239 pub struct ExternCrateNotIdiomatic {
240 #[suggestion_short(applicability = "machine-applicable", code = "{suggestion_code}")]
242 pub msg_code: String,
243 pub suggestion_code: String,
246 #[derive(Diagnostic)]
247 #[diag(hir_analysis::expected_used_symbol)]
248 pub struct ExpectedUsedSymbol {