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_drop_impl_on_wrong_item, code = "E0120")]
56 pub struct DropImplOnWrongItem {
63 #[diag(hir_analysis_field_already_declared, code = "E0124")]
64 pub struct FieldAlreadyDeclared {
65 pub field_name: Ident,
69 #[label(previous_decl_label)]
74 #[diag(hir_analysis_copy_impl_on_type_with_dtor, code = "E0184")]
75 pub struct CopyImplOnTypeWithDtor {
82 #[diag(hir_analysis_multiple_relaxed_default_bounds, code = "E0203")]
83 pub struct MultipleRelaxedDefaultBounds {
89 #[diag(hir_analysis_copy_impl_on_non_adt, code = "E0206")]
90 pub struct CopyImplOnNonAdt {
97 #[diag(hir_analysis_trait_object_declared_with_no_traits, code = "E0224")]
98 pub struct TraitObjectDeclaredWithNoTraits {
102 pub trait_alias_span: Option<Span>,
105 #[derive(Diagnostic)]
106 #[diag(hir_analysis_ambiguous_lifetime_bound, code = "E0227")]
107 pub struct AmbiguousLifetimeBound {
112 #[derive(Diagnostic)]
113 #[diag(hir_analysis_assoc_type_binding_not_allowed, code = "E0229")]
114 pub struct AssocTypeBindingNotAllowed {
120 #[derive(Diagnostic)]
121 #[diag(hir_analysis_typeof_reserved_keyword_used, code = "E0516")]
122 pub struct TypeofReservedKeywordUsed<'tcx> {
127 #[suggestion(style = "verbose", code = "{ty}")]
128 pub opt_sugg: Option<(Span, Applicability)>,
131 #[derive(Diagnostic)]
132 #[diag(hir_analysis_value_of_associated_struct_already_specified, code = "E0719")]
133 pub struct ValueOfAssociatedStructAlreadySpecified {
137 #[label(previous_bound_label)]
139 pub item_name: Ident,
140 pub def_path: String,
143 #[derive(Diagnostic)]
144 #[diag(hir_analysis_unconstrained_opaque_type)]
146 pub struct UnconstrainedOpaqueType {
150 pub what: &'static str,
153 pub struct MissingTypeParams {
156 pub span_snippet: Option<String>,
157 pub missing_type_params: Vec<Symbol>,
158 pub empty_generic_args: bool,
161 // Manual implementation of `IntoDiagnostic` to be able to call `span_to_snippet`.
162 impl<'a> IntoDiagnostic<'a> for MissingTypeParams {
164 fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
165 let mut err = handler.struct_span_err_with_code(
167 rustc_errors::fluent::hir_analysis_missing_type_params,
170 err.set_arg("parameterCount", self.missing_type_params.len());
173 self.missing_type_params
175 .map(|n| format!("`{}`", n))
180 err.span_label(self.def_span, rustc_errors::fluent::label);
182 let mut suggested = false;
183 // Don't suggest setting the type params if there are some already: the order is
184 // tricky to get right and the user will already know what the syntax is.
185 if let Some(snippet) = self.span_snippet && self.empty_generic_args {
186 if snippet.ends_with('>') {
187 // The user wrote `Trait<'a, T>` or similar. To provide an accurate suggestion
188 // we would have to preserve the right order. For now, as clearly the user is
189 // aware of the syntax, we do nothing.
191 // The user wrote `Iterator`, so we don't have a type we can suggest, but at
192 // least we can clue them to the correct syntax `Iterator<Type>`.
195 rustc_errors::fluent::suggestion,
199 self.missing_type_params
201 .map(|n| n.to_string())
205 Applicability::HasPlaceholders,
211 err.span_label(self.span, rustc_errors::fluent::no_suggestion_label);
214 err.note(rustc_errors::fluent::note);
219 #[derive(Diagnostic)]
220 #[diag(hir_analysis_manual_implementation, code = "E0183")]
222 pub struct ManualImplementation {
226 pub trait_name: String,
229 #[derive(Diagnostic)]
230 #[diag(hir_analysis_substs_on_overridden_impl)]
231 pub struct SubstsOnOverriddenImpl {
236 #[derive(LintDiagnostic)]
237 #[diag(hir_analysis_unused_extern_crate)]
238 pub struct UnusedExternCrate {
239 #[suggestion(applicability = "machine-applicable", code = "")]
243 #[derive(LintDiagnostic)]
244 #[diag(hir_analysis_extern_crate_not_idiomatic)]
245 pub struct ExternCrateNotIdiomatic {
248 applicability = "machine-applicable",
249 code = "{suggestion_code}"
252 pub msg_code: String,
253 pub suggestion_code: String,
256 #[derive(Diagnostic)]
257 #[diag(hir_analysis_const_impl_for_non_const_trait)]
258 pub struct ConstImplForNonConstTrait {
260 pub trait_ref_span: Span,
261 pub trait_name: String,
262 #[suggestion(applicability = "machine-applicable", code = "#[const_trait]")]
263 pub local_trait_span: Option<Span>,
270 #[derive(Diagnostic)]
271 #[diag(hir_analysis_const_bound_for_non_const_trait)]
272 pub struct ConstBoundForNonConstTrait {
277 #[derive(Diagnostic)]
278 #[diag(hir_analysis_self_in_impl_self)]
279 pub struct SelfInImplSelf {
286 #[derive(Diagnostic)]
287 #[diag(hir_analysis_linkage_type, code = "E0791")]
288 pub(crate) struct LinkageType {