33use rustc_abi:: Size ;
44use rustc_ast as ast;
55use rustc_hir:: LangItem ;
6- use rustc_middle:: mir:: interpret:: {
7- Allocation , CTFE_ALLOC_SALT , LitToConstError , LitToConstInput , Scalar ,
8- } ;
6+ use rustc_middle:: mir:: interpret:: { Allocation , CTFE_ALLOC_SALT , LitToConstInput , Scalar } ;
97use rustc_middle:: mir:: * ;
108use rustc_middle:: thir:: * ;
119use rustc_middle:: ty:: {
12- self , CanonicalUserType , CanonicalUserTypeAnnotation , Ty , TyCtxt , UserTypeAnnotationIndex ,
10+ self , CanonicalUserType , CanonicalUserTypeAnnotation , Ty , TyCtxt , TypeVisitableExt as _,
11+ UserTypeAnnotationIndex ,
1312} ;
1413use rustc_middle:: { bug, mir, span_bug} ;
1514use tracing:: { instrument, trace} ;
@@ -50,16 +49,7 @@ pub(crate) fn as_constant_inner<'tcx>(
5049 let Expr { ty, temp_lifetime : _, span, ref kind } = * expr;
5150 match * kind {
5251 ExprKind :: Literal { lit, neg } => {
53- let const_ = match lit_to_mir_constant ( tcx, LitToConstInput { lit : & lit. node , ty, neg } )
54- {
55- Ok ( c) => c,
56- Err ( LitToConstError :: Reported ( guar) ) => {
57- Const :: Ty ( Ty :: new_error ( tcx, guar) , ty:: Const :: new_error ( tcx, guar) )
58- }
59- Err ( LitToConstError :: TypeError ) => {
60- bug ! ( "encountered type error in `lit_to_mir_constant`" )
61- }
62- } ;
52+ let const_ = lit_to_mir_constant ( tcx, LitToConstInput { lit : & lit. node , ty, neg } ) ;
6353
6454 ConstOperand { span, user_ty : None , const_ }
6555 }
@@ -108,11 +98,13 @@ pub(crate) fn as_constant_inner<'tcx>(
10898}
10999
110100#[ instrument( skip( tcx, lit_input) ) ]
111- fn lit_to_mir_constant < ' tcx > (
112- tcx : TyCtxt < ' tcx > ,
113- lit_input : LitToConstInput < ' tcx > ,
114- ) -> Result < Const < ' tcx > , LitToConstError > {
101+ fn lit_to_mir_constant < ' tcx > ( tcx : TyCtxt < ' tcx > , lit_input : LitToConstInput < ' tcx > ) -> Const < ' tcx > {
115102 let LitToConstInput { lit, ty, neg } = lit_input;
103+
104+ if let Err ( guar) = ty. error_reported ( ) {
105+ return Const :: Ty ( Ty :: new_error ( tcx, guar) , ty:: Const :: new_error ( tcx, guar) ) ;
106+ }
107+
116108 let trunc = |n| {
117109 let width = match tcx. layout_of ( ty:: TypingEnv :: fully_monomorphized ( ) . as_query_input ( ty) ) {
118110 Ok ( layout) => layout. size ,
@@ -123,7 +115,7 @@ fn lit_to_mir_constant<'tcx>(
123115 trace ! ( "trunc {} with size {} and shift {}" , n, width. bits( ) , 128 - width. bits( ) ) ;
124116 let result = width. truncate ( n) ;
125117 trace ! ( "trunc result: {}" , result) ;
126- Ok ( ConstValue :: Scalar ( Scalar :: from_uint ( result, width) ) )
118+ ConstValue :: Scalar ( Scalar :: from_uint ( result, width) )
127119 } ;
128120
129121 let value = match ( lit, ty. kind ( ) ) {
@@ -154,20 +146,18 @@ fn lit_to_mir_constant<'tcx>(
154146 ConstValue :: Scalar ( Scalar :: from_uint ( * n, Size :: from_bytes ( 1 ) ) )
155147 }
156148 ( ast:: LitKind :: Int ( n, _) , ty:: Uint ( _) ) | ( ast:: LitKind :: Int ( n, _) , ty:: Int ( _) ) => {
157- trunc ( if neg { ( n. get ( ) as i128 ) . overflowing_neg ( ) . 0 as u128 } else { n. get ( ) } ) ?
158- }
159- ( ast:: LitKind :: Float ( n, _) , ty:: Float ( fty) ) => parse_float_into_constval ( * n, * fty, neg)
160- . ok_or_else ( || {
161- LitToConstError :: Reported (
162- tcx. dcx ( )
163- . delayed_bug ( format ! ( "couldn't parse float literal: {:?}" , lit_input. lit) ) ,
164- )
165- } ) ?,
149+ trunc ( if neg { ( n. get ( ) as i128 ) . overflowing_neg ( ) . 0 as u128 } else { n. get ( ) } )
150+ }
151+ ( ast:: LitKind :: Float ( n, _) , ty:: Float ( fty) ) => {
152+ parse_float_into_constval ( * n, * fty, neg) . unwrap ( )
153+ }
166154 ( ast:: LitKind :: Bool ( b) , ty:: Bool ) => ConstValue :: Scalar ( Scalar :: from_bool ( * b) ) ,
167155 ( ast:: LitKind :: Char ( c) , ty:: Char ) => ConstValue :: Scalar ( Scalar :: from_char ( * c) ) ,
168- ( ast:: LitKind :: Err ( guar) , _) => return Err ( LitToConstError :: Reported ( * guar) ) ,
169- _ => return Err ( LitToConstError :: TypeError ) ,
156+ ( ast:: LitKind :: Err ( guar) , _) => {
157+ return Const :: Ty ( Ty :: new_error ( tcx, * guar) , ty:: Const :: new_error ( tcx, * guar) ) ;
158+ }
159+ _ => bug ! ( "invalid lit/ty combination in `lit_to_mir_constant`: {lit:?}: {ty:?}" ) ,
170160 } ;
171161
172- Ok ( Const :: Val ( value, ty) )
162+ Const :: Val ( value, ty)
173163}
0 commit comments