1 //! Errors emitted by `hir_analysis`.
3 use rustc_errors::IntoDiagnostic;
4 use rustc_errors::{error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed, Handler};
5 use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
6 use rustc_middle::ty::Ty;
7 use rustc_span::{symbol::Ident, Span, Symbol};
10 #[diag(hir_analysis::field_multiply_specified_in_initializer, code = "E0062")]
11 pub struct FieldMultiplySpecifiedInInitializer {
15 #[label(hir_analysis::previous_use_label)]
21 #[diag(hir_analysis::unrecognized_atomic_operation, code = "E0092")]
22 pub struct UnrecognizedAtomicOperation<'a> {
30 #[diag(hir_analysis::wrong_number_of_generic_arguments_to_intrinsic, code = "E0094")]
31 pub struct WrongNumberOfGenericArgumentsToIntrinsic<'a> {
41 #[diag(hir_analysis::unrecognized_intrinsic_function, code = "E0093")]
42 pub struct UnrecognizedIntrinsicFunction {
50 #[diag(hir_analysis::lifetimes_or_bounds_mismatch_on_trait, code = "E0195")]
51 pub struct LifetimesOrBoundsMismatchOnTrait {
55 #[label(hir_analysis::generics_label)]
56 pub generics_span: Option<Span>,
57 pub item_kind: &'static str,
62 #[diag(hir_analysis::drop_impl_on_wrong_item, code = "E0120")]
63 pub struct DropImplOnWrongItem {
70 #[diag(hir_analysis::field_already_declared, code = "E0124")]
71 pub struct FieldAlreadyDeclared {
72 pub field_name: Ident,
76 #[label(hir_analysis::previous_decl_label)]
81 #[diag(hir_analysis::copy_impl_on_type_with_dtor, code = "E0184")]
82 pub struct CopyImplOnTypeWithDtor {
89 #[diag(hir_analysis::multiple_relaxed_default_bounds, code = "E0203")]
90 pub struct MultipleRelaxedDefaultBounds {
96 #[diag(hir_analysis::copy_impl_on_non_adt, code = "E0206")]
97 pub struct CopyImplOnNonAdt {
103 #[derive(Diagnostic)]
104 #[diag(hir_analysis::trait_object_declared_with_no_traits, code = "E0224")]
105 pub struct TraitObjectDeclaredWithNoTraits {
108 #[label(hir_analysis::alias_span)]
109 pub trait_alias_span: Option<Span>,
112 #[derive(Diagnostic)]
113 #[diag(hir_analysis::ambiguous_lifetime_bound, code = "E0227")]
114 pub struct AmbiguousLifetimeBound {
119 #[derive(Diagnostic)]
120 #[diag(hir_analysis::assoc_type_binding_not_allowed, code = "E0229")]
121 pub struct AssocTypeBindingNotAllowed {
127 #[derive(Diagnostic)]
128 #[diag(hir_analysis::functional_record_update_on_non_struct, code = "E0436")]
129 pub struct FunctionalRecordUpdateOnNonStruct {
134 #[derive(Diagnostic)]
135 #[diag(hir_analysis::typeof_reserved_keyword_used, code = "E0516")]
136 pub struct TypeofReservedKeywordUsed<'tcx> {
141 #[suggestion_verbose(code = "{ty}")]
142 pub opt_sugg: Option<(Span, Applicability)>,
145 #[derive(Diagnostic)]
146 #[diag(hir_analysis::return_stmt_outside_of_fn_body, code = "E0572")]
147 pub struct ReturnStmtOutsideOfFnBody {
150 #[label(hir_analysis::encl_body_label)]
151 pub encl_body_span: Option<Span>,
152 #[label(hir_analysis::encl_fn_label)]
153 pub encl_fn_span: Option<Span>,
156 #[derive(Diagnostic)]
157 #[diag(hir_analysis::yield_expr_outside_of_generator, code = "E0627")]
158 pub struct YieldExprOutsideOfGenerator {
163 #[derive(Diagnostic)]
164 #[diag(hir_analysis::struct_expr_non_exhaustive, code = "E0639")]
165 pub struct StructExprNonExhaustive {
168 pub what: &'static str,
171 #[derive(Diagnostic)]
172 #[diag(hir_analysis::method_call_on_unknown_type, code = "E0699")]
173 pub struct MethodCallOnUnknownType {
178 #[derive(Diagnostic)]
179 #[diag(hir_analysis::value_of_associated_struct_already_specified, code = "E0719")]
180 pub struct ValueOfAssociatedStructAlreadySpecified {
184 #[label(hir_analysis::previous_bound_label)]
186 pub item_name: Ident,
187 pub def_path: String,
190 #[derive(Diagnostic)]
191 #[diag(hir_analysis::address_of_temporary_taken, code = "E0745")]
192 pub struct AddressOfTemporaryTaken {
198 #[derive(Subdiagnostic)]
199 pub enum AddReturnTypeSuggestion {
201 hir_analysis::add_return_type_add,
202 code = "-> {found} ",
203 applicability = "machine-applicable"
211 hir_analysis::add_return_type_missing_here,
213 applicability = "has-placeholders"
221 #[derive(Subdiagnostic)]
222 pub enum ExpectedReturnTypeLabel<'tcx> {
223 #[label(hir_analysis::expected_default_return_type)]
228 #[label(hir_analysis::expected_return_type)]
236 #[derive(Diagnostic)]
237 #[diag(hir_analysis::unconstrained_opaque_type)]
239 pub struct UnconstrainedOpaqueType {
245 pub struct MissingTypeParams {
248 pub span_snippet: Option<String>,
249 pub missing_type_params: Vec<Symbol>,
250 pub empty_generic_args: bool,
253 // Manual implementation of `IntoDiagnostic` to be able to call `span_to_snippet`.
254 impl<'a> IntoDiagnostic<'a> for MissingTypeParams {
255 fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
256 let mut err = handler.struct_span_err_with_code(
258 rustc_errors::fluent::hir_analysis::missing_type_params,
261 err.set_arg("parameterCount", self.missing_type_params.len());
264 self.missing_type_params
266 .map(|n| format!("`{}`", n))
271 err.span_label(self.def_span, rustc_errors::fluent::hir_analysis::label);
273 let mut suggested = false;
274 // Don't suggest setting the type params if there are some already: the order is
275 // tricky to get right and the user will already know what the syntax is.
276 if let Some(snippet) = self.span_snippet && self.empty_generic_args {
277 if snippet.ends_with('>') {
278 // The user wrote `Trait<'a, T>` or similar. To provide an accurate suggestion
279 // we would have to preserve the right order. For now, as clearly the user is
280 // aware of the syntax, we do nothing.
282 // The user wrote `Iterator`, so we don't have a type we can suggest, but at
283 // least we can clue them to the correct syntax `Iterator<Type>`.
286 rustc_errors::fluent::hir_analysis::suggestion,
290 self.missing_type_params
292 .map(|n| n.to_string())
296 Applicability::HasPlaceholders,
302 err.span_label(self.span, rustc_errors::fluent::hir_analysis::no_suggestion_label);
305 err.note(rustc_errors::fluent::hir_analysis::note);
310 #[derive(Diagnostic)]
311 #[diag(hir_analysis::manual_implementation, code = "E0183")]
313 pub struct ManualImplementation {
317 pub trait_name: String,
320 #[derive(Diagnostic)]
321 #[diag(hir_analysis::substs_on_overridden_impl)]
322 pub struct SubstsOnOverriddenImpl {
327 #[derive(LintDiagnostic)]
328 #[diag(hir_analysis::unused_extern_crate)]
329 pub struct UnusedExternCrate {
330 #[suggestion(applicability = "machine-applicable", code = "")]
334 #[derive(LintDiagnostic)]
335 #[diag(hir_analysis::extern_crate_not_idiomatic)]
336 pub struct ExternCrateNotIdiomatic {
337 #[suggestion_short(applicability = "machine-applicable", code = "{suggestion_code}")]
339 pub msg_code: String,
340 pub suggestion_code: String,
343 #[derive(Diagnostic)]
344 #[diag(hir_analysis::expected_used_symbol)]
345 pub struct ExpectedUsedSymbol {