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};
8 #[diag(hir_typeck_field_multiply_specified_in_initializer, code = "E0062")]
9 pub struct FieldMultiplySpecifiedInInitializer {
13 #[label(previous_use_label)]
19 #[diag(hir_typeck_return_stmt_outside_of_fn_body, code = "E0572")]
20 pub struct ReturnStmtOutsideOfFnBody {
23 #[label(encl_body_label)]
24 pub encl_body_span: Option<Span>,
25 #[label(encl_fn_label)]
26 pub encl_fn_span: Option<Span>,
30 #[diag(hir_typeck_yield_expr_outside_of_generator, code = "E0627")]
31 pub struct YieldExprOutsideOfGenerator {
37 #[diag(hir_typeck_struct_expr_non_exhaustive, code = "E0639")]
38 pub struct StructExprNonExhaustive {
41 pub what: &'static str,
45 #[diag(hir_typeck_method_call_on_unknown_type, code = "E0699")]
46 pub struct MethodCallOnUnknownType {
52 #[diag(hir_typeck_functional_record_update_on_non_struct, code = "E0436")]
53 pub struct FunctionalRecordUpdateOnNonStruct {
59 #[diag(hir_typeck_address_of_temporary_taken, code = "E0745")]
60 pub struct AddressOfTemporaryTaken {
66 #[derive(Subdiagnostic)]
67 pub enum AddReturnTypeSuggestion {
69 hir_typeck_add_return_type_add,
71 applicability = "machine-applicable"
79 hir_typeck_add_return_type_missing_here,
81 applicability = "has-placeholders"
89 #[derive(Subdiagnostic)]
90 pub enum ExpectedReturnTypeLabel<'tcx> {
91 #[label(hir_typeck_expected_default_return_type)]
96 #[label(hir_typeck_expected_return_type)]
104 #[derive(Diagnostic)]
105 #[diag(hir_typeck_missing_parentheses_in_range, code = "E0689")]
106 pub struct MissingParentheseInRange {
108 #[label(hir_typeck_missing_parentheses_in_range)]
111 pub method_name: String,
113 pub add_missing_parentheses: Option<AddMissingParenthesesInRange>,
116 #[derive(Subdiagnostic)]
117 #[multipart_suggestion(
118 hir_typeck_add_missing_parentheses_in_range,
120 applicability = "maybe-incorrect"
122 pub struct AddMissingParenthesesInRange {
123 pub func_name: String,
124 #[suggestion_part(code = "(")]
126 #[suggestion_part(code = ")")]
130 #[derive(Diagnostic)]
131 #[diag(hir_typeck_op_trait_generic_params)]
132 pub struct OpMethodGenericParams {
135 pub method_name: String,
138 pub struct TypeMismatchFruTypo {
139 /// Span of the LHS of the range
141 /// Span of the `..RHS` part of the range
143 /// Rendered expression of the RHS of the range
144 pub expr: Option<String>,
147 impl AddToDiagnostic for TypeMismatchFruTypo {
148 fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
150 F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
152 diag.set_arg("expr", self.expr.as_deref().unwrap_or("NONE"));
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() {
157 self.expr_span.to(self.fru_span),
158 rustc_errors::fluent::hir_typeck_fru_note,
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);
167 diag.span_suggestion(
168 self.expr_span.shrink_to_hi(),
169 rustc_errors::fluent::hir_typeck_fru_suggestion,
171 Applicability::MaybeIncorrect,