1 //! Errors emitted by typeck.
2 use rustc_errors::{error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed};
3 use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
4 use rustc_middle::ty::Ty;
5 use rustc_session::{parse::ParseSess, SessionDiagnostic};
6 use rustc_span::{symbol::Ident, Span, Symbol};
8 #[derive(SessionDiagnostic)]
9 #[error(typeck::field_multiply_specified_in_initializer, code = "E0062")]
10 pub struct FieldMultiplySpecifiedInInitializer {
14 #[label(typeck::previous_use_label)]
19 #[derive(SessionDiagnostic)]
20 #[error(typeck::unrecognized_atomic_operation, code = "E0092")]
21 pub struct UnrecognizedAtomicOperation<'a> {
28 #[derive(SessionDiagnostic)]
29 #[error(typeck::wrong_number_of_generic_arguments_to_intrinsic, code = "E0094")]
30 pub struct WrongNumberOfGenericArgumentsToIntrinsic<'a> {
39 #[derive(SessionDiagnostic)]
40 #[error(typeck::unrecognized_intrinsic_function, code = "E0093")]
41 pub struct UnrecognizedIntrinsicFunction {
48 #[derive(SessionDiagnostic)]
49 #[error(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(SessionDiagnostic)]
61 #[error(typeck::drop_impl_on_wrong_item, code = "E0120")]
62 pub struct DropImplOnWrongItem {
68 #[derive(SessionDiagnostic)]
69 #[error(typeck::field_already_declared, code = "E0124")]
70 pub struct FieldAlreadyDeclared {
71 pub field_name: Ident,
75 #[label(typeck::previous_decl_label)]
79 #[derive(SessionDiagnostic)]
80 #[error(typeck::copy_impl_on_type_with_dtor, code = "E0184")]
81 pub struct CopyImplOnTypeWithDtor {
87 #[derive(SessionDiagnostic)]
88 #[error(typeck::multiple_relaxed_default_bounds, code = "E0203")]
89 pub struct MultipleRelaxedDefaultBounds {
94 #[derive(SessionDiagnostic)]
95 #[error(typeck::copy_impl_on_non_adt, code = "E0206")]
96 pub struct CopyImplOnNonAdt {
102 #[derive(SessionDiagnostic)]
103 #[error(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(SessionDiagnostic)]
112 #[error(typeck::ambiguous_lifetime_bound, code = "E0227")]
113 pub struct AmbiguousLifetimeBound {
118 #[derive(SessionDiagnostic)]
119 #[error(typeck::assoc_type_binding_not_allowed, code = "E0229")]
120 pub struct AssocTypeBindingNotAllowed {
126 #[derive(SessionDiagnostic)]
127 #[error(typeck::functional_record_update_on_non_struct, code = "E0436")]
128 pub struct FunctionalRecordUpdateOnNonStruct {
133 #[derive(SessionDiagnostic)]
134 #[error(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(SessionDiagnostic)]
145 #[error(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(SessionDiagnostic)]
156 #[error(typeck::yield_expr_outside_of_generator, code = "E0627")]
157 pub struct YieldExprOutsideOfGenerator {
162 #[derive(SessionDiagnostic)]
163 #[error(typeck::struct_expr_non_exhaustive, code = "E0639")]
164 pub struct StructExprNonExhaustive {
167 pub what: &'static str,
170 #[derive(SessionDiagnostic)]
171 #[error(typeck::method_call_on_unknown_type, code = "E0699")]
172 pub struct MethodCallOnUnknownType {
177 #[derive(SessionDiagnostic)]
178 #[error(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(SessionDiagnostic)]
190 #[error(typeck::address_of_temporary_taken, code = "E0745")]
191 pub struct AddressOfTemporaryTaken {
197 #[derive(SessionSubdiagnostic)]
198 pub enum AddReturnTypeSuggestion<'tcx> {
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(SessionDiagnostic)]
236 #[error(typeck::unconstrained_opaque_type)]
238 pub struct UnconstrainedOpaqueType {
244 pub struct MissingTypeParams {
247 pub missing_type_params: Vec<String>,
248 pub empty_generic_args: bool,
251 // Manual implementation of `SessionDiagnostic` to be able to call `span_to_snippet`.
252 impl<'a> SessionDiagnostic<'a> for MissingTypeParams {
253 fn into_diagnostic(self, sess: &'a ParseSess) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
254 let mut err = sess.span_diagnostic.struct_span_err_with_code(
256 rustc_errors::fluent::typeck::missing_type_params,
259 err.set_arg("parameterCount", self.missing_type_params.len());
262 self.missing_type_params
264 .map(|n| format!("`{}`", n))
269 err.span_label(self.def_span, rustc_errors::fluent::typeck::label);
271 let mut suggested = false;
272 if let (Ok(snippet), true) = (
273 sess.source_map().span_to_snippet(self.span),
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 self.empty_generic_args,
278 if snippet.ends_with('>') {
279 // The user wrote `Trait<'a, T>` or similar. To provide an accurate suggestion
280 // we would have to preserve the right order. For now, as clearly the user is
281 // aware of the syntax, we do nothing.
283 // The user wrote `Iterator`, so we don't have a type we can suggest, but at
284 // least we can clue them to the correct syntax `Iterator<Type>`.
287 rustc_errors::fluent::typeck::suggestion,
288 format!("{}<{}>", snippet, self.missing_type_params.join(", ")),
289 Applicability::HasPlaceholders,
295 err.span_label(self.span, rustc_errors::fluent::typeck::no_suggestion_label);
298 err.note(rustc_errors::fluent::typeck::note);
303 #[derive(SessionDiagnostic)]
304 #[error(typeck::manual_implementation, code = "E0183")]
306 pub struct ManualImplementation {
310 pub trait_name: String,
313 #[derive(SessionDiagnostic)]
314 #[error(typeck::substs_on_overridden_impl)]
315 pub struct SubstsOnOverriddenImpl {