11//! See docs in build/expr/mod.rs
22
3- use rustc_abi:: { BackendRepr , Size } ;
3+ use rustc_abi:: Size ;
44use rustc_ast as ast;
55use rustc_hir:: LangItem ;
66use rustc_middle:: mir:: interpret:: { CTFE_ALLOC_SALT , LitToConstInput , Scalar } ;
77use rustc_middle:: mir:: * ;
88use rustc_middle:: thir:: * ;
99use rustc_middle:: ty:: {
1010 self , CanonicalUserType , CanonicalUserTypeAnnotation , Ty , TyCtxt , TypeVisitableExt as _,
11- TypingEnv , UserTypeAnnotationIndex ,
11+ UserTypeAnnotationIndex ,
1212} ;
1313use rustc_middle:: { bug, mir, span_bug} ;
1414use tracing:: { instrument, trace} ;
@@ -19,27 +19,32 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1919 /// Compile `expr`, yielding a compile-time constant. Assumes that
2020 /// `expr` is a valid compile-time constant!
2121 pub ( crate ) fn as_constant ( & mut self , expr : & Expr < ' tcx > ) -> ConstOperand < ' tcx > {
22+ let this = self ;
23+ let tcx = this. tcx ;
2224 let Expr { ty, temp_lifetime : _, span, ref kind } = * expr;
2325 match kind {
2426 ExprKind :: Scope { region_scope : _, lint_level : _, value } => {
25- self . as_constant ( & self . thir [ * value] )
27+ this . as_constant ( & this . thir [ * value] )
2628 }
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+ ) ,
3440 }
3541 }
3642}
3743
3844pub ( crate ) fn as_constant_inner < ' tcx > (
39- tcx : TyCtxt < ' tcx > ,
40- typing_env : TypingEnv < ' tcx > ,
4145 expr : & Expr < ' tcx > ,
4246 push_cuta : impl FnMut ( & Box < CanonicalUserType < ' tcx > > ) -> Option < UserTypeAnnotationIndex > ,
47+ tcx : TyCtxt < ' tcx > ,
4348) -> ConstOperand < ' tcx > {
4449 let Expr { ty, temp_lifetime : _, span, ref kind } = * expr;
4550 match * kind {
@@ -83,23 +88,8 @@ pub(crate) fn as_constant_inner<'tcx>(
8388 ConstOperand { user_ty : None , span, const_ }
8489 }
8590 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) ;
10393
10494 ConstOperand { span, user_ty : None , const_ }
10595 }
0 commit comments