1use std::path::{Path, PathBuf};
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::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)]
42#[diag(middle_unsupported_union)]
43pub struct UnsupportedUnion {
44 pub ty_name: String,
45}
46
47#[derive(Diagnostic)]
49#[diag(middle_autodiff_unsafe_inner_const_ref)]
50pub struct AutodiffUnsafeInnerConstRef {
51 #[primary_span]
52 pub span: Span,
53 pub ty: String,
54}
55
56#[derive(Subdiagnostic)]
57pub enum TypeMismatchReason {
58 #[label(middle_conflict_types)]
59 ConflictType {
60 #[primary_span]
61 span: Span,
62 },
63 #[note(middle_previous_use_here)]
64 PreviousUse {
65 #[primary_span]
66 span: Span,
67 },
68}
69
70#[derive(Diagnostic)]
71#[diag(middle_limit_invalid)]
72pub(crate) struct LimitInvalid<'a> {
73 #[primary_span]
74 pub span: Span,
75 #[label]
76 pub value_span: Span,
77 pub error_str: &'a str,
78}
79
80#[derive(Diagnostic)]
81#[diag(middle_recursion_limit_reached)]
82#[help]
83pub(crate) struct RecursionLimitReached<'tcx> {
84 pub ty: Ty<'tcx>,
85 pub suggested_limit: rustc_session::Limit,
86}
87
88#[derive(Diagnostic)]
89#[diag(middle_const_eval_non_int)]
90pub(crate) struct ConstEvalNonIntError {
91 #[primary_span]
92 pub span: Span,
93}
94
95#[derive(Diagnostic)]
96#[diag(middle_strict_coherence_needs_negative_coherence)]
97pub(crate) struct StrictCoherenceNeedsNegativeCoherence {
98 #[primary_span]
99 pub span: Span,
100 #[label]
101 pub attr_span: Option<Span>,
102}
103
104#[derive(Diagnostic)]
105#[diag(middle_requires_lang_item)]
106pub(crate) struct RequiresLangItem {
107 #[primary_span]
108 pub span: Option<Span>,
109 pub name: Symbol,
110}
111
112#[derive(Diagnostic)]
113#[diag(middle_const_not_used_in_type_alias)]
114pub(super) struct ConstNotUsedTraitAlias {
115 pub ct: String,
116 #[primary_span]
117 pub span: Span,
118}
119
120pub struct CustomSubdiagnostic<'a> {
121 pub msg: fn() -> DiagMessage,
122 pub add_args: Box<dyn FnOnce(&mut dyn FnMut(DiagArgName, DiagArgValue)) + 'a>,
123}
124
125impl<'a> CustomSubdiagnostic<'a> {
126 pub fn label(x: fn() -> DiagMessage) -> Self {
127 Self::label_and_then(x, |_| {})
128 }
129 pub fn label_and_then<F: FnOnce(&mut dyn FnMut(DiagArgName, DiagArgValue)) + 'a>(
130 msg: fn() -> DiagMessage,
131 f: F,
132 ) -> Self {
133 Self { msg, add_args: Box::new(move |x| f(x)) }
134 }
135}
136
137impl fmt::Debug for CustomSubdiagnostic<'_> {
138 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
139 f.debug_struct("CustomSubdiagnostic").finish_non_exhaustive()
140 }
141}
142
143#[derive(Diagnostic)]
144pub enum LayoutError<'tcx> {
145 #[diag(middle_unknown_layout)]
146 Unknown { ty: Ty<'tcx> },
147
148 #[diag(middle_too_generic)]
149 TooGeneric { ty: Ty<'tcx> },
150
151 #[diag(middle_values_too_big)]
152 Overflow { ty: Ty<'tcx> },
153
154 #[diag(middle_cannot_be_normalized)]
155 NormalizationFailure { ty: Ty<'tcx>, failure_ty: String },
156
157 #[diag(middle_cycle)]
158 Cycle,
159
160 #[diag(middle_layout_references_error)]
161 ReferencesError,
162}
163
164#[derive(Diagnostic)]
165#[diag(middle_erroneous_constant)]
166pub(crate) struct ErroneousConstant {
167 #[primary_span]
168 pub span: Span,
169}
170
171#[derive(Diagnostic)]
172#[diag(middle_type_length_limit)]
173#[help(middle_consider_type_length_limit)]
174pub(crate) struct TypeLengthLimit {
175 #[primary_span]
176 pub span: Span,
177 pub shrunk: String,
178 #[note(middle_written_to_path)]
179 pub was_written: bool,
180 pub path: PathBuf,
181 pub type_length: usize,
182}