@@ -8,7 +8,7 @@ use crate::ty::{self, layout, Ty};
8
8
9
9
use backtrace:: Backtrace ;
10
10
use rustc_data_structures:: sync:: Lock ;
11
- use rustc_errors:: { struct_span_err, DiagnosticBuilder } ;
11
+ use rustc_errors:: { struct_span_err, DiagnosticBuilder , ErrorReported } ;
12
12
use rustc_hir as hir;
13
13
use rustc_hir:: definitions:: DefPathData ;
14
14
use rustc_macros:: HashStable ;
@@ -19,25 +19,16 @@ use std::{any::Any, fmt, mem};
19
19
20
20
#[ derive( Debug , Copy , Clone , PartialEq , Eq , HashStable , RustcEncodable , RustcDecodable ) ]
21
21
pub enum ErrorHandled {
22
- /// Already reported a lint or an error for this evaluation.
23
- Reported ,
22
+ /// Already reported an error for this evaluation, and the compilation is
23
+ /// *guaranteed* to fail. Warnings/lints *must not* produce `Reported`.
24
+ Reported ( ErrorReported ) ,
25
+ /// Already emitted a lint for this evaluation.
26
+ Linted ,
24
27
/// Don't emit an error, the evaluation failed because the MIR was generic
25
28
/// and the substs didn't fully monomorphize it.
26
29
TooGeneric ,
27
30
}
28
31
29
- impl ErrorHandled {
30
- pub fn assert_reported ( self ) {
31
- match self {
32
- ErrorHandled :: Reported => { }
33
- ErrorHandled :: TooGeneric => bug ! (
34
- "MIR interpretation failed without reporting an error \
35
- even though it was fully monomorphized"
36
- ) ,
37
- }
38
- }
39
- }
40
-
41
32
CloneTypeFoldableImpls ! {
42
33
ErrorHandled ,
43
34
}
@@ -84,15 +75,12 @@ impl<'tcx> ConstEvalErr<'tcx> {
84
75
tcx : TyCtxtAt < ' tcx > ,
85
76
message : & str ,
86
77
emit : impl FnOnce ( DiagnosticBuilder < ' _ > ) ,
87
- ) -> Result < ( ) , ErrorHandled > {
78
+ ) -> ErrorHandled {
88
79
self . struct_generic ( tcx, message, emit, None )
89
80
}
90
81
91
82
pub fn report_as_error ( & self , tcx : TyCtxtAt < ' tcx > , message : & str ) -> ErrorHandled {
92
- match self . struct_error ( tcx, message, |mut e| e. emit ( ) ) {
93
- Ok ( _) => ErrorHandled :: Reported ,
94
- Err ( x) => x,
95
- }
83
+ self . struct_error ( tcx, message, |mut e| e. emit ( ) )
96
84
}
97
85
98
86
pub fn report_as_lint (
@@ -102,7 +90,7 @@ impl<'tcx> ConstEvalErr<'tcx> {
102
90
lint_root : hir:: HirId ,
103
91
span : Option < Span > ,
104
92
) -> ErrorHandled {
105
- match self . struct_generic (
93
+ self . struct_generic (
106
94
tcx,
107
95
message,
108
96
|mut lint : DiagnosticBuilder < ' _ > | {
@@ -122,10 +110,7 @@ impl<'tcx> ConstEvalErr<'tcx> {
122
110
lint. emit ( ) ;
123
111
} ,
124
112
Some ( lint_root) ,
125
- ) {
126
- Ok ( _) => ErrorHandled :: Reported ,
127
- Err ( err) => err,
128
- }
113
+ )
129
114
}
130
115
131
116
/// Create a diagnostic for this const eval error.
@@ -143,12 +128,14 @@ impl<'tcx> ConstEvalErr<'tcx> {
143
128
message : & str ,
144
129
emit : impl FnOnce ( DiagnosticBuilder < ' _ > ) ,
145
130
lint_root : Option < hir:: HirId > ,
146
- ) -> Result < ( ) , ErrorHandled > {
131
+ ) -> ErrorHandled {
147
132
let must_error = match self . error {
148
133
err_inval ! ( Layout ( LayoutError :: Unknown ( _) ) ) | err_inval ! ( TooGeneric ) => {
149
- return Err ( ErrorHandled :: TooGeneric ) ;
134
+ return ErrorHandled :: TooGeneric ;
135
+ }
136
+ err_inval ! ( TypeckError ( error_reported) ) => {
137
+ return ErrorHandled :: Reported ( error_reported) ;
150
138
}
151
- err_inval ! ( TypeckError ) => return Err ( ErrorHandled :: Reported ) ,
152
139
// We must *always* hard error on these, even if the caller wants just a lint.
153
140
err_inval ! ( Layout ( LayoutError :: SizeOverflow ( _) ) ) => true ,
154
141
_ => false ,
@@ -183,6 +170,7 @@ impl<'tcx> ConstEvalErr<'tcx> {
183
170
// caller thinks anyway.
184
171
// See <https://github.com/rust-lang/rust/pull/63152>.
185
172
finish ( struct_error ( tcx, & err_msg) , None ) ;
173
+ ErrorHandled :: Reported ( ErrorReported )
186
174
} else {
187
175
// Regular case.
188
176
if let Some ( lint_root) = lint_root {
@@ -200,12 +188,13 @@ impl<'tcx> ConstEvalErr<'tcx> {
200
188
tcx. span ,
201
189
|lint| finish ( lint. build ( message) , Some ( err_msg) ) ,
202
190
) ;
191
+ ErrorHandled :: Linted
203
192
} else {
204
193
// Report as hard error.
205
194
finish ( struct_error ( tcx, message) , Some ( err_msg) ) ;
195
+ ErrorHandled :: Reported ( ErrorReported )
206
196
}
207
197
}
208
- Ok ( ( ) )
209
198
}
210
199
}
211
200
@@ -246,7 +235,9 @@ fn print_backtrace(backtrace: &mut Backtrace) {
246
235
impl From < ErrorHandled > for InterpErrorInfo < ' _ > {
247
236
fn from ( err : ErrorHandled ) -> Self {
248
237
match err {
249
- ErrorHandled :: Reported => err_inval ! ( ReferencedConstant ) ,
238
+ ErrorHandled :: Reported ( ErrorReported ) | ErrorHandled :: Linted => {
239
+ err_inval ! ( ReferencedConstant )
240
+ }
250
241
ErrorHandled :: TooGeneric => err_inval ! ( TooGeneric ) ,
251
242
}
252
243
. into ( )
@@ -288,7 +279,7 @@ pub enum InvalidProgramInfo<'tcx> {
288
279
/// which already produced an error.
289
280
ReferencedConstant ,
290
281
/// Abort in case type errors are reached.
291
- TypeckError ,
282
+ TypeckError ( ErrorReported ) ,
292
283
/// An error occurred during layout computation.
293
284
Layout ( layout:: LayoutError < ' tcx > ) ,
294
285
/// An invalid transmute happened.
@@ -301,7 +292,9 @@ impl fmt::Debug for InvalidProgramInfo<'_> {
301
292
match self {
302
293
TooGeneric => write ! ( f, "encountered overly generic constant" ) ,
303
294
ReferencedConstant => write ! ( f, "referenced constant has errors" ) ,
304
- TypeckError => write ! ( f, "encountered constants with type errors, stopping evaluation" ) ,
295
+ TypeckError ( ErrorReported ) => {
296
+ write ! ( f, "encountered constants with type errors, stopping evaluation" )
297
+ }
305
298
Layout ( ref err) => write ! ( f, "{}" , err) ,
306
299
TransmuteSizeDiff ( from_ty, to_ty) => write ! (
307
300
f,
0 commit comments