rustc_middle/
error.rs

1use std::path::Path;
2use std::{fmt, io};
3
4use rustc_errors::codes::*;
5use rustc_errors::{DiagArgName, DiagArgValue, DiagMessage};
6use rustc_macros::{Diagnostic, Subdiagnostic};
7use rustc_span::{Span, Symbol};
8
9use crate::ty::{Instance, Ty};
10
11#[derive(Diagnostic)]
12#[diag(middle_drop_check_overflow, code = E0320)]
13#[note]
14pub(crate) struct DropCheckOverflow<'tcx> {
15    #[primary_span]
16    pub span: Span,
17    pub ty: Ty<'tcx>,
18    pub overflow_ty: Ty<'tcx>,
19}
20
21#[derive(Diagnostic)]
22#[diag(middle_failed_writing_file)]
23pub(crate) struct FailedWritingFile<'a> {
24    pub path: &'a Path,
25    pub error: io::Error,
26}
27
28#[derive(Diagnostic)]
29#[diag(middle_opaque_hidden_type_mismatch)]
30pub(crate) struct OpaqueHiddenTypeMismatch<'tcx> {
31    pub self_ty: Ty<'tcx>,
32    pub other_ty: Ty<'tcx>,
33    #[primary_span]
34    #[label]
35    pub other_span: Span,
36    #[subdiagnostic]
37    pub sub: TypeMismatchReason,
38}
39
40#[derive(Diagnostic)]
41#[diag(middle_unsupported_union)]
42pub struct UnsupportedUnion {
43    pub ty_name: String,
44}
45
46// FIXME(autodiff): I should get used somewhere
47#[derive(Diagnostic)]
48#[diag(middle_autodiff_unsafe_inner_const_ref)]
49pub struct AutodiffUnsafeInnerConstRef<'tcx> {
50    #[primary_span]
51    pub span: Span,
52    pub ty: Ty<'tcx>,
53}
54
55#[derive(Subdiagnostic)]
56pub enum TypeMismatchReason {
57    #[label(middle_conflict_types)]
58    ConflictType {
59        #[primary_span]
60        span: Span,
61    },
62    #[note(middle_previous_use_here)]
63    PreviousUse {
64        #[primary_span]
65        span: Span,
66    },
67}
68
69#[derive(Diagnostic)]
70#[diag(middle_recursion_limit_reached)]
71#[help]
72pub(crate) struct RecursionLimitReached<'tcx> {
73    #[primary_span]
74    pub span: Span,
75    pub ty: Ty<'tcx>,
76    pub suggested_limit: rustc_hir::limit::Limit,
77}
78
79#[derive(Diagnostic)]
80#[diag(middle_const_eval_non_int)]
81pub(crate) struct ConstEvalNonIntError {
82    #[primary_span]
83    pub span: Span,
84}
85
86#[derive(Diagnostic)]
87#[diag(middle_strict_coherence_needs_negative_coherence)]
88pub(crate) struct StrictCoherenceNeedsNegativeCoherence {
89    #[primary_span]
90    pub span: Span,
91    #[label]
92    pub attr_span: Option<Span>,
93}
94
95#[derive(Diagnostic)]
96#[diag(middle_requires_lang_item)]
97pub(crate) struct RequiresLangItem {
98    #[primary_span]
99    pub span: Span,
100    pub name: Symbol,
101}
102
103#[derive(Diagnostic)]
104#[diag(middle_const_not_used_in_type_alias)]
105pub(super) struct ConstNotUsedTraitAlias {
106    pub ct: String,
107    #[primary_span]
108    pub span: Span,
109}
110
111pub struct CustomSubdiagnostic<'a> {
112    pub msg: fn() -> DiagMessage,
113    pub add_args: Box<dyn FnOnce(&mut dyn FnMut(DiagArgName, DiagArgValue)) + 'a>,
114}
115
116impl<'a> CustomSubdiagnostic<'a> {
117    pub fn label(x: fn() -> DiagMessage) -> Self {
118        Self::label_and_then(x, |_| {})
119    }
120    pub fn label_and_then<F: FnOnce(&mut dyn FnMut(DiagArgName, DiagArgValue)) + 'a>(
121        msg: fn() -> DiagMessage,
122        f: F,
123    ) -> Self {
124        Self { msg, add_args: Box::new(move |x| f(x)) }
125    }
126}
127
128impl fmt::Debug for CustomSubdiagnostic<'_> {
129    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
130        f.debug_struct("CustomSubdiagnostic").finish_non_exhaustive()
131    }
132}
133
134#[derive(Diagnostic)]
135pub enum LayoutError<'tcx> {
136    #[diag(middle_layout_unknown)]
137    Unknown { ty: Ty<'tcx> },
138
139    #[diag(middle_layout_too_generic)]
140    TooGeneric { ty: Ty<'tcx> },
141
142    #[diag(middle_layout_size_overflow)]
143    Overflow { ty: Ty<'tcx> },
144
145    #[diag(middle_layout_simd_too_many)]
146    SimdTooManyLanes { ty: Ty<'tcx>, max_lanes: u64 },
147
148    #[diag(middle_layout_simd_zero_length)]
149    SimdZeroLength { ty: Ty<'tcx> },
150
151    #[diag(middle_layout_normalization_failure)]
152    NormalizationFailure { ty: Ty<'tcx>, failure_ty: String },
153
154    #[diag(middle_layout_cycle)]
155    Cycle,
156
157    #[diag(middle_layout_references_error)]
158    ReferencesError,
159}
160
161#[derive(Diagnostic)]
162#[diag(middle_erroneous_constant)]
163pub(crate) struct ErroneousConstant {
164    #[primary_span]
165    pub span: Span,
166}
167
168#[derive(Diagnostic)]
169#[diag(middle_type_length_limit)]
170#[help(middle_consider_type_length_limit)]
171pub(crate) struct TypeLengthLimit<'tcx> {
172    #[primary_span]
173    pub span: Span,
174    pub instance: Instance<'tcx>,
175    pub type_length: usize,
176}
177
178#[derive(Diagnostic)]
179#[diag(middle_max_num_nodes_in_valtree)]
180pub(crate) struct MaxNumNodesInValtree {
181    #[primary_span]
182    pub span: Span,
183    pub global_const_id: String,
184}
185
186#[derive(Diagnostic)]
187#[diag(middle_invalid_const_in_valtree)]
188#[note]
189pub(crate) struct InvalidConstInValtree {
190    #[primary_span]
191    pub span: Span,
192    pub global_const_id: String,
193}