1 //! Errors emitted by typeck.
3 error_code, Applicability, DiagnosticBuilder, DiagnosticMessage, ErrorGuaranteed,
5 use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
6 use rustc_middle::ty::Ty;
7 use rustc_session::{parse::ParseSess, SessionDiagnostic};
8 use rustc_span::{symbol::Ident, Span, Symbol};
10 #[derive(SessionDiagnostic)]
11 #[error(code = "E0062", slug = "typeck-field-multiply-specified-in-initializer")]
12 pub struct FieldMultiplySpecifiedInInitializer {
16 #[label = "previous-use-label"]
21 #[derive(SessionDiagnostic)]
22 #[error(code = "E0092", slug = "typeck-unrecognized-atomic-operation")]
23 pub struct UnrecognizedAtomicOperation<'a> {
30 #[derive(SessionDiagnostic)]
31 #[error(code = "E0094", slug = "typeck-wrong-number-of-generic-arguments-to-intrinsic")]
32 pub struct WrongNumberOfGenericArgumentsToIntrinsic<'a> {
41 #[derive(SessionDiagnostic)]
42 #[error(code = "E0093", slug = "typeck-unrecognized-intrinsic-function")]
43 pub struct UnrecognizedIntrinsicFunction {
50 #[derive(SessionDiagnostic)]
51 #[error(code = "E0195", slug = "typeck-lifetimes-or-bounds-mismatch-on-trait")]
52 pub struct LifetimesOrBoundsMismatchOnTrait {
56 #[label = "generics-label"]
57 pub generics_span: Option<Span>,
58 pub item_kind: &'static str,
62 #[derive(SessionDiagnostic)]
63 #[error(code = "E0120", slug = "typeck-drop-impl-on-wrong-item")]
64 pub struct DropImplOnWrongItem {
70 #[derive(SessionDiagnostic)]
71 #[error(code = "E0124", slug = "typeck-field-already-declared")]
72 pub struct FieldAlreadyDeclared {
73 pub field_name: Ident,
77 #[label = "previous-decl-label"]
81 #[derive(SessionDiagnostic)]
82 #[error(code = "E0184", slug = "typeck-copy-impl-on-type-with-dtor")]
83 pub struct CopyImplOnTypeWithDtor {
89 #[derive(SessionDiagnostic)]
90 #[error(code = "E0203", slug = "typeck-multiple-relaxed-default-bounds")]
91 pub struct MultipleRelaxedDefaultBounds {
96 #[derive(SessionDiagnostic)]
97 #[error(code = "E0206", slug = "typeck-copy-impl-on-non-adt")]
98 pub struct CopyImplOnNonAdt {
104 #[derive(SessionDiagnostic)]
105 #[error(code = "E0224", slug = "typeck-trait-object-declared-with-no-traits")]
106 pub struct TraitObjectDeclaredWithNoTraits {
109 #[label = "alias-span"]
110 pub trait_alias_span: Option<Span>,
113 #[derive(SessionDiagnostic)]
114 #[error(code = "E0227", slug = "typeck-ambiguous-lifetime-bound")]
115 pub struct AmbiguousLifetimeBound {
120 #[derive(SessionDiagnostic)]
121 #[error(code = "E0229", slug = "typeck-assoc-type-binding-not-allowed")]
122 pub struct AssocTypeBindingNotAllowed {
128 #[derive(SessionDiagnostic)]
129 #[error(code = "E0436", slug = "typeck-functional-record-update-on-non-struct")]
130 pub struct FunctionalRecordUpdateOnNonStruct {
135 #[derive(SessionDiagnostic)]
136 #[error(code = "E0516", slug = "typeck-typeof-reserved-keyword-used")]
137 pub struct TypeofReservedKeywordUsed<'tcx> {
142 #[suggestion_verbose(code = "{ty}")]
143 pub opt_sugg: Option<(Span, Applicability)>,
146 #[derive(SessionDiagnostic)]
147 #[error(code = "E0572", slug = "typeck-return-stmt-outside-of-fn-body")]
148 pub struct ReturnStmtOutsideOfFnBody {
151 #[label = "encl-body-label"]
152 pub encl_body_span: Option<Span>,
153 #[label = "encl-fn-label"]
154 pub encl_fn_span: Option<Span>,
157 #[derive(SessionDiagnostic)]
158 #[error(code = "E0627", slug = "typeck-yield-expr-outside-of-generator")]
159 pub struct YieldExprOutsideOfGenerator {
164 #[derive(SessionDiagnostic)]
165 #[error(code = "E0639", slug = "typeck-struct-expr-non-exhaustive")]
166 pub struct StructExprNonExhaustive {
169 pub what: &'static str,
172 #[derive(SessionDiagnostic)]
173 #[error(code = "E0699", slug = "typeck-method-call-on-unknown-type")]
174 pub struct MethodCallOnUnknownType {
179 #[derive(SessionDiagnostic)]
180 #[error(code = "E0719", slug = "typeck-value-of-associated-struct-already-specified")]
181 pub struct ValueOfAssociatedStructAlreadySpecified {
185 #[label = "previous-bound-label"]
187 pub item_name: Ident,
188 pub def_path: String,
191 #[derive(SessionDiagnostic)]
192 #[error(code = "E0745", slug = "typeck-address-of-temporary-taken")]
193 pub struct AddressOfTemporaryTaken {
199 #[derive(SessionSubdiagnostic)]
200 pub enum AddReturnTypeSuggestion<'tcx> {
202 slug = "typeck-add-return-type-add",
203 code = "-> {found} ",
204 applicability = "machine-applicable"
212 slug = "typeck-add-return-type-missing-here",
214 applicability = "has-placeholders"
222 #[derive(SessionSubdiagnostic)]
223 pub enum ExpectedReturnTypeLabel<'tcx> {
224 #[label(slug = "typeck-expected-default-return-type")]
229 #[label(slug = "typeck-expected-return-type")]
237 #[derive(SessionDiagnostic)]
238 #[error(slug = "typeck-unconstrained-opaque-type")]
240 pub struct UnconstrainedOpaqueType {
246 #[derive(SessionDiagnostic)]
247 #[error(code = "E0632", slug = "typeck-explicit-generic-args-with-impl-trait")]
249 pub struct ExplicitGenericArgsWithImplTrait {
252 pub spans: Vec<Span>,
254 pub is_nightly_build: Option<()>,
257 pub struct MissingTypeParams {
260 pub missing_type_params: Vec<String>,
261 pub empty_generic_args: bool,
264 // Manual implementation of `SessionDiagnostic` to be able to call `span_to_snippet`.
265 impl<'a> SessionDiagnostic<'a> for MissingTypeParams {
266 fn into_diagnostic(self, sess: &'a ParseSess) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
267 static SLUG: &'static str = "typeck-missing-type-params";
268 let mut err = sess.span_diagnostic.struct_span_err_with_code(
270 DiagnosticMessage::fluent(SLUG),
273 err.set_arg("parameterCount", self.missing_type_params.len());
276 self.missing_type_params
278 .map(|n| format!("`{}`", n))
283 err.span_label(self.def_span, DiagnosticMessage::fluent_attr(SLUG, "label"));
285 let mut suggested = false;
286 if let (Ok(snippet), true) = (
287 sess.source_map().span_to_snippet(self.span),
288 // Don't suggest setting the type params if there are some already: the order is
289 // tricky to get right and the user will already know what the syntax is.
290 self.empty_generic_args,
292 if snippet.ends_with('>') {
293 // The user wrote `Trait<'a, T>` or similar. To provide an accurate suggestion
294 // we would have to preserve the right order. For now, as clearly the user is
295 // aware of the syntax, we do nothing.
297 // The user wrote `Iterator`, so we don't have a type we can suggest, but at
298 // least we can clue them to the correct syntax `Iterator<Type>`.
301 DiagnosticMessage::fluent_attr(SLUG, "suggestion"),
302 format!("{}<{}>", snippet, self.missing_type_params.join(", ")),
303 Applicability::HasPlaceholders,
309 err.span_label(self.span, DiagnosticMessage::fluent_attr(SLUG, "no-suggestion-label"));
312 err.note(DiagnosticMessage::fluent_attr(SLUG, "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 {