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;
6 edition::{Edition, LATEST_STABLE_EDITION},
12 #[diag(hir_typeck_field_multiply_specified_in_initializer, code = "E0062")]
13 pub struct FieldMultiplySpecifiedInInitializer {
17 #[label(previous_use_label)]
23 #[diag(hir_typeck_return_stmt_outside_of_fn_body, code = "E0572")]
24 pub struct ReturnStmtOutsideOfFnBody {
27 #[label(encl_body_label)]
28 pub encl_body_span: Option<Span>,
29 #[label(encl_fn_label)]
30 pub encl_fn_span: Option<Span>,
34 #[diag(hir_typeck_yield_expr_outside_of_generator, code = "E0627")]
35 pub struct YieldExprOutsideOfGenerator {
41 #[diag(hir_typeck_struct_expr_non_exhaustive, code = "E0639")]
42 pub struct StructExprNonExhaustive {
45 pub what: &'static str,
49 #[diag(hir_typeck_method_call_on_unknown_type, code = "E0699")]
50 pub struct MethodCallOnUnknownType {
56 #[diag(hir_typeck_functional_record_update_on_non_struct, code = "E0436")]
57 pub struct FunctionalRecordUpdateOnNonStruct {
63 #[diag(hir_typeck_address_of_temporary_taken, code = "E0745")]
64 pub struct AddressOfTemporaryTaken {
70 #[derive(Subdiagnostic)]
71 pub enum AddReturnTypeSuggestion {
73 hir_typeck_add_return_type_add,
75 applicability = "machine-applicable"
83 hir_typeck_add_return_type_missing_here,
85 applicability = "has-placeholders"
93 #[derive(Subdiagnostic)]
94 pub enum ExpectedReturnTypeLabel<'tcx> {
95 #[label(hir_typeck_expected_default_return_type)]
100 #[label(hir_typeck_expected_return_type)]
108 #[derive(Diagnostic)]
109 #[diag(hir_typeck_missing_parentheses_in_range, code = "E0689")]
110 pub struct MissingParentheseInRange {
112 #[label(hir_typeck_missing_parentheses_in_range)]
115 pub method_name: String,
117 pub add_missing_parentheses: Option<AddMissingParenthesesInRange>,
120 #[derive(Subdiagnostic)]
121 #[multipart_suggestion(
122 hir_typeck_add_missing_parentheses_in_range,
124 applicability = "maybe-incorrect"
126 pub struct AddMissingParenthesesInRange {
127 pub func_name: String,
128 #[suggestion_part(code = "(")]
130 #[suggestion_part(code = ")")]
134 #[derive(Diagnostic)]
135 #[diag(hir_typeck_op_trait_generic_params)]
136 pub struct OpMethodGenericParams {
139 pub method_name: String,
142 pub struct TypeMismatchFruTypo {
143 /// Span of the LHS of the range
145 /// Span of the `..RHS` part of the range
147 /// Rendered expression of the RHS of the range
148 pub expr: Option<String>,
151 impl AddToDiagnostic for TypeMismatchFruTypo {
152 fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
154 F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
156 diag.set_arg("expr", self.expr.as_deref().unwrap_or("NONE"));
158 // Only explain that `a ..b` is a range if it's split up
159 if self.expr_span.between(self.fru_span).is_empty() {
161 self.expr_span.to(self.fru_span),
162 rustc_errors::fluent::hir_typeck_fru_note,
165 let mut multispan: MultiSpan = vec![self.expr_span, self.fru_span].into();
166 multispan.push_span_label(self.expr_span, rustc_errors::fluent::hir_typeck_fru_expr);
167 multispan.push_span_label(self.fru_span, rustc_errors::fluent::hir_typeck_fru_expr2);
168 diag.span_note(multispan, rustc_errors::fluent::hir_typeck_fru_note);
171 diag.span_suggestion(
172 self.expr_span.shrink_to_hi(),
173 rustc_errors::fluent::hir_typeck_fru_suggestion,
175 Applicability::MaybeIncorrect,
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 {
186 pub params_span: Span,
187 pub found_param_count: usize,
190 #[derive(Diagnostic)]
191 #[diag(hir_typeck_lang_start_incorrect_param)]
192 pub struct LangStartIncorrectParam<'tcx> {
194 #[suggestion(style = "short", code = "{expected_ty}", applicability = "machine-applicable")]
195 pub param_span: Span,
197 pub param_num: usize,
198 pub expected_ty: Ty<'tcx>,
199 pub found_ty: Ty<'tcx>,
202 #[derive(Diagnostic)]
203 #[diag(hir_typeck_lang_start_incorrect_ret_ty)]
204 pub struct LangStartIncorrectRetTy<'tcx> {
206 #[suggestion(style = "short", code = "{expected_ty}", applicability = "machine-applicable")]
209 pub expected_ty: Ty<'tcx>,
210 pub found_ty: Ty<'tcx>,
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 },
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 }
229 Self::Standalone { edition }