1
1
//! See docs in build/expr/mod.rs
2
2
3
- use rustc_abi:: { BackendRepr , Size } ;
3
+ use rustc_abi:: Size ;
4
4
use rustc_ast as ast;
5
5
use rustc_hir:: LangItem ;
6
6
use rustc_middle:: mir:: interpret:: { CTFE_ALLOC_SALT , LitToConstInput , Scalar } ;
7
7
use rustc_middle:: mir:: * ;
8
8
use rustc_middle:: thir:: * ;
9
9
use rustc_middle:: ty:: {
10
10
self , CanonicalUserType , CanonicalUserTypeAnnotation , Ty , TyCtxt , TypeVisitableExt as _,
11
- TypingEnv , UserTypeAnnotationIndex ,
11
+ UserTypeAnnotationIndex ,
12
12
} ;
13
13
use rustc_middle:: { bug, mir, span_bug} ;
14
14
use tracing:: { instrument, trace} ;
@@ -19,27 +19,32 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
19
19
/// Compile `expr`, yielding a compile-time constant. Assumes that
20
20
/// `expr` is a valid compile-time constant!
21
21
pub ( crate ) fn as_constant ( & mut self , expr : & Expr < ' tcx > ) -> ConstOperand < ' tcx > {
22
+ let this = self ;
23
+ let tcx = this. tcx ;
22
24
let Expr { ty, temp_lifetime : _, span, ref kind } = * expr;
23
25
match kind {
24
26
ExprKind :: Scope { region_scope : _, lint_level : _, value } => {
25
- self . as_constant ( & self . thir [ * value] )
27
+ this . as_constant ( & this . thir [ * value] )
26
28
}
27
- _ => as_constant_inner ( self . tcx , self . typing_env ( ) , expr, |user_ty| {
28
- Some ( self . canonical_user_type_annotations . push ( CanonicalUserTypeAnnotation {
29
- span,
30
- user_ty : user_ty. clone ( ) ,
31
- inferred_ty : ty,
32
- } ) )
33
- } ) ,
29
+ _ => as_constant_inner (
30
+ expr,
31
+ |user_ty| {
32
+ Some ( this. canonical_user_type_annotations . push ( CanonicalUserTypeAnnotation {
33
+ span,
34
+ user_ty : user_ty. clone ( ) ,
35
+ inferred_ty : ty,
36
+ } ) )
37
+ } ,
38
+ tcx,
39
+ ) ,
34
40
}
35
41
}
36
42
}
37
43
38
44
pub ( crate ) fn as_constant_inner < ' tcx > (
39
- tcx : TyCtxt < ' tcx > ,
40
- typing_env : TypingEnv < ' tcx > ,
41
45
expr : & Expr < ' tcx > ,
42
46
push_cuta : impl FnMut ( & Box < CanonicalUserType < ' tcx > > ) -> Option < UserTypeAnnotationIndex > ,
47
+ tcx : TyCtxt < ' tcx > ,
43
48
) -> ConstOperand < ' tcx > {
44
49
let Expr { ty, temp_lifetime : _, span, ref kind } = * expr;
45
50
match * kind {
@@ -83,23 +88,8 @@ pub(crate) fn as_constant_inner<'tcx>(
83
88
ConstOperand { user_ty : None , span, const_ }
84
89
}
85
90
ExprKind :: StaticRef { alloc_id, ty, .. } => {
86
- let layout = tcx. layout_of ( typing_env. as_query_input ( ty) ) ;
87
- let const_ = if let Ok ( layout) = layout
88
- && let BackendRepr :: Scalar ( ..) = layout. backend_repr
89
- {
90
- let const_val = ConstValue :: Scalar ( Scalar :: from_pointer ( alloc_id. into ( ) , & tcx) ) ;
91
- Const :: Val ( const_val, ty)
92
- } else {
93
- // Ill-formed code may produce instances where `pointee` is not `Sized`.
94
- // This should be reported by wfcheck on the static itself.
95
- // Still, producing a single scalar constant would be inconsistent, as pointers to
96
- // non-`Sized` types are scalar pairs. Avoid an ICE by producing an error constant.
97
- let guar = tcx. dcx ( ) . span_delayed_bug (
98
- span,
99
- format ! ( "pointer to static's type `{ty}` is not scalar" ) ,
100
- ) ;
101
- Const :: Ty ( ty, ty:: Const :: new_error ( tcx, guar) )
102
- } ;
91
+ let const_val = ConstValue :: Scalar ( Scalar :: from_pointer ( alloc_id. into ( ) , & tcx) ) ;
92
+ let const_ = Const :: Val ( const_val, ty) ;
103
93
104
94
ConstOperand { span, user_ty : None , const_ }
105
95
}
0 commit comments