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>,
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(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 {
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(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 {
146 pub what: &'static str,
149 pub struct MissingTypeParams {
152 pub span_snippet: Option<String>,
153 pub missing_type_params: Vec<Symbol>,
154 pub empty_generic_args: bool,
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(
162 rustc_errors::fluent::hir_analysis_missing_type_params,
165 err.set_arg("parameterCount", self.missing_type_params.len());
168 self.missing_type_params
170 .map(|n| format!("`{}`", n))
175 err.span_label(self.def_span, rustc_errors::fluent::label);
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.
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>`.
190 rustc_errors::fluent::suggestion,
194 self.missing_type_params
196 .map(|n| n.to_string())
200 Applicability::HasPlaceholders,
206 err.span_label(self.span, rustc_errors::fluent::no_suggestion_label);
209 err.note(rustc_errors::fluent::note);
214 #[derive(Diagnostic)]
215 #[diag(hir_analysis_manual_implementation, code = "E0183")]
217 pub struct ManualImplementation {
221 pub trait_name: String,
224 #[derive(Diagnostic)]
225 #[diag(hir_analysis_substs_on_overridden_impl)]
226 pub struct SubstsOnOverriddenImpl {
231 #[derive(LintDiagnostic)]
232 #[diag(hir_analysis_unused_extern_crate)]
233 pub struct UnusedExternCrate {
234 #[suggestion(applicability = "machine-applicable", code = "")]
238 #[derive(LintDiagnostic)]
239 #[diag(hir_analysis_extern_crate_not_idiomatic)]
240 pub struct ExternCrateNotIdiomatic {
241 #[suggestion_short(applicability = "machine-applicable", code = "{suggestion_code}")]
243 pub msg_code: String,
244 pub suggestion_code: String,
247 #[derive(Diagnostic)]
248 #[diag(hir_analysis_expected_used_symbol)]
249 pub struct ExpectedUsedSymbol {
254 #[derive(Diagnostic)]
255 #[diag(hir_analysis_const_impl_for_non_const_trait)]
256 pub struct ConstImplForNonConstTrait {
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>,
268 #[derive(Diagnostic)]
269 #[diag(hir_analysis_const_bound_for_non_const_trait)]
270 pub struct ConstBoundForNonConstTrait {
275 #[derive(Diagnostic)]
276 #[diag(hir_analysis_self_in_impl_self)]
277 pub struct SelfInImplSelf {