rustc_hir_typeck/
errors.rs

1//! Errors emitted by `rustc_hir_typeck`.
2
3use std::borrow::Cow;
4
5use rustc_errors::codes::*;
6use rustc_errors::{
7    Applicability, Diag, DiagArgValue, DiagSymbolList, EmissionGuarantee, IntoDiagArg, MultiSpan,
8    SubdiagMessageOp, Subdiagnostic,
9};
10use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
11use rustc_middle::ty::{self, Ty};
12use rustc_span::edition::{Edition, LATEST_STABLE_EDITION};
13use rustc_span::{Ident, Span, Symbol};
14
15use crate::fluent_generated as fluent;
16
17#[derive(Diagnostic)]
18#[diag(hir_typeck_base_expression_double_dot, code = E0797)]
19pub(crate) struct BaseExpressionDoubleDot {
20    #[primary_span]
21    pub span: Span,
22    #[suggestion(
23        hir_typeck_base_expression_double_dot_enable_default_field_values,
24        code = "#![feature(default_field_values)]\n",
25        applicability = "machine-applicable",
26        style = "verbose"
27    )]
28    pub default_field_values_suggestion: Option<Span>,
29    #[subdiagnostic]
30    pub default_field_values_help: Option<BaseExpressionDoubleDotEnableDefaultFieldValues>,
31    #[subdiagnostic]
32    pub add_expr: Option<BaseExpressionDoubleDotAddExpr>,
33    #[subdiagnostic]
34    pub remove_dots: Option<BaseExpressionDoubleDotRemove>,
35}
36
37#[derive(Subdiagnostic)]
38#[suggestion(
39    hir_typeck_base_expression_double_dot_remove,
40    code = "",
41    applicability = "machine-applicable",
42    style = "verbose"
43)]
44pub(crate) struct BaseExpressionDoubleDotRemove {
45    #[primary_span]
46    pub span: Span,
47}
48
49#[derive(Subdiagnostic)]
50#[suggestion(
51    hir_typeck_base_expression_double_dot_add_expr,
52    code = "/* expr */",
53    applicability = "has-placeholders",
54    style = "verbose"
55)]
56pub(crate) struct BaseExpressionDoubleDotAddExpr {
57    #[primary_span]
58    pub span: Span,
59}
60
61#[derive(Subdiagnostic)]
62#[help(hir_typeck_base_expression_double_dot_enable_default_field_values)]
63pub(crate) struct BaseExpressionDoubleDotEnableDefaultFieldValues;
64
65#[derive(Diagnostic)]
66#[diag(hir_typeck_field_multiply_specified_in_initializer, code = E0062)]
67pub(crate) struct FieldMultiplySpecifiedInInitializer {
68    #[primary_span]
69    #[label]
70    pub span: Span,
71    #[label(hir_typeck_previous_use_label)]
72    pub prev_span: Span,
73    pub ident: Ident,
74}
75
76#[derive(Diagnostic)]
77#[diag(hir_typeck_return_stmt_outside_of_fn_body, code = E0572)]
78pub(crate) struct ReturnStmtOutsideOfFnBody {
79    #[primary_span]
80    pub span: Span,
81    #[label(hir_typeck_encl_body_label)]
82    pub encl_body_span: Option<Span>,
83    #[label(hir_typeck_encl_fn_label)]
84    pub encl_fn_span: Option<Span>,
85    pub statement_kind: ReturnLikeStatementKind,
86}
87
88pub(crate) enum ReturnLikeStatementKind {
89    Return,
90    Become,
91}
92
93impl IntoDiagArg for ReturnLikeStatementKind {
94    fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
95        let kind = match self {
96            Self::Return => "return",
97            Self::Become => "become",
98        }
99        .into();
100
101        DiagArgValue::Str(kind)
102    }
103}
104
105#[derive(Diagnostic)]
106#[diag(hir_typeck_rustcall_incorrect_args)]
107pub(crate) struct RustCallIncorrectArgs {
108    #[primary_span]
109    pub span: Span,
110}
111
112#[derive(Diagnostic)]
113#[diag(hir_typeck_yield_expr_outside_of_coroutine, code = E0627)]
114pub(crate) struct YieldExprOutsideOfCoroutine {
115    #[primary_span]
116    pub span: Span,
117}
118
119#[derive(Diagnostic)]
120#[diag(hir_typeck_struct_expr_non_exhaustive, code = E0639)]
121pub(crate) struct StructExprNonExhaustive {
122    #[primary_span]
123    pub span: Span,
124    pub what: &'static str,
125}
126
127#[derive(Diagnostic)]
128#[diag(hir_typeck_functional_record_update_on_non_struct, code = E0436)]
129pub(crate) struct FunctionalRecordUpdateOnNonStruct {
130    #[primary_span]
131    pub span: Span,
132}
133
134#[derive(Diagnostic)]
135#[diag(hir_typeck_address_of_temporary_taken, code = E0745)]
136pub(crate) struct AddressOfTemporaryTaken {
137    #[primary_span]
138    #[label]
139    pub span: Span,
140}
141
142#[derive(Subdiagnostic)]
143pub(crate) enum AddReturnTypeSuggestion {
144    #[suggestion(
145        hir_typeck_add_return_type_add,
146        code = " -> {found}",
147        applicability = "machine-applicable"
148    )]
149    Add {
150        #[primary_span]
151        span: Span,
152        found: String,
153    },
154    #[suggestion(
155        hir_typeck_add_return_type_missing_here,
156        code = " -> _",
157        applicability = "has-placeholders"
158    )]
159    MissingHere {
160        #[primary_span]
161        span: Span,
162    },
163}
164
165#[derive(Subdiagnostic)]
166pub(crate) enum ExpectedReturnTypeLabel<'tcx> {
167    #[label(hir_typeck_expected_default_return_type)]
168    Unit {
169        #[primary_span]
170        span: Span,
171    },
172    #[label(hir_typeck_expected_return_type)]
173    Other {
174        #[primary_span]
175        span: Span,
176        expected: Ty<'tcx>,
177    },
178}
179
180#[derive(Diagnostic)]
181#[diag(hir_typeck_explicit_destructor, code = E0040)]
182pub(crate) struct ExplicitDestructorCall {
183    #[primary_span]
184    #[label]
185    pub span: Span,
186    #[subdiagnostic]
187    pub sugg: ExplicitDestructorCallSugg,
188}
189
190#[derive(Subdiagnostic)]
191pub(crate) enum ExplicitDestructorCallSugg {
192    #[suggestion(hir_typeck_suggestion, code = "drop", applicability = "maybe-incorrect")]
193    Empty(#[primary_span] Span),
194    #[multipart_suggestion(hir_typeck_suggestion, style = "short")]
195    Snippet {
196        #[suggestion_part(code = "drop(")]
197        lo: Span,
198        #[suggestion_part(code = ")")]
199        hi: Span,
200    },
201}
202
203#[derive(Diagnostic)]
204#[diag(hir_typeck_missing_parentheses_in_range, code = E0689)]
205pub(crate) struct MissingParenthesesInRange {
206    #[primary_span]
207    #[label(hir_typeck_missing_parentheses_in_range)]
208    pub span: Span,
209    pub ty_str: String,
210    pub method_name: String,
211    #[subdiagnostic]
212    pub add_missing_parentheses: Option<AddMissingParenthesesInRange>,
213}
214
215#[derive(LintDiagnostic)]
216pub(crate) enum NeverTypeFallbackFlowingIntoUnsafe {
217    #[help]
218    #[diag(hir_typeck_never_type_fallback_flowing_into_unsafe_call)]
219    Call {
220        #[subdiagnostic]
221        sugg: SuggestAnnotations,
222    },
223    #[help]
224    #[diag(hir_typeck_never_type_fallback_flowing_into_unsafe_method)]
225    Method {
226        #[subdiagnostic]
227        sugg: SuggestAnnotations,
228    },
229    #[help]
230    #[diag(hir_typeck_never_type_fallback_flowing_into_unsafe_path)]
231    Path {
232        #[subdiagnostic]
233        sugg: SuggestAnnotations,
234    },
235    #[help]
236    #[diag(hir_typeck_never_type_fallback_flowing_into_unsafe_union_field)]
237    UnionField {
238        #[subdiagnostic]
239        sugg: SuggestAnnotations,
240    },
241    #[help]
242    #[diag(hir_typeck_never_type_fallback_flowing_into_unsafe_deref)]
243    Deref {
244        #[subdiagnostic]
245        sugg: SuggestAnnotations,
246    },
247}
248
249#[derive(LintDiagnostic)]
250#[help]
251#[diag(hir_typeck_dependency_on_unit_never_type_fallback)]
252pub(crate) struct DependencyOnUnitNeverTypeFallback<'tcx> {
253    #[note]
254    pub obligation_span: Span,
255    pub obligation: ty::Predicate<'tcx>,
256    #[subdiagnostic]
257    pub sugg: SuggestAnnotations,
258}
259
260#[derive(Clone)]
261pub(crate) enum SuggestAnnotation {
262    Unit(Span),
263    Path(Span),
264    Local(Span),
265    Turbo(Span, usize, usize),
266}
267
268#[derive(Clone)]
269pub(crate) struct SuggestAnnotations {
270    pub suggestions: Vec<SuggestAnnotation>,
271}
272impl Subdiagnostic for SuggestAnnotations {
273    fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
274        self,
275        diag: &mut Diag<'_, G>,
276        _: &F,
277    ) {
278        if self.suggestions.is_empty() {
279            return;
280        }
281
282        let mut suggestions = vec![];
283        for suggestion in self.suggestions {
284            match suggestion {
285                SuggestAnnotation::Unit(span) => {
286                    suggestions.push((span, "()".to_string()));
287                }
288                SuggestAnnotation::Path(span) => {
289                    suggestions.push((span.shrink_to_lo(), "<() as ".to_string()));
290                    suggestions.push((span.shrink_to_hi(), ">".to_string()));
291                }
292                SuggestAnnotation::Local(span) => {
293                    suggestions.push((span, ": ()".to_string()));
294                }
295                SuggestAnnotation::Turbo(span, n_args, idx) => suggestions.push((
296                    span,
297                    format!(
298                        "::<{}>",
299                        (0..n_args)
300                            .map(|i| if i == idx { "()" } else { "_" })
301                            .collect::<Vec<_>>()
302                            .join(", "),
303                    ),
304                )),
305            }
306        }
307
308        diag.multipart_suggestion_verbose(
309            "use `()` annotations to avoid fallback changes",
310            suggestions,
311            Applicability::MachineApplicable,
312        );
313    }
314}
315
316#[derive(Subdiagnostic)]
317#[multipart_suggestion(
318    hir_typeck_add_missing_parentheses_in_range,
319    style = "verbose",
320    applicability = "maybe-incorrect"
321)]
322pub(crate) struct AddMissingParenthesesInRange {
323    pub func_name: String,
324    #[suggestion_part(code = "(")]
325    pub left: Span,
326    #[suggestion_part(code = ")")]
327    pub right: Span,
328}
329
330pub(crate) struct TypeMismatchFruTypo {
331    /// Span of the LHS of the range
332    pub expr_span: Span,
333    /// Span of the `..RHS` part of the range
334    pub fru_span: Span,
335    /// Rendered expression of the RHS of the range
336    pub expr: Option<String>,
337}
338
339impl Subdiagnostic for TypeMismatchFruTypo {
340    fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
341        self,
342        diag: &mut Diag<'_, G>,
343        _f: &F,
344    ) {
345        diag.arg("expr", self.expr.as_deref().unwrap_or("NONE"));
346
347        // Only explain that `a ..b` is a range if it's split up
348        if self.expr_span.between(self.fru_span).is_empty() {
349            diag.span_note(self.expr_span.to(self.fru_span), fluent::hir_typeck_fru_note);
350        } else {
351            let mut multispan: MultiSpan = vec![self.expr_span, self.fru_span].into();
352            multispan.push_span_label(self.expr_span, fluent::hir_typeck_fru_expr);
353            multispan.push_span_label(self.fru_span, fluent::hir_typeck_fru_expr2);
354            diag.span_note(multispan, fluent::hir_typeck_fru_note);
355        }
356
357        diag.span_suggestion(
358            self.expr_span.shrink_to_hi(),
359            fluent::hir_typeck_fru_suggestion,
360            ", ",
361            Applicability::MaybeIncorrect,
362        );
363    }
364}
365
366#[derive(LintDiagnostic)]
367#[diag(hir_typeck_lossy_provenance_int2ptr)]
368#[help]
369pub(crate) struct LossyProvenanceInt2Ptr<'tcx> {
370    pub expr_ty: Ty<'tcx>,
371    pub cast_ty: Ty<'tcx>,
372    #[subdiagnostic]
373    pub sugg: LossyProvenanceInt2PtrSuggestion,
374}
375
376#[derive(Diagnostic)]
377#[diag(hir_typeck_ptr_cast_add_auto_to_object, code = E0804)]
378#[note]
379#[help]
380pub(crate) struct PtrCastAddAutoToObject {
381    #[primary_span]
382    #[label]
383    pub span: Span,
384    pub traits_len: usize,
385    pub traits: DiagSymbolList<String>,
386}
387
388#[derive(Subdiagnostic)]
389#[multipart_suggestion(hir_typeck_suggestion, applicability = "has-placeholders")]
390pub(crate) struct LossyProvenanceInt2PtrSuggestion {
391    #[suggestion_part(code = "(...).with_addr(")]
392    pub lo: Span,
393    #[suggestion_part(code = ")")]
394    pub hi: Span,
395}
396
397#[derive(LintDiagnostic)]
398#[diag(hir_typeck_lossy_provenance_ptr2int)]
399#[help]
400pub(crate) struct LossyProvenancePtr2Int<'tcx> {
401    pub expr_ty: Ty<'tcx>,
402    pub cast_ty: Ty<'tcx>,
403    #[subdiagnostic]
404    pub sugg: LossyProvenancePtr2IntSuggestion<'tcx>,
405}
406
407#[derive(Subdiagnostic)]
408pub(crate) enum LossyProvenancePtr2IntSuggestion<'tcx> {
409    #[multipart_suggestion(hir_typeck_suggestion, applicability = "maybe-incorrect")]
410    NeedsParensCast {
411        #[suggestion_part(code = "(")]
412        expr_span: Span,
413        #[suggestion_part(code = ").addr() as {cast_ty}")]
414        cast_span: Span,
415        cast_ty: Ty<'tcx>,
416    },
417    #[multipart_suggestion(hir_typeck_suggestion, applicability = "maybe-incorrect")]
418    NeedsParens {
419        #[suggestion_part(code = "(")]
420        expr_span: Span,
421        #[suggestion_part(code = ").addr()")]
422        cast_span: Span,
423    },
424    #[suggestion(
425        hir_typeck_suggestion,
426        code = ".addr() as {cast_ty}",
427        applicability = "maybe-incorrect"
428    )]
429    NeedsCast {
430        #[primary_span]
431        cast_span: Span,
432        cast_ty: Ty<'tcx>,
433    },
434    #[suggestion(hir_typeck_suggestion, code = ".addr()", applicability = "maybe-incorrect")]
435    Other {
436        #[primary_span]
437        cast_span: Span,
438    },
439}
440
441#[derive(Subdiagnostic)]
442pub(crate) enum HelpUseLatestEdition {
443    #[help(hir_typeck_help_set_edition_cargo)]
444    #[note(hir_typeck_note_edition_guide)]
445    Cargo { edition: Edition },
446    #[help(hir_typeck_help_set_edition_standalone)]
447    #[note(hir_typeck_note_edition_guide)]
448    Standalone { edition: Edition },
449}
450
451impl HelpUseLatestEdition {
452    pub(crate) fn new() -> Self {
453        let edition = LATEST_STABLE_EDITION;
454        if rustc_session::utils::was_invoked_from_cargo() {
455            Self::Cargo { edition }
456        } else {
457            Self::Standalone { edition }
458        }
459    }
460}
461
462#[derive(Diagnostic)]
463#[diag(hir_typeck_no_field_on_type, code = E0609)]
464pub(crate) struct NoFieldOnType<'tcx> {
465    #[primary_span]
466    pub(crate) span: Span,
467    pub(crate) ty: Ty<'tcx>,
468    pub(crate) field: Ident,
469}
470
471#[derive(Diagnostic)]
472#[diag(hir_typeck_no_field_on_variant, code = E0609)]
473pub(crate) struct NoFieldOnVariant<'tcx> {
474    #[primary_span]
475    pub(crate) span: Span,
476    pub(crate) container: Ty<'tcx>,
477    pub(crate) ident: Ident,
478    pub(crate) field: Ident,
479    #[label(hir_typeck_no_field_on_variant_enum)]
480    pub(crate) enum_span: Span,
481    #[label(hir_typeck_no_field_on_variant_field)]
482    pub(crate) field_span: Span,
483}
484
485#[derive(Diagnostic)]
486#[diag(hir_typeck_cant_dereference, code = E0614)]
487pub(crate) struct CantDereference<'tcx> {
488    #[primary_span]
489    #[label(hir_typeck_cant_dereference_label)]
490    pub(crate) span: Span,
491    pub(crate) ty: Ty<'tcx>,
492}
493
494#[derive(Diagnostic)]
495#[diag(hir_typeck_expected_array_or_slice, code = E0529)]
496pub(crate) struct ExpectedArrayOrSlice<'tcx> {
497    #[primary_span]
498    #[label(hir_typeck_expected_array_or_slice_label)]
499    pub(crate) span: Span,
500    pub(crate) ty: Ty<'tcx>,
501    pub(crate) slice_pat_semantics: bool,
502    #[subdiagnostic]
503    pub(crate) as_deref: Option<AsDerefSuggestion>,
504    #[subdiagnostic]
505    pub(crate) slicing: Option<SlicingSuggestion>,
506}
507
508#[derive(Subdiagnostic)]
509#[suggestion(
510    hir_typeck_as_deref_suggestion,
511    code = ".as_deref()",
512    style = "verbose",
513    applicability = "maybe-incorrect"
514)]
515pub(crate) struct AsDerefSuggestion {
516    #[primary_span]
517    pub(crate) span: Span,
518}
519
520#[derive(Subdiagnostic)]
521#[suggestion(
522    hir_typeck_slicing_suggestion,
523    code = "[..]",
524    style = "verbose",
525    applicability = "maybe-incorrect"
526)]
527pub(crate) struct SlicingSuggestion {
528    #[primary_span]
529    pub(crate) span: Span,
530}
531
532#[derive(Diagnostic)]
533#[diag(hir_typeck_invalid_callee, code = E0618)]
534pub(crate) struct InvalidCallee<'tcx> {
535    #[primary_span]
536    pub span: Span,
537    pub ty: Ty<'tcx>,
538    pub found: String,
539}
540
541#[derive(Diagnostic)]
542#[diag(hir_typeck_int_to_fat, code = E0606)]
543pub(crate) struct IntToWide<'tcx> {
544    #[primary_span]
545    #[label(hir_typeck_int_to_fat_label)]
546    pub span: Span,
547    pub metadata: &'tcx str,
548    pub expr_ty: Ty<'tcx>,
549    pub cast_ty: Ty<'tcx>,
550    #[label(hir_typeck_int_to_fat_label_nightly)]
551    pub expr_if_nightly: Option<Span>,
552    pub known_wide: bool,
553}
554
555#[derive(Subdiagnostic)]
556pub(crate) enum OptionResultRefMismatch {
557    #[suggestion(
558        hir_typeck_option_result_copied,
559        code = ".copied()",
560        style = "verbose",
561        applicability = "machine-applicable"
562    )]
563    Copied {
564        #[primary_span]
565        span: Span,
566        def_path: String,
567    },
568    #[suggestion(
569        hir_typeck_option_result_cloned,
570        code = ".cloned()",
571        style = "verbose",
572        applicability = "machine-applicable"
573    )]
574    Cloned {
575        #[primary_span]
576        span: Span,
577        def_path: String,
578    },
579    // FIXME: #114050
580    // #[suggestion(
581    //     hir_typeck_option_result_asref,
582    //     code = ".as_ref()",
583    //     style = "verbose",
584    //     applicability = "machine-applicable"
585    // )]
586    // AsRef {
587    //     #[primary_span]
588    //     span: Span,
589    //     def_path: String,
590    //     expected_ty: Ty<'tcx>,
591    //     expr_ty: Ty<'tcx>,
592    // },
593}
594
595pub(crate) struct RemoveSemiForCoerce {
596    pub expr: Span,
597    pub ret: Span,
598    pub semi: Span,
599}
600
601impl Subdiagnostic for RemoveSemiForCoerce {
602    fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
603        self,
604        diag: &mut Diag<'_, G>,
605        _f: &F,
606    ) {
607        let mut multispan: MultiSpan = self.semi.into();
608        multispan.push_span_label(self.expr, fluent::hir_typeck_remove_semi_for_coerce_expr);
609        multispan.push_span_label(self.ret, fluent::hir_typeck_remove_semi_for_coerce_ret);
610        multispan.push_span_label(self.semi, fluent::hir_typeck_remove_semi_for_coerce_semi);
611        diag.span_note(multispan, fluent::hir_typeck_remove_semi_for_coerce);
612
613        diag.tool_only_span_suggestion(
614            self.semi,
615            fluent::hir_typeck_remove_semi_for_coerce_suggestion,
616            "",
617            Applicability::MaybeIncorrect,
618        );
619    }
620}
621
622#[derive(Diagnostic)]
623#[diag(hir_typeck_const_select_must_be_const)]
624#[help]
625pub(crate) struct ConstSelectMustBeConst {
626    #[primary_span]
627    pub span: Span,
628}
629
630#[derive(Diagnostic)]
631#[diag(hir_typeck_const_select_must_be_fn)]
632#[note]
633#[help]
634pub(crate) struct ConstSelectMustBeFn<'a> {
635    #[primary_span]
636    pub span: Span,
637    pub ty: Ty<'a>,
638}
639
640#[derive(Diagnostic)]
641#[diag(hir_typeck_union_pat_multiple_fields)]
642pub(crate) struct UnionPatMultipleFields {
643    #[primary_span]
644    pub span: Span,
645}
646
647#[derive(Diagnostic)]
648#[diag(hir_typeck_union_pat_dotdot)]
649pub(crate) struct UnionPatDotDot {
650    #[primary_span]
651    pub span: Span,
652}
653
654#[derive(Subdiagnostic)]
655#[multipart_suggestion(
656    hir_typeck_use_is_empty,
657    applicability = "maybe-incorrect",
658    style = "verbose"
659)]
660pub(crate) struct UseIsEmpty<'tcx> {
661    #[suggestion_part(code = "!")]
662    pub lo: Span,
663    #[suggestion_part(code = ".is_empty()")]
664    pub hi: Span,
665    pub expr_ty: Ty<'tcx>,
666}
667
668#[derive(Diagnostic)]
669#[diag(hir_typeck_arg_mismatch_indeterminate)]
670pub(crate) struct ArgMismatchIndeterminate {
671    #[primary_span]
672    pub span: Span,
673}
674
675#[derive(Subdiagnostic)]
676pub(crate) enum SuggestBoxing {
677    #[note(hir_typeck_suggest_boxing_note)]
678    #[multipart_suggestion(
679        hir_typeck_suggest_boxing_when_appropriate,
680        applicability = "machine-applicable"
681    )]
682    Unit {
683        #[suggestion_part(code = "Box::new(())")]
684        start: Span,
685        #[suggestion_part(code = "")]
686        end: Span,
687    },
688    #[note(hir_typeck_suggest_boxing_note)]
689    AsyncBody,
690    #[note(hir_typeck_suggest_boxing_note)]
691    #[multipart_suggestion(
692        hir_typeck_suggest_boxing_when_appropriate,
693        applicability = "machine-applicable"
694    )]
695    Other {
696        #[suggestion_part(code = "Box::new(")]
697        start: Span,
698        #[suggestion_part(code = ")")]
699        end: Span,
700    },
701}
702
703#[derive(Subdiagnostic)]
704#[suggestion(
705    hir_typeck_suggest_ptr_null_mut,
706    applicability = "maybe-incorrect",
707    style = "verbose",
708    code = "core::ptr::null_mut()"
709)]
710pub(crate) struct SuggestPtrNullMut {
711    #[primary_span]
712    pub span: Span,
713}
714
715#[derive(LintDiagnostic)]
716#[diag(hir_typeck_trivial_cast)]
717#[help]
718pub(crate) struct TrivialCast<'tcx> {
719    pub numeric: bool,
720    pub expr_ty: Ty<'tcx>,
721    pub cast_ty: Ty<'tcx>,
722}
723
724#[derive(Diagnostic)]
725#[diag(hir_typeck_no_associated_item, code = E0599)]
726pub(crate) struct NoAssociatedItem {
727    #[primary_span]
728    pub span: Span,
729    pub item_kind: &'static str,
730    pub item_name: Ident,
731    pub ty_prefix: Cow<'static, str>,
732    pub ty_str: String,
733    pub trait_missing_method: bool,
734}
735
736#[derive(Subdiagnostic)]
737#[note(hir_typeck_candidate_trait_note)]
738pub(crate) struct CandidateTraitNote {
739    #[primary_span]
740    pub span: Span,
741    pub trait_name: String,
742    pub item_name: Ident,
743    pub action_or_ty: String,
744}
745
746#[derive(Diagnostic)]
747#[diag(hir_typeck_cannot_cast_to_bool, code = E0054)]
748pub(crate) struct CannotCastToBool<'tcx> {
749    #[primary_span]
750    pub span: Span,
751    pub expr_ty: Ty<'tcx>,
752    #[subdiagnostic]
753    pub help: CannotCastToBoolHelp,
754}
755
756#[derive(Diagnostic)]
757#[diag(hir_typeck_cast_enum_drop)]
758pub(crate) struct CastEnumDrop<'tcx> {
759    #[primary_span]
760    pub span: Span,
761    pub expr_ty: Ty<'tcx>,
762    pub cast_ty: Ty<'tcx>,
763}
764
765#[derive(Diagnostic)]
766#[diag(hir_typeck_cast_unknown_pointer, code = E0641)]
767pub(crate) struct CastUnknownPointer {
768    #[primary_span]
769    pub span: Span,
770    pub to: bool,
771    #[subdiagnostic]
772    pub sub: CastUnknownPointerSub,
773}
774
775pub(crate) enum CastUnknownPointerSub {
776    To(Span),
777    From(Span),
778}
779
780impl rustc_errors::Subdiagnostic for CastUnknownPointerSub {
781    fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
782        self,
783        diag: &mut Diag<'_, G>,
784        f: &F,
785    ) {
786        match self {
787            CastUnknownPointerSub::To(span) => {
788                let msg = f(diag, crate::fluent_generated::hir_typeck_label_to);
789                diag.span_label(span, msg);
790                let msg = f(diag, crate::fluent_generated::hir_typeck_note);
791                diag.note(msg);
792            }
793            CastUnknownPointerSub::From(span) => {
794                let msg = f(diag, crate::fluent_generated::hir_typeck_label_from);
795                diag.span_label(span, msg);
796            }
797        }
798    }
799}
800
801#[derive(Subdiagnostic)]
802pub(crate) enum CannotCastToBoolHelp {
803    #[suggestion(
804        hir_typeck_suggestion,
805        applicability = "machine-applicable",
806        code = " != 0",
807        style = "verbose"
808    )]
809    Numeric(#[primary_span] Span),
810    #[label(hir_typeck_label)]
811    Unsupported(#[primary_span] Span),
812}
813
814#[derive(Diagnostic)]
815#[diag(hir_typeck_ctor_is_private, code = E0603)]
816pub(crate) struct CtorIsPrivate {
817    #[primary_span]
818    pub span: Span,
819    pub def: String,
820}
821
822#[derive(Subdiagnostic)]
823#[note(hir_typeck_deref_is_empty)]
824pub(crate) struct DerefImplsIsEmpty<'tcx> {
825    #[primary_span]
826    pub span: Span,
827    pub deref_ty: Ty<'tcx>,
828}
829
830#[derive(Subdiagnostic)]
831#[multipart_suggestion(
832    hir_typeck_convert_using_method,
833    applicability = "machine-applicable",
834    style = "verbose"
835)]
836pub(crate) struct SuggestConvertViaMethod<'tcx> {
837    #[suggestion_part(code = "{sugg}")]
838    pub span: Span,
839    #[suggestion_part(code = "")]
840    pub borrow_removal_span: Option<Span>,
841    pub sugg: String,
842    pub expected: Ty<'tcx>,
843    pub found: Ty<'tcx>,
844}
845
846#[derive(Subdiagnostic)]
847#[note(hir_typeck_note_caller_chooses_ty_for_ty_param)]
848pub(crate) struct NoteCallerChoosesTyForTyParam<'tcx> {
849    pub ty_param_name: Symbol,
850    pub found_ty: Ty<'tcx>,
851}
852
853#[derive(Subdiagnostic)]
854pub(crate) enum SuggestBoxingForReturnImplTrait {
855    #[multipart_suggestion(hir_typeck_rpit_change_return_type, applicability = "maybe-incorrect")]
856    ChangeReturnType {
857        #[suggestion_part(code = "Box<dyn")]
858        start_sp: Span,
859        #[suggestion_part(code = ">")]
860        end_sp: Span,
861    },
862    #[multipart_suggestion(hir_typeck_rpit_box_return_expr, applicability = "maybe-incorrect")]
863    BoxReturnExpr {
864        #[suggestion_part(code = "Box::new(")]
865        starts: Vec<Span>,
866        #[suggestion_part(code = ")")]
867        ends: Vec<Span>,
868    },
869}
870
871#[derive(Diagnostic)]
872#[diag(hir_typeck_self_ctor_from_outer_item, code = E0401)]
873pub(crate) struct SelfCtorFromOuterItem {
874    #[primary_span]
875    pub span: Span,
876    #[label]
877    pub impl_span: Span,
878    #[subdiagnostic]
879    pub sugg: Option<ReplaceWithName>,
880}
881
882#[derive(LintDiagnostic)]
883#[diag(hir_typeck_self_ctor_from_outer_item)]
884pub(crate) struct SelfCtorFromOuterItemLint {
885    #[label]
886    pub impl_span: Span,
887    #[subdiagnostic]
888    pub sugg: Option<ReplaceWithName>,
889}
890
891#[derive(Subdiagnostic)]
892#[suggestion(hir_typeck_suggestion, code = "{name}", applicability = "machine-applicable")]
893pub(crate) struct ReplaceWithName {
894    #[primary_span]
895    pub span: Span,
896    pub name: String,
897}
898
899#[derive(Diagnostic)]
900#[diag(hir_typeck_cast_thin_pointer_to_wide_pointer, code = E0607)]
901pub(crate) struct CastThinPointerToWidePointer<'tcx> {
902    #[primary_span]
903    pub span: Span,
904    pub expr_ty: Ty<'tcx>,
905    pub cast_ty: Ty<'tcx>,
906    #[note(hir_typeck_teach_help)]
907    pub(crate) teach: bool,
908}
909
910#[derive(Diagnostic)]
911#[diag(hir_typeck_pass_to_variadic_function, code = E0617)]
912pub(crate) struct PassToVariadicFunction<'a, 'tcx> {
913    #[primary_span]
914    pub span: Span,
915    pub ty: Ty<'tcx>,
916    pub cast_ty: &'a str,
917    #[suggestion(code = " as {cast_ty}", applicability = "machine-applicable", style = "verbose")]
918    pub sugg_span: Span,
919    #[note(hir_typeck_teach_help)]
920    pub(crate) teach: bool,
921}
922
923#[derive(Diagnostic)]
924#[diag(hir_typeck_fn_item_to_variadic_function, code = E0617)]
925#[help]
926#[note]
927pub(crate) struct PassFnItemToVariadicFunction {
928    #[primary_span]
929    pub span: Span,
930    #[suggestion(code = " as {replace}", applicability = "machine-applicable", style = "verbose")]
931    pub sugg_span: Span,
932    pub replace: String,
933}
934
935#[derive(Subdiagnostic)]
936#[suggestion(
937    hir_typeck_replace_comma_with_semicolon,
938    applicability = "machine-applicable",
939    style = "verbose",
940    code = "; "
941)]
942pub(crate) struct ReplaceCommaWithSemicolon {
943    #[primary_span]
944    pub comma_span: Span,
945    pub descr: &'static str,
946}
947
948#[derive(LintDiagnostic)]
949#[diag(hir_typeck_supertrait_item_shadowing)]
950pub(crate) struct SupertraitItemShadowing {
951    pub item: Symbol,
952    pub subtrait: Symbol,
953    #[subdiagnostic]
954    pub shadower: SupertraitItemShadower,
955    #[subdiagnostic]
956    pub shadowee: SupertraitItemShadowee,
957}
958
959#[derive(Subdiagnostic)]
960#[note(hir_typeck_supertrait_item_shadower)]
961pub(crate) struct SupertraitItemShadower {
962    pub subtrait: Symbol,
963    #[primary_span]
964    pub span: Span,
965}
966
967#[derive(Subdiagnostic)]
968pub(crate) enum SupertraitItemShadowee {
969    #[note(hir_typeck_supertrait_item_shadowee)]
970    Labeled {
971        #[primary_span]
972        span: Span,
973        supertrait: Symbol,
974    },
975    #[note(hir_typeck_supertrait_item_multiple_shadowee)]
976    Several {
977        #[primary_span]
978        spans: MultiSpan,
979        traits: DiagSymbolList,
980    },
981}