• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! Errors emitted by `rustc_hir_typeck`.
2 use std::borrow::Cow;
3 
4 use crate::fluent_generated as fluent;
5 use rustc_errors::{
6     AddToDiagnostic, Applicability, Diagnostic, DiagnosticArgValue, IntoDiagnosticArg, MultiSpan,
7     SubdiagnosticMessage,
8 };
9 use rustc_macros::{Diagnostic, Subdiagnostic};
10 use rustc_middle::ty::Ty;
11 use rustc_span::{
12     edition::{Edition, LATEST_STABLE_EDITION},
13     symbol::Ident,
14     Span,
15 };
16 
17 #[derive(Diagnostic)]
18 #[diag(hir_typeck_field_multiply_specified_in_initializer, code = "E0062")]
19 pub struct FieldMultiplySpecifiedInInitializer {
20     #[primary_span]
21     #[label]
22     pub span: Span,
23     #[label(hir_typeck_previous_use_label)]
24     pub prev_span: Span,
25     pub ident: Ident,
26 }
27 
28 #[derive(Diagnostic)]
29 #[diag(hir_typeck_return_stmt_outside_of_fn_body, code = "E0572")]
30 pub struct ReturnStmtOutsideOfFnBody {
31     #[primary_span]
32     pub span: Span,
33     #[label(hir_typeck_encl_body_label)]
34     pub encl_body_span: Option<Span>,
35     #[label(hir_typeck_encl_fn_label)]
36     pub encl_fn_span: Option<Span>,
37     pub statement_kind: ReturnLikeStatementKind,
38 }
39 
40 pub enum ReturnLikeStatementKind {
41     Return,
42     Become,
43 }
44 
45 impl IntoDiagnosticArg for ReturnLikeStatementKind {
into_diagnostic_arg(self) -> DiagnosticArgValue<'static>46     fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
47         let kind = match self {
48             Self::Return => "return",
49             Self::Become => "become",
50         }
51         .into();
52 
53         DiagnosticArgValue::Str(kind)
54     }
55 }
56 
57 #[derive(Diagnostic)]
58 #[diag(hir_typeck_yield_expr_outside_of_generator, code = "E0627")]
59 pub struct YieldExprOutsideOfGenerator {
60     #[primary_span]
61     pub span: Span,
62 }
63 
64 #[derive(Diagnostic)]
65 #[diag(hir_typeck_struct_expr_non_exhaustive, code = "E0639")]
66 pub struct StructExprNonExhaustive {
67     #[primary_span]
68     pub span: Span,
69     pub what: &'static str,
70 }
71 
72 #[derive(Diagnostic)]
73 #[diag(hir_typeck_method_call_on_unknown_raw_pointee, code = "E0699")]
74 pub struct MethodCallOnUnknownRawPointee {
75     #[primary_span]
76     pub span: Span,
77 }
78 
79 #[derive(Diagnostic)]
80 #[diag(hir_typeck_functional_record_update_on_non_struct, code = "E0436")]
81 pub struct FunctionalRecordUpdateOnNonStruct {
82     #[primary_span]
83     pub span: Span,
84 }
85 
86 #[derive(Diagnostic)]
87 #[diag(hir_typeck_address_of_temporary_taken, code = "E0745")]
88 pub struct AddressOfTemporaryTaken {
89     #[primary_span]
90     #[label]
91     pub span: Span,
92 }
93 
94 #[derive(Subdiagnostic)]
95 pub enum AddReturnTypeSuggestion {
96     #[suggestion(
97         hir_typeck_add_return_type_add,
98         code = "-> {found} ",
99         applicability = "machine-applicable"
100     )]
101     Add {
102         #[primary_span]
103         span: Span,
104         found: String,
105     },
106     #[suggestion(
107         hir_typeck_add_return_type_missing_here,
108         code = "-> _ ",
109         applicability = "has-placeholders"
110     )]
111     MissingHere {
112         #[primary_span]
113         span: Span,
114     },
115 }
116 
117 #[derive(Subdiagnostic)]
118 pub enum ExpectedReturnTypeLabel<'tcx> {
119     #[label(hir_typeck_expected_default_return_type)]
120     Unit {
121         #[primary_span]
122         span: Span,
123     },
124     #[label(hir_typeck_expected_return_type)]
125     Other {
126         #[primary_span]
127         span: Span,
128         expected: Ty<'tcx>,
129     },
130 }
131 
132 #[derive(Diagnostic)]
133 #[diag(hir_typeck_missing_parentheses_in_range, code = "E0689")]
134 pub struct MissingParenthesesInRange {
135     #[primary_span]
136     #[label(hir_typeck_missing_parentheses_in_range)]
137     pub span: Span,
138     pub ty_str: String,
139     pub method_name: String,
140     #[subdiagnostic]
141     pub add_missing_parentheses: Option<AddMissingParenthesesInRange>,
142 }
143 
144 #[derive(Subdiagnostic)]
145 #[multipart_suggestion(
146     hir_typeck_add_missing_parentheses_in_range,
147     style = "verbose",
148     applicability = "maybe-incorrect"
149 )]
150 pub struct AddMissingParenthesesInRange {
151     pub func_name: String,
152     #[suggestion_part(code = "(")]
153     pub left: Span,
154     #[suggestion_part(code = ")")]
155     pub right: Span,
156 }
157 
158 #[derive(Diagnostic)]
159 #[diag(hir_typeck_op_trait_generic_params)]
160 pub struct OpMethodGenericParams {
161     #[primary_span]
162     pub span: Span,
163     pub method_name: String,
164 }
165 
166 pub struct TypeMismatchFruTypo {
167     /// Span of the LHS of the range
168     pub expr_span: Span,
169     /// Span of the `..RHS` part of the range
170     pub fru_span: Span,
171     /// Rendered expression of the RHS of the range
172     pub expr: Option<String>,
173 }
174 
175 impl AddToDiagnostic for TypeMismatchFruTypo {
add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F) where F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,176     fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
177     where
178         F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
179     {
180         diag.set_arg("expr", self.expr.as_deref().unwrap_or("NONE"));
181 
182         // Only explain that `a ..b` is a range if it's split up
183         if self.expr_span.between(self.fru_span).is_empty() {
184             diag.span_note(self.expr_span.to(self.fru_span), fluent::hir_typeck_fru_note);
185         } else {
186             let mut multispan: MultiSpan = vec![self.expr_span, self.fru_span].into();
187             multispan.push_span_label(self.expr_span, fluent::hir_typeck_fru_expr);
188             multispan.push_span_label(self.fru_span, fluent::hir_typeck_fru_expr2);
189             diag.span_note(multispan, fluent::hir_typeck_fru_note);
190         }
191 
192         diag.span_suggestion(
193             self.expr_span.shrink_to_hi(),
194             fluent::hir_typeck_fru_suggestion,
195             ", ",
196             Applicability::MaybeIncorrect,
197         );
198     }
199 }
200 
201 #[derive(Diagnostic)]
202 #[diag(hir_typeck_lang_start_incorrect_number_params)]
203 #[note(hir_typeck_lang_start_incorrect_number_params_note_expected_count)]
204 #[note(hir_typeck_lang_start_expected_sig_note)]
205 pub struct LangStartIncorrectNumberArgs {
206     #[primary_span]
207     pub params_span: Span,
208     pub found_param_count: usize,
209 }
210 
211 #[derive(Diagnostic)]
212 #[diag(hir_typeck_lang_start_incorrect_param)]
213 pub struct LangStartIncorrectParam<'tcx> {
214     #[primary_span]
215     #[suggestion(style = "short", code = "{expected_ty}", applicability = "machine-applicable")]
216     pub param_span: Span,
217 
218     pub param_num: usize,
219     pub expected_ty: Ty<'tcx>,
220     pub found_ty: Ty<'tcx>,
221 }
222 
223 #[derive(Diagnostic)]
224 #[diag(hir_typeck_lang_start_incorrect_ret_ty)]
225 pub struct LangStartIncorrectRetTy<'tcx> {
226     #[primary_span]
227     #[suggestion(style = "short", code = "{expected_ty}", applicability = "machine-applicable")]
228     pub ret_span: Span,
229 
230     pub expected_ty: Ty<'tcx>,
231     pub found_ty: Ty<'tcx>,
232 }
233 
234 #[derive(Subdiagnostic)]
235 pub enum HelpUseLatestEdition {
236     #[help(hir_typeck_help_set_edition_cargo)]
237     #[note(hir_typeck_note_edition_guide)]
238     Cargo { edition: Edition },
239     #[help(hir_typeck_help_set_edition_standalone)]
240     #[note(hir_typeck_note_edition_guide)]
241     Standalone { edition: Edition },
242 }
243 
244 impl HelpUseLatestEdition {
new() -> Self245     pub fn new() -> Self {
246         let edition = LATEST_STABLE_EDITION;
247         if std::env::var_os("CARGO").is_some() {
248             Self::Cargo { edition }
249         } else {
250             Self::Standalone { edition }
251         }
252     }
253 }
254 
255 #[derive(Diagnostic)]
256 #[diag(hir_typeck_const_select_must_be_const)]
257 #[help]
258 pub struct ConstSelectMustBeConst {
259     #[primary_span]
260     pub span: Span,
261 }
262 
263 #[derive(Diagnostic)]
264 #[diag(hir_typeck_const_select_must_be_fn)]
265 #[note]
266 #[help]
267 pub struct ConstSelectMustBeFn<'a> {
268     #[primary_span]
269     pub span: Span,
270     pub ty: Ty<'a>,
271 }
272 
273 #[derive(Diagnostic)]
274 #[diag(hir_typeck_union_pat_multiple_fields)]
275 pub struct UnionPatMultipleFields {
276     #[primary_span]
277     pub span: Span,
278 }
279 
280 #[derive(Diagnostic)]
281 #[diag(hir_typeck_union_pat_dotdot)]
282 pub struct UnionPatDotDot {
283     #[primary_span]
284     pub span: Span,
285 }
286 
287 #[derive(Diagnostic)]
288 #[diag(hir_typeck_arg_mismatch_indeterminate)]
289 pub struct ArgMismatchIndeterminate {
290     #[primary_span]
291     pub span: Span,
292 }
293 
294 #[derive(Subdiagnostic)]
295 pub enum SuggestBoxing {
296     #[note(hir_typeck_suggest_boxing_note)]
297     #[multipart_suggestion(
298         hir_typeck_suggest_boxing_when_appropriate,
299         applicability = "machine-applicable"
300     )]
301     Unit {
302         #[suggestion_part(code = "Box::new(())")]
303         start: Span,
304         #[suggestion_part(code = "")]
305         end: Span,
306     },
307     #[note(hir_typeck_suggest_boxing_note)]
308     AsyncBody,
309     #[note(hir_typeck_suggest_boxing_note)]
310     #[multipart_suggestion(
311         hir_typeck_suggest_boxing_when_appropriate,
312         applicability = "machine-applicable"
313     )]
314     Other {
315         #[suggestion_part(code = "Box::new(")]
316         start: Span,
317         #[suggestion_part(code = ")")]
318         end: Span,
319     },
320 }
321 
322 #[derive(Subdiagnostic)]
323 #[suggestion(
324     hir_typeck_suggest_ptr_null_mut,
325     applicability = "maybe-incorrect",
326     code = "core::ptr::null_mut()"
327 )]
328 pub struct SuggestPtrNullMut {
329     #[primary_span]
330     pub span: Span,
331 }
332 
333 #[derive(Diagnostic)]
334 #[diag(hir_typeck_no_associated_item, code = "E0599")]
335 pub struct NoAssociatedItem {
336     #[primary_span]
337     pub span: Span,
338     pub item_kind: &'static str,
339     pub item_name: Ident,
340     pub ty_prefix: Cow<'static, str>,
341     pub ty_str: String,
342     pub trait_missing_method: bool,
343 }
344 
345 #[derive(Subdiagnostic)]
346 #[note(hir_typeck_candidate_trait_note)]
347 pub struct CandidateTraitNote {
348     #[primary_span]
349     pub span: Span,
350     pub trait_name: String,
351     pub item_name: Ident,
352     pub action_or_ty: String,
353 }
354 
355 #[derive(Diagnostic)]
356 #[diag(hir_typeck_ctor_is_private, code = "E0603")]
357 pub struct CtorIsPrivate {
358     #[primary_span]
359     pub span: Span,
360     pub def: String,
361 }
362 
363 #[derive(Subdiagnostic)]
364 #[multipart_suggestion(
365     hir_typeck_convert_using_method,
366     applicability = "machine-applicable",
367     style = "verbose"
368 )]
369 pub struct SuggestConvertViaMethod<'tcx> {
370     #[suggestion_part(code = "{sugg}")]
371     pub span: Span,
372     #[suggestion_part(code = "")]
373     pub borrow_removal_span: Option<Span>,
374     pub sugg: &'static str,
375     pub expected: Ty<'tcx>,
376     pub found: Ty<'tcx>,
377 }
378