3
3
use rustc_abi:: Size ;
4
4
use rustc_ast as ast;
5
5
use 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 } ;
9
7
use rustc_middle:: mir:: * ;
10
8
use rustc_middle:: thir:: * ;
11
9
use rustc_middle:: ty:: {
12
- self , CanonicalUserType , CanonicalUserTypeAnnotation , Ty , TyCtxt , UserTypeAnnotationIndex ,
10
+ self , CanonicalUserType , CanonicalUserTypeAnnotation , Ty , TyCtxt , TypeVisitableExt as _,
11
+ UserTypeAnnotationIndex ,
13
12
} ;
14
13
use rustc_middle:: { bug, mir, span_bug} ;
15
14
use tracing:: { instrument, trace} ;
@@ -50,16 +49,7 @@ pub(crate) fn as_constant_inner<'tcx>(
50
49
let Expr { ty, temp_lifetime : _, span, ref kind } = * expr;
51
50
match * kind {
52
51
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 } ) ;
63
53
64
54
ConstOperand { span, user_ty : None , const_ }
65
55
}
@@ -108,11 +98,13 @@ pub(crate) fn as_constant_inner<'tcx>(
108
98
}
109
99
110
100
#[ 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 > {
115
102
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
+
116
108
let trunc = |n| {
117
109
let width = match tcx. layout_of ( ty:: TypingEnv :: fully_monomorphized ( ) . as_query_input ( ty) ) {
118
110
Ok ( layout) => layout. size ,
@@ -123,7 +115,7 @@ fn lit_to_mir_constant<'tcx>(
123
115
trace ! ( "trunc {} with size {} and shift {}" , n, width. bits( ) , 128 - width. bits( ) ) ;
124
116
let result = width. truncate ( n) ;
125
117
trace ! ( "trunc result: {}" , result) ;
126
- Ok ( ConstValue :: Scalar ( Scalar :: from_uint ( result, width) ) )
118
+ ConstValue :: Scalar ( Scalar :: from_uint ( result, width) )
127
119
} ;
128
120
129
121
let value = match ( lit, ty. kind ( ) ) {
@@ -154,20 +146,18 @@ fn lit_to_mir_constant<'tcx>(
154
146
ConstValue :: Scalar ( Scalar :: from_uint ( * n, Size :: from_bytes ( 1 ) ) )
155
147
}
156
148
( 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
+ }
166
154
( ast:: LitKind :: Bool ( b) , ty:: Bool ) => ConstValue :: Scalar ( Scalar :: from_bool ( * b) ) ,
167
155
( 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:?}" ) ,
170
160
} ;
171
161
172
- Ok ( Const :: Val ( value, ty) )
162
+ Const :: Val ( value, ty)
173
163
}
0 commit comments