@@ -8,7 +8,7 @@ use crate::ty::{self, layout, Ty};
88
99use backtrace:: Backtrace ;
1010use rustc_data_structures:: sync:: Lock ;
11- use rustc_errors:: { struct_span_err, DiagnosticBuilder } ;
11+ use rustc_errors:: { struct_span_err, DiagnosticBuilder , ErrorReported } ;
1212use rustc_hir as hir;
1313use rustc_hir:: definitions:: DefPathData ;
1414use rustc_macros:: HashStable ;
@@ -19,25 +19,16 @@ use std::{any::Any, fmt, mem};
1919
2020#[ derive( Debug , Copy , Clone , PartialEq , Eq , HashStable , RustcEncodable , RustcDecodable ) ]
2121pub 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 ,
2427 /// Don't emit an error, the evaluation failed because the MIR was generic
2528 /// and the substs didn't fully monomorphize it.
2629 TooGeneric ,
2730}
2831
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-
4132CloneTypeFoldableImpls ! {
4233 ErrorHandled ,
4334}
@@ -84,15 +75,12 @@ impl<'tcx> ConstEvalErr<'tcx> {
8475 tcx : TyCtxtAt < ' tcx > ,
8576 message : & str ,
8677 emit : impl FnOnce ( DiagnosticBuilder < ' _ > ) ,
87- ) -> Result < ( ) , ErrorHandled > {
78+ ) -> ErrorHandled {
8879 self . struct_generic ( tcx, message, emit, None )
8980 }
9081
9182 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 ( ) )
9684 }
9785
9886 pub fn report_as_lint (
@@ -102,7 +90,7 @@ impl<'tcx> ConstEvalErr<'tcx> {
10290 lint_root : hir:: HirId ,
10391 span : Option < Span > ,
10492 ) -> ErrorHandled {
105- match self . struct_generic (
93+ self . struct_generic (
10694 tcx,
10795 message,
10896 |mut lint : DiagnosticBuilder < ' _ > | {
@@ -122,10 +110,7 @@ impl<'tcx> ConstEvalErr<'tcx> {
122110 lint. emit ( ) ;
123111 } ,
124112 Some ( lint_root) ,
125- ) {
126- Ok ( _) => ErrorHandled :: Reported ,
127- Err ( err) => err,
128- }
113+ )
129114 }
130115
131116 /// Create a diagnostic for this const eval error.
@@ -143,12 +128,14 @@ impl<'tcx> ConstEvalErr<'tcx> {
143128 message : & str ,
144129 emit : impl FnOnce ( DiagnosticBuilder < ' _ > ) ,
145130 lint_root : Option < hir:: HirId > ,
146- ) -> Result < ( ) , ErrorHandled > {
131+ ) -> ErrorHandled {
147132 let must_error = match self . error {
148133 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) ;
150138 }
151- err_inval ! ( TypeckError ) => return Err ( ErrorHandled :: Reported ) ,
152139 // We must *always* hard error on these, even if the caller wants just a lint.
153140 err_inval ! ( Layout ( LayoutError :: SizeOverflow ( _) ) ) => true ,
154141 _ => false ,
@@ -183,6 +170,7 @@ impl<'tcx> ConstEvalErr<'tcx> {
183170 // caller thinks anyway.
184171 // See <https://github.com/rust-lang/rust/pull/63152>.
185172 finish ( struct_error ( tcx, & err_msg) , None ) ;
173+ ErrorHandled :: Reported ( ErrorReported )
186174 } else {
187175 // Regular case.
188176 if let Some ( lint_root) = lint_root {
@@ -200,12 +188,13 @@ impl<'tcx> ConstEvalErr<'tcx> {
200188 tcx. span ,
201189 |lint| finish ( lint. build ( message) , Some ( err_msg) ) ,
202190 ) ;
191+ ErrorHandled :: Linted
203192 } else {
204193 // Report as hard error.
205194 finish ( struct_error ( tcx, message) , Some ( err_msg) ) ;
195+ ErrorHandled :: Reported ( ErrorReported )
206196 }
207197 }
208- Ok ( ( ) )
209198 }
210199}
211200
@@ -246,7 +235,9 @@ fn print_backtrace(backtrace: &mut Backtrace) {
246235impl From < ErrorHandled > for InterpErrorInfo < ' _ > {
247236 fn from ( err : ErrorHandled ) -> Self {
248237 match err {
249- ErrorHandled :: Reported => err_inval ! ( ReferencedConstant ) ,
238+ ErrorHandled :: Reported ( ErrorReported ) | ErrorHandled :: Linted => {
239+ err_inval ! ( ReferencedConstant )
240+ }
250241 ErrorHandled :: TooGeneric => err_inval ! ( TooGeneric ) ,
251242 }
252243 . into ( )
@@ -288,7 +279,7 @@ pub enum InvalidProgramInfo<'tcx> {
288279 /// which already produced an error.
289280 ReferencedConstant ,
290281 /// Abort in case type errors are reached.
291- TypeckError ,
282+ TypeckError ( ErrorReported ) ,
292283 /// An error occurred during layout computation.
293284 Layout ( layout:: LayoutError < ' tcx > ) ,
294285 /// An invalid transmute happened.
@@ -301,7 +292,9 @@ impl fmt::Debug for InvalidProgramInfo<'_> {
301292 match self {
302293 TooGeneric => write ! ( f, "encountered overly generic constant" ) ,
303294 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+ }
305298 Layout ( ref err) => write ! ( f, "{}" , err) ,
306299 TransmuteSizeDiff ( from_ty, to_ty) => write ! (
307300 f,
0 commit comments