@@ -8,7 +8,9 @@ use rustc_middle::mir::interpret::{
8
8
} ;
9
9
use rustc_middle:: mir:: * ;
10
10
use rustc_middle:: thir:: * ;
11
- use rustc_middle:: ty:: { self , CanonicalUserTypeAnnotation , TyCtxt } ;
11
+ use rustc_middle:: ty:: {
12
+ self , CanonicalUserType , CanonicalUserTypeAnnotation , TyCtxt , UserTypeAnnotationIndex ,
13
+ } ;
12
14
use rustc_span:: DUMMY_SP ;
13
15
use rustc_target:: abi:: Size ;
14
16
@@ -19,84 +21,87 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
19
21
let this = self ;
20
22
let tcx = this. tcx ;
21
23
let Expr { ty, temp_lifetime : _, span, ref kind } = * expr;
22
- match * kind {
24
+ match kind {
23
25
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] )
39
27
}
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 {
43
32
span,
44
33
user_ty : user_ty. clone ( ) ,
45
34
inferred_ty : ty,
46
- } )
47
- } ) ;
48
- let literal = ConstantKind :: Val ( ConstValue :: Scalar ( Scalar :: Int ( lit) ) , ty) ;
35
+ } ) )
36
+ } ,
37
+ tcx,
38
+ ) ,
39
+ }
40
+ }
41
+ }
49
42
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
+ } ;
61
61
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 ( ) ;
72
66
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) ;
76
68
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 ( ) ;
82
73
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) ;
89
75
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 ( ) ;
95
80
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 }
99
103
}
104
+ _ => span_bug ! ( span, "expression is not a valid constant {:?}" , kind) ,
100
105
}
101
106
}
102
107
0 commit comments