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(code = "E0062", slug = "typeck-field-multiply-specified-in-initializer")]
10 pub struct FieldMultiplySpecifiedInInitializer {
14 #[label = "previous-use-label"]
19 #[derive(SessionDiagnostic)]
20 #[error(code = "E0092", slug = "typeck-unrecognized-atomic-operation")]
21 pub struct UnrecognizedAtomicOperation<'a> {
28 #[derive(SessionDiagnostic)]
29 #[error(code = "E0094", slug = "typeck-wrong-number-of-generic-arguments-to-intrinsic")]
30 pub struct WrongNumberOfGenericArgumentsToIntrinsic<'a> {
39 #[derive(SessionDiagnostic)]
40 #[error(code = "E0093", slug = "typeck-unrecognized-intrinsic-function")]
41 pub struct UnrecognizedIntrinsicFunction {
48 #[derive(SessionDiagnostic)]
49 #[error(code = "E0195", slug = "typeck-lifetimes-or-bounds-mismatch-on-trait")]
50 pub struct LifetimesOrBoundsMismatchOnTrait {
54 #[label = "generics-label"]
55 pub generics_span: Option<Span>,
56 pub item_kind: &'static str,
60 #[derive(SessionDiagnostic)]
61 #[error(code = "E0120", slug = "typeck-drop-impl-on-wrong-item")]
62 pub struct DropImplOnWrongItem {
68 #[derive(SessionDiagnostic)]
69 #[error(code = "E0124", slug = "typeck-field-already-declared")]
70 pub struct FieldAlreadyDeclared {
71 pub field_name: Ident,
75 #[label = "previous-decl-label"]
79 #[derive(SessionDiagnostic)]
80 #[error(code = "E0184", slug = "typeck-copy-impl-on-type-with-dtor")]
81 pub struct CopyImplOnTypeWithDtor {
87 #[derive(SessionDiagnostic)]
88 #[error(code = "E0203", slug = "typeck-multiple-relaxed-default-bounds")]
89 pub struct MultipleRelaxedDefaultBounds {
94 #[derive(SessionDiagnostic)]
95 #[error(code = "E0206", slug = "typeck-copy-impl-on-non-adt")]
96 pub struct CopyImplOnNonAdt {
102 #[derive(SessionDiagnostic)]
103 #[error(code = "E0224", slug = "typeck-trait-object-declared-with-no-traits")]
104 pub struct TraitObjectDeclaredWithNoTraits {
107 #[label = "alias-span"]
108 pub trait_alias_span: Option<Span>,
111 #[derive(SessionDiagnostic)]
112 #[error(code = "E0227", slug = "typeck-ambiguous-lifetime-bound")]
113 pub struct AmbiguousLifetimeBound {
118 #[derive(SessionDiagnostic)]
119 #[error(code = "E0229", slug = "typeck-assoc-type-binding-not-allowed")]
120 pub struct AssocTypeBindingNotAllowed {
126 #[derive(SessionDiagnostic)]
127 #[error(code = "E0436", slug = "typeck-functional-record-update-on-non-struct")]
128 pub struct FunctionalRecordUpdateOnNonStruct {
133 #[derive(SessionDiagnostic)]
134 #[error(code = "E0516", slug = "typeck-typeof-reserved-keyword-used")]
135 pub struct TypeofReservedKeywordUsed<'tcx> {
140 #[suggestion_verbose(code = "{ty}")]
141 pub opt_sugg: Option<(Span, Applicability)>,
144 #[derive(SessionDiagnostic)]
145 #[error(code = "E0572", slug = "typeck-return-stmt-outside-of-fn-body")]
146 pub struct ReturnStmtOutsideOfFnBody {
149 #[label = "encl-body-label"]
150 pub encl_body_span: Option<Span>,
151 #[label = "encl-fn-label"]
152 pub encl_fn_span: Option<Span>,
155 #[derive(SessionDiagnostic)]
156 #[error(code = "E0627", slug = "typeck-yield-expr-outside-of-generator")]
157 pub struct YieldExprOutsideOfGenerator {
162 #[derive(SessionDiagnostic)]
163 #[error(code = "E0639", slug = "typeck-struct-expr-non-exhaustive")]
164 pub struct StructExprNonExhaustive {
167 pub what: &'static str,
170 #[derive(SessionDiagnostic)]
171 #[error(code = "E0699", slug = "typeck-method-call-on-unknown-type")]
172 pub struct MethodCallOnUnknownType {
177 #[derive(SessionDiagnostic)]
178 #[error(code = "E0719", slug = "typeck-value-of-associated-struct-already-specified")]
179 pub struct ValueOfAssociatedStructAlreadySpecified {
183 #[label = "previous-bound-label"]
185 pub item_name: Ident,
186 pub def_path: String,
189 #[derive(SessionDiagnostic)]
190 #[error(code = "E0745", slug = "typeck-address-of-temporary-taken")]
191 pub struct AddressOfTemporaryTaken {
197 #[derive(SessionSubdiagnostic)]
198 pub enum AddReturnTypeSuggestion<'tcx> {
200 slug = "typeck-add-return-type-add",
201 code = "-> {found} ",
202 applicability = "machine-applicable"
210 slug = "typeck-add-return-type-missing-here",
212 applicability = "has-placeholders"
220 #[derive(SessionSubdiagnostic)]
221 pub enum ExpectedReturnTypeLabel<'tcx> {
222 #[label(slug = "typeck-expected-default-return-type")]
227 #[label(slug = "typeck-expected-return-type")]
235 #[derive(SessionDiagnostic)]
236 #[error(slug = "typeck-unconstrained-opaque-type")]
238 pub struct UnconstrainedOpaqueType {
244 #[derive(SessionDiagnostic)]
245 #[error(code = "E0632", slug = "typeck-explicit-generic-args-with-impl-trait")]
247 pub struct ExplicitGenericArgsWithImplTrait {
250 pub spans: Vec<Span>,
252 pub is_nightly_build: Option<()>,
255 pub struct MissingTypeParams {
258 pub missing_type_params: Vec<String>,
259 pub empty_generic_args: bool,
262 // Manual implementation of `SessionDiagnostic` to be able to call `span_to_snippet`.
263 impl<'a> SessionDiagnostic<'a> for MissingTypeParams {
264 fn into_diagnostic(self, sess: &'a ParseSess) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
265 let mut err = sess.span_diagnostic.struct_span_err_with_code(
267 rustc_errors::fluent::typeck::missing_type_params,
270 err.set_arg("parameterCount", self.missing_type_params.len());
273 self.missing_type_params
275 .map(|n| format!("`{}`", n))
280 err.span_label(self.def_span, rustc_errors::fluent::typeck::missing_type_params_label);
282 let mut suggested = false;
283 if let (Ok(snippet), true) = (
284 sess.source_map().span_to_snippet(self.span),
285 // Don't suggest setting the type params if there are some already: the order is
286 // tricky to get right and the user will already know what the syntax is.
287 self.empty_generic_args,
289 if snippet.ends_with('>') {
290 // The user wrote `Trait<'a, T>` or similar. To provide an accurate suggestion
291 // we would have to preserve the right order. For now, as clearly the user is
292 // aware of the syntax, we do nothing.
294 // The user wrote `Iterator`, so we don't have a type we can suggest, but at
295 // least we can clue them to the correct syntax `Iterator<Type>`.
298 rustc_errors::fluent::typeck::missing_type_params_suggestion,
299 format!("{}<{}>", snippet, self.missing_type_params.join(", ")),
300 Applicability::HasPlaceholders,
308 rustc_errors::fluent::typeck::missing_type_params_no_suggestion_label,
312 err.note(rustc_errors::fluent::typeck::missing_type_params_note);
317 #[derive(SessionDiagnostic)]
318 #[error(code = "E0183", slug = "typeck-manual-implementation")]
320 pub struct ManualImplementation {
324 pub trait_name: String,
327 #[derive(SessionDiagnostic)]
328 #[error(slug = "typeck-substs-on-overridden-impl")]
329 pub struct SubstsOnOverriddenImpl {