@@ -8,7 +8,9 @@ use rustc_middle::mir::interpret::{
88} ;
99use rustc_middle:: mir:: * ;
1010use rustc_middle:: thir:: * ;
11- use rustc_middle:: ty:: { self , CanonicalUserTypeAnnotation , TyCtxt } ;
11+ use rustc_middle:: ty:: {
12+ self , CanonicalUserType , CanonicalUserTypeAnnotation , TyCtxt , UserTypeAnnotationIndex ,
13+ } ;
1214use rustc_span:: DUMMY_SP ;
1315use rustc_target:: abi:: Size ;
1416
@@ -19,84 +21,87 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1921 let this = self ;
2022 let tcx = this. tcx ;
2123 let Expr { ty, temp_lifetime : _, span, ref kind } = * expr;
22- match * kind {
24+ match kind {
2325 ExprKind :: Scope { region_scope : _, lint_level : _, value } => {
24- this. as_constant ( & this. thir [ value] )
25- }
26- ExprKind :: Literal { lit, neg } => {
27- let literal =
28- match lit_to_mir_constant ( tcx, LitToConstInput { lit : & lit. node , ty, neg } ) {
29- Ok ( c) => c,
30- Err ( LitToConstError :: Reported ( guar) ) => {
31- ConstantKind :: Ty ( tcx. const_error_with_guaranteed ( ty, guar) )
32- }
33- Err ( LitToConstError :: TypeError ) => {
34- bug ! ( "encountered type error in `lit_to_mir_constant" )
35- }
36- } ;
37-
38- Constant { span, user_ty : None , literal }
26+ this. as_constant ( & this. thir [ * value] )
3927 }
40- ExprKind :: NonHirLiteral { lit, ref user_ty } => {
41- let user_ty = user_ty. as_ref ( ) . map ( |user_ty| {
42- this. canonical_user_type_annotations . push ( CanonicalUserTypeAnnotation {
28+ _ => as_constant_inner (
29+ expr,
30+ |user_ty| {
31+ Some ( this. canonical_user_type_annotations . push ( CanonicalUserTypeAnnotation {
4332 span,
4433 user_ty : user_ty. clone ( ) ,
4534 inferred_ty : ty,
46- } )
47- } ) ;
48- let literal = ConstantKind :: Val ( ConstValue :: Scalar ( Scalar :: Int ( lit) ) , ty) ;
35+ } ) )
36+ } ,
37+ tcx,
38+ ) ,
39+ }
40+ }
41+ }
4942
50- Constant { span, user_ty : user_ty, literal }
51- }
52- ExprKind :: ZstLiteral { ref user_ty } => {
53- let user_ty = user_ty. as_ref ( ) . map ( |user_ty| {
54- this. canonical_user_type_annotations . push ( CanonicalUserTypeAnnotation {
55- span,
56- user_ty : user_ty. clone ( ) ,
57- inferred_ty : ty,
58- } )
59- } ) ;
60- let literal = ConstantKind :: Val ( ConstValue :: ZeroSized , ty) ;
43+ pub fn as_constant_inner < ' tcx > (
44+ expr : & Expr < ' tcx > ,
45+ push_cuta : impl FnMut ( & Box < CanonicalUserType < ' tcx > > ) -> Option < UserTypeAnnotationIndex > ,
46+ tcx : TyCtxt < ' tcx > ,
47+ ) -> Constant < ' tcx > {
48+ let Expr { ty, temp_lifetime : _, span, ref kind } = * expr;
49+ match * kind {
50+ ExprKind :: Literal { lit, neg } => {
51+ let literal =
52+ match lit_to_mir_constant ( tcx, LitToConstInput { lit : & lit. node , ty, neg } ) {
53+ Ok ( c) => c,
54+ Err ( LitToConstError :: Reported ( guar) ) => {
55+ ConstantKind :: Ty ( tcx. const_error_with_guaranteed ( ty, guar) )
56+ }
57+ Err ( LitToConstError :: TypeError ) => {
58+ bug ! ( "encountered type error in `lit_to_mir_constant" )
59+ }
60+ } ;
6161
62- Constant { span, user_ty : user_ty, literal }
63- }
64- ExprKind :: NamedConst { def_id, substs, ref user_ty } => {
65- let user_ty = user_ty. as_ref ( ) . map ( |user_ty| {
66- this. canonical_user_type_annotations . push ( CanonicalUserTypeAnnotation {
67- span,
68- user_ty : user_ty. clone ( ) ,
69- inferred_ty : ty,
70- } )
71- } ) ;
62+ Constant { span, user_ty : None , literal }
63+ }
64+ ExprKind :: NonHirLiteral { lit, ref user_ty } => {
65+ let user_ty = user_ty. as_ref ( ) . map ( push_cuta) . flatten ( ) ;
7266
73- let uneval =
74- mir:: UnevaluatedConst :: new ( ty:: WithOptConstParam :: unknown ( def_id) , substs) ;
75- let literal = ConstantKind :: Unevaluated ( uneval, ty) ;
67+ let literal = ConstantKind :: Val ( ConstValue :: Scalar ( Scalar :: Int ( lit) ) , ty) ;
7668
77- Constant { user_ty, span, literal }
78- }
79- ExprKind :: ConstParam { param, def_id : _ } => {
80- let const_param = tcx. mk_const ( param, expr. ty ) ;
81- let literal = ConstantKind :: Ty ( const_param) ;
69+ Constant { span, user_ty : user_ty, literal }
70+ }
71+ ExprKind :: ZstLiteral { ref user_ty } => {
72+ let user_ty = user_ty. as_ref ( ) . map ( push_cuta) . flatten ( ) ;
8273
83- Constant { user_ty : None , span, literal }
84- }
85- ExprKind :: ConstBlock { did : def_id, substs } => {
86- let uneval =
87- mir:: UnevaluatedConst :: new ( ty:: WithOptConstParam :: unknown ( def_id) , substs) ;
88- let literal = ConstantKind :: Unevaluated ( uneval, ty) ;
74+ let literal = ConstantKind :: Val ( ConstValue :: ZeroSized , ty) ;
8975
90- Constant { user_ty : None , span, literal }
91- }
92- ExprKind :: StaticRef { alloc_id, ty, .. } => {
93- let const_val = ConstValue :: Scalar ( Scalar :: from_pointer ( alloc_id. into ( ) , & tcx) ) ;
94- let literal = ConstantKind :: Val ( const_val, ty) ;
76+ Constant { span, user_ty : user_ty, literal }
77+ }
78+ ExprKind :: NamedConst { def_id, substs, ref user_ty } => {
79+ let user_ty = user_ty. as_ref ( ) . map ( push_cuta) . flatten ( ) ;
9580
96- Constant { span, user_ty : None , literal }
97- }
98- _ => span_bug ! ( span, "expression is not a valid constant {:?}" , kind) ,
81+ let uneval = mir:: UnevaluatedConst :: new ( ty:: WithOptConstParam :: unknown ( def_id) , substs) ;
82+ let literal = ConstantKind :: Unevaluated ( uneval, ty) ;
83+
84+ Constant { user_ty, span, literal }
85+ }
86+ ExprKind :: ConstParam { param, def_id : _ } => {
87+ let const_param = tcx. mk_const ( ty:: ConstKind :: Param ( param) , expr. ty ) ;
88+ let literal = ConstantKind :: Ty ( const_param) ;
89+
90+ Constant { user_ty : None , span, literal }
91+ }
92+ ExprKind :: ConstBlock { did : def_id, substs } => {
93+ let uneval = mir:: UnevaluatedConst :: new ( ty:: WithOptConstParam :: unknown ( def_id) , substs) ;
94+ let literal = ConstantKind :: Unevaluated ( uneval, ty) ;
95+
96+ Constant { user_ty : None , span, literal }
97+ }
98+ ExprKind :: StaticRef { alloc_id, ty, .. } => {
99+ let const_val = ConstValue :: Scalar ( Scalar :: from_pointer ( alloc_id. into ( ) , & tcx) ) ;
100+ let literal = ConstantKind :: Val ( const_val, ty) ;
101+
102+ Constant { span, user_ty : None , literal }
99103 }
104+ _ => span_bug ! ( span, "expression is not a valid constant {:?}" , kind) ,
100105 }
101106}
102107
0 commit comments