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