1 //! Errors emitted by typeck.
2 use rustc_errors::IntoDiagnostic;
3 use rustc_errors::{error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed, Handler};
4 use rustc_macros::{DiagnosticHandler, LintDiagnostic, SessionSubdiagnostic};
5 use rustc_middle::ty::Ty;
6 use rustc_span::{symbol::Ident, Span, Symbol};
8 #[derive(DiagnosticHandler)]
9 #[diag(typeck::field_multiply_specified_in_initializer, code = "E0062")]
10 pub struct FieldMultiplySpecifiedInInitializer {
14 #[label(typeck::previous_use_label)]
19 #[derive(DiagnosticHandler)]
20 #[diag(typeck::unrecognized_atomic_operation, code = "E0092")]
21 pub struct UnrecognizedAtomicOperation<'a> {
28 #[derive(DiagnosticHandler)]
29 #[diag(typeck::wrong_number_of_generic_arguments_to_intrinsic, code = "E0094")]
30 pub struct WrongNumberOfGenericArgumentsToIntrinsic<'a> {
39 #[derive(DiagnosticHandler)]
40 #[diag(typeck::unrecognized_intrinsic_function, code = "E0093")]
41 pub struct UnrecognizedIntrinsicFunction {
48 #[derive(DiagnosticHandler)]
49 #[diag(typeck::lifetimes_or_bounds_mismatch_on_trait, code = "E0195")]
50 pub struct LifetimesOrBoundsMismatchOnTrait {
54 #[label(typeck::generics_label)]
55 pub generics_span: Option<Span>,
56 pub item_kind: &'static str,
60 #[derive(DiagnosticHandler)]
61 #[diag(typeck::drop_impl_on_wrong_item, code = "E0120")]
62 pub struct DropImplOnWrongItem {
68 #[derive(DiagnosticHandler)]
69 #[diag(typeck::field_already_declared, code = "E0124")]
70 pub struct FieldAlreadyDeclared {
71 pub field_name: Ident,
75 #[label(typeck::previous_decl_label)]
79 #[derive(DiagnosticHandler)]
80 #[diag(typeck::copy_impl_on_type_with_dtor, code = "E0184")]
81 pub struct CopyImplOnTypeWithDtor {
87 #[derive(DiagnosticHandler)]
88 #[diag(typeck::multiple_relaxed_default_bounds, code = "E0203")]
89 pub struct MultipleRelaxedDefaultBounds {
94 #[derive(DiagnosticHandler)]
95 #[diag(typeck::copy_impl_on_non_adt, code = "E0206")]
96 pub struct CopyImplOnNonAdt {
102 #[derive(DiagnosticHandler)]
103 #[diag(typeck::trait_object_declared_with_no_traits, code = "E0224")]
104 pub struct TraitObjectDeclaredWithNoTraits {
107 #[label(typeck::alias_span)]
108 pub trait_alias_span: Option<Span>,
111 #[derive(DiagnosticHandler)]
112 #[diag(typeck::ambiguous_lifetime_bound, code = "E0227")]
113 pub struct AmbiguousLifetimeBound {
118 #[derive(DiagnosticHandler)]
119 #[diag(typeck::assoc_type_binding_not_allowed, code = "E0229")]
120 pub struct AssocTypeBindingNotAllowed {
126 #[derive(DiagnosticHandler)]
127 #[diag(typeck::functional_record_update_on_non_struct, code = "E0436")]
128 pub struct FunctionalRecordUpdateOnNonStruct {
133 #[derive(DiagnosticHandler)]
134 #[diag(typeck::typeof_reserved_keyword_used, code = "E0516")]
135 pub struct TypeofReservedKeywordUsed<'tcx> {
140 #[suggestion_verbose(code = "{ty}")]
141 pub opt_sugg: Option<(Span, Applicability)>,
144 #[derive(DiagnosticHandler)]
145 #[diag(typeck::return_stmt_outside_of_fn_body, code = "E0572")]
146 pub struct ReturnStmtOutsideOfFnBody {
149 #[label(typeck::encl_body_label)]
150 pub encl_body_span: Option<Span>,
151 #[label(typeck::encl_fn_label)]
152 pub encl_fn_span: Option<Span>,
155 #[derive(DiagnosticHandler)]
156 #[diag(typeck::yield_expr_outside_of_generator, code = "E0627")]
157 pub struct YieldExprOutsideOfGenerator {
162 #[derive(DiagnosticHandler)]
163 #[diag(typeck::struct_expr_non_exhaustive, code = "E0639")]
164 pub struct StructExprNonExhaustive {
167 pub what: &'static str,
170 #[derive(DiagnosticHandler)]
171 #[diag(typeck::method_call_on_unknown_type, code = "E0699")]
172 pub struct MethodCallOnUnknownType {
177 #[derive(DiagnosticHandler)]
178 #[diag(typeck::value_of_associated_struct_already_specified, code = "E0719")]
179 pub struct ValueOfAssociatedStructAlreadySpecified {
183 #[label(typeck::previous_bound_label)]
185 pub item_name: Ident,
186 pub def_path: String,
189 #[derive(DiagnosticHandler)]
190 #[diag(typeck::address_of_temporary_taken, code = "E0745")]
191 pub struct AddressOfTemporaryTaken {
197 #[derive(SessionSubdiagnostic)]
198 pub enum AddReturnTypeSuggestion {
200 typeck::add_return_type_add,
201 code = "-> {found} ",
202 applicability = "machine-applicable"
210 typeck::add_return_type_missing_here,
212 applicability = "has-placeholders"
220 #[derive(SessionSubdiagnostic)]
221 pub enum ExpectedReturnTypeLabel<'tcx> {
222 #[label(typeck::expected_default_return_type)]
227 #[label(typeck::expected_return_type)]
235 #[derive(DiagnosticHandler)]
236 #[diag(typeck::unconstrained_opaque_type)]
238 pub struct UnconstrainedOpaqueType {
244 pub struct MissingTypeParams {
247 pub span_snippet: Option<String>,
248 pub missing_type_params: Vec<Symbol>,
249 pub empty_generic_args: bool,
252 // Manual implementation of `DiagnosticHandler` to be able to call `span_to_snippet`.
253 impl<'a> IntoDiagnostic<'a> for MissingTypeParams {
254 fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
255 let mut err = handler.struct_span_err_with_code(
257 rustc_errors::fluent::typeck::missing_type_params,
260 err.set_arg("parameterCount", self.missing_type_params.len());
263 self.missing_type_params
265 .map(|n| format!("`{}`", n))
270 err.span_label(self.def_span, rustc_errors::fluent::typeck::label);
272 let mut suggested = false;
273 // Don't suggest setting the type params if there are some already: the order is
274 // tricky to get right and the user will already know what the syntax is.
275 if let Some(snippet) = self.span_snippet && self.empty_generic_args {
276 if snippet.ends_with('>') {
277 // The user wrote `Trait<'a, T>` or similar. To provide an accurate suggestion
278 // we would have to preserve the right order. For now, as clearly the user is
279 // aware of the syntax, we do nothing.
281 // The user wrote `Iterator`, so we don't have a type we can suggest, but at
282 // least we can clue them to the correct syntax `Iterator<Type>`.
285 rustc_errors::fluent::typeck::suggestion,
289 self.missing_type_params
291 .map(|n| n.to_string())
295 Applicability::HasPlaceholders,
301 err.span_label(self.span, rustc_errors::fluent::typeck::no_suggestion_label);
304 err.note(rustc_errors::fluent::typeck::note);
309 #[derive(DiagnosticHandler)]
310 #[diag(typeck::manual_implementation, code = "E0183")]
312 pub struct ManualImplementation {
316 pub trait_name: String,
319 #[derive(DiagnosticHandler)]
320 #[diag(typeck::substs_on_overridden_impl)]
321 pub struct SubstsOnOverriddenImpl {
326 #[derive(LintDiagnostic)]
327 #[diag(typeck::unused_extern_crate)]
328 pub struct UnusedExternCrate {
329 #[suggestion(applicability = "machine-applicable", code = "")]
333 #[derive(LintDiagnostic)]
334 #[diag(typeck::extern_crate_not_idiomatic)]
335 pub struct ExternCrateNotIdiomatic {
336 #[suggestion_short(applicability = "machine-applicable", code = "{suggestion_code}")]
338 pub msg_code: String,
339 pub suggestion_code: String,
342 #[derive(DiagnosticHandler)]
343 #[diag(typeck::expected_used_symbol)]
344 pub struct ExpectedUsedSymbol {