]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_hir_typeck/src/errors.rs
Rollup merge of #107151 - tmiasko:dominators-no-inline, r=compiler-errors
[rust.git] / compiler / rustc_hir_typeck / src / errors.rs
1 //! Errors emitted by `rustc_hir_typeck`.
2 use rustc_errors::{AddToDiagnostic, Applicability, Diagnostic, MultiSpan, SubdiagnosticMessage};
3 use rustc_macros::{Diagnostic, Subdiagnostic};
4 use rustc_middle::ty::Ty;
5 use rustc_span::{symbol::Ident, Span};
6
7 #[derive(Diagnostic)]
8 #[diag(hir_typeck_field_multiply_specified_in_initializer, code = "E0062")]
9 pub struct FieldMultiplySpecifiedInInitializer {
10     #[primary_span]
11     #[label]
12     pub span: Span,
13     #[label(previous_use_label)]
14     pub prev_span: Span,
15     pub ident: Ident,
16 }
17
18 #[derive(Diagnostic)]
19 #[diag(hir_typeck_return_stmt_outside_of_fn_body, code = "E0572")]
20 pub struct ReturnStmtOutsideOfFnBody {
21     #[primary_span]
22     pub span: Span,
23     #[label(encl_body_label)]
24     pub encl_body_span: Option<Span>,
25     #[label(encl_fn_label)]
26     pub encl_fn_span: Option<Span>,
27 }
28
29 #[derive(Diagnostic)]
30 #[diag(hir_typeck_yield_expr_outside_of_generator, code = "E0627")]
31 pub struct YieldExprOutsideOfGenerator {
32     #[primary_span]
33     pub span: Span,
34 }
35
36 #[derive(Diagnostic)]
37 #[diag(hir_typeck_struct_expr_non_exhaustive, code = "E0639")]
38 pub struct StructExprNonExhaustive {
39     #[primary_span]
40     pub span: Span,
41     pub what: &'static str,
42 }
43
44 #[derive(Diagnostic)]
45 #[diag(hir_typeck_method_call_on_unknown_type, code = "E0699")]
46 pub struct MethodCallOnUnknownType {
47     #[primary_span]
48     pub span: Span,
49 }
50
51 #[derive(Diagnostic)]
52 #[diag(hir_typeck_functional_record_update_on_non_struct, code = "E0436")]
53 pub struct FunctionalRecordUpdateOnNonStruct {
54     #[primary_span]
55     pub span: Span,
56 }
57
58 #[derive(Diagnostic)]
59 #[diag(hir_typeck_address_of_temporary_taken, code = "E0745")]
60 pub struct AddressOfTemporaryTaken {
61     #[primary_span]
62     #[label]
63     pub span: Span,
64 }
65
66 #[derive(Subdiagnostic)]
67 pub enum AddReturnTypeSuggestion {
68     #[suggestion(
69         hir_typeck_add_return_type_add,
70         code = "-> {found} ",
71         applicability = "machine-applicable"
72     )]
73     Add {
74         #[primary_span]
75         span: Span,
76         found: String,
77     },
78     #[suggestion(
79         hir_typeck_add_return_type_missing_here,
80         code = "-> _ ",
81         applicability = "has-placeholders"
82     )]
83     MissingHere {
84         #[primary_span]
85         span: Span,
86     },
87 }
88
89 #[derive(Subdiagnostic)]
90 pub enum ExpectedReturnTypeLabel<'tcx> {
91     #[label(hir_typeck_expected_default_return_type)]
92     Unit {
93         #[primary_span]
94         span: Span,
95     },
96     #[label(hir_typeck_expected_return_type)]
97     Other {
98         #[primary_span]
99         span: Span,
100         expected: Ty<'tcx>,
101     },
102 }
103
104 #[derive(Diagnostic)]
105 #[diag(hir_typeck_missing_parentheses_in_range, code = "E0689")]
106 pub struct MissingParentheseInRange {
107     #[primary_span]
108     #[label(hir_typeck_missing_parentheses_in_range)]
109     pub span: Span,
110     pub ty_str: String,
111     pub method_name: String,
112     #[subdiagnostic]
113     pub add_missing_parentheses: Option<AddMissingParenthesesInRange>,
114 }
115
116 #[derive(Subdiagnostic)]
117 #[multipart_suggestion(
118     hir_typeck_add_missing_parentheses_in_range,
119     style = "verbose",
120     applicability = "maybe-incorrect"
121 )]
122 pub struct AddMissingParenthesesInRange {
123     pub func_name: String,
124     #[suggestion_part(code = "(")]
125     pub left: Span,
126     #[suggestion_part(code = ")")]
127     pub right: Span,
128 }
129
130 #[derive(Diagnostic)]
131 #[diag(hir_typeck_op_trait_generic_params)]
132 pub struct OpMethodGenericParams {
133     #[primary_span]
134     pub span: Span,
135     pub method_name: String,
136 }
137
138 pub struct TypeMismatchFruTypo {
139     /// Span of the LHS of the range
140     pub expr_span: Span,
141     /// Span of the `..RHS` part of the range
142     pub fru_span: Span,
143     /// Rendered expression of the RHS of the range
144     pub expr: Option<String>,
145 }
146
147 impl AddToDiagnostic for TypeMismatchFruTypo {
148     fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
149     where
150         F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
151     {
152         diag.set_arg("expr", self.expr.as_deref().unwrap_or("NONE"));
153
154         // Only explain that `a ..b` is a range if it's split up
155         if self.expr_span.between(self.fru_span).is_empty() {
156             diag.span_note(
157                 self.expr_span.to(self.fru_span),
158                 rustc_errors::fluent::hir_typeck_fru_note,
159             );
160         } else {
161             let mut multispan: MultiSpan = vec![self.expr_span, self.fru_span].into();
162             multispan.push_span_label(self.expr_span, rustc_errors::fluent::hir_typeck_fru_expr);
163             multispan.push_span_label(self.fru_span, rustc_errors::fluent::hir_typeck_fru_expr2);
164             diag.span_note(multispan, rustc_errors::fluent::hir_typeck_fru_note);
165         }
166
167         diag.span_suggestion(
168             self.expr_span.shrink_to_hi(),
169             rustc_errors::fluent::hir_typeck_fru_suggestion,
170             ", ",
171             Applicability::MaybeIncorrect,
172         );
173     }
174 }
175
176 #[derive(Diagnostic)]
177 #[diag(hir_typeck_lang_start_incorrect_number_params)]
178 #[note(hir_typeck_lang_start_incorrect_number_params_note_expected_count)]
179 #[note(hir_typeck_lang_start_expected_sig_note)]
180 pub struct LangStartIncorrectNumberArgs {
181     #[primary_span]
182     pub params_span: Span,
183     pub found_param_count: usize,
184 }
185
186 #[derive(Diagnostic)]
187 #[diag(hir_typeck_lang_start_incorrect_param)]
188 pub struct LangStartIncorrectParam<'tcx> {
189     #[primary_span]
190     #[suggestion(style = "short", code = "{expected_ty}", applicability = "machine-applicable")]
191     pub param_span: Span,
192
193     pub param_num: usize,
194     pub expected_ty: Ty<'tcx>,
195     pub found_ty: Ty<'tcx>,
196 }
197
198 #[derive(Diagnostic)]
199 #[diag(hir_typeck_lang_start_incorrect_ret_ty)]
200 pub struct LangStartIncorrectRetTy<'tcx> {
201     #[primary_span]
202     #[suggestion(style = "short", code = "{expected_ty}", applicability = "machine-applicable")]
203     pub ret_span: Span,
204
205     pub expected_ty: Ty<'tcx>,
206     pub found_ty: Ty<'tcx>,
207 }