@@ -5,7 +5,7 @@ use crate::hair::cx::to_ref::ToRef;
55use crate :: hair:: util:: UserAnnotatedTyHelpers ;
66use rustc_index:: vec:: Idx ;
77use rustc:: hir:: def:: { CtorOf , Res , DefKind , CtorKind } ;
8- use rustc:: mir:: interpret:: { GlobalId , ErrorHandled } ;
8+ use rustc:: mir:: interpret:: { GlobalId , ErrorHandled , ConstValue , Scalar } ;
99use rustc:: ty:: { self , AdtKind , Ty } ;
1010use rustc:: ty:: adjustment:: { Adjustment , Adjust , AutoBorrow , AutoBorrowMutability , PointerCast } ;
1111use rustc:: ty:: subst:: { InternalSubsts , SubstsRef } ;
@@ -961,7 +961,31 @@ fn convert_path_expr<'a, 'tcx>(
961961 }
962962 }
963963
964- Res :: Def ( DefKind :: Static , id) => ExprKind :: StaticRef { id } ,
964+ // We encode uses of statics as a `*&STATIC` where the `&STATIC` part is
965+ // a constant reference (or constant raw pointer for `static mut`) in MIR
966+ Res :: Def ( DefKind :: Static , id) => {
967+ let ty = cx. tcx . type_of ( id) ;
968+ let ty = if cx. tcx . is_mutable_static ( id) {
969+ cx. tcx . mk_mut_ptr ( ty)
970+ } else if cx. tcx . is_foreign_item ( id) {
971+ cx. tcx . mk_imm_ptr ( ty)
972+ } else {
973+ cx. tcx . mk_imm_ref ( cx. tcx . lifetimes . re_static , ty)
974+ } ;
975+ let ptr = cx. tcx . alloc_map . lock ( ) . create_static_alloc ( id) ;
976+ let temp_lifetime = cx. region_scope_tree . temporary_scope ( expr. hir_id . local_id ) ;
977+ ExprKind :: Deref { arg : Expr {
978+ ty,
979+ temp_lifetime,
980+ span : expr. span ,
981+ kind : ExprKind :: Literal {
982+ literal : cx. tcx . mk_const ( ty:: Const {
983+ ty, val : ConstValue :: Scalar ( Scalar :: Ptr ( ptr. into ( ) ) ) ,
984+ } ) ,
985+ user_ty : None ,
986+ }
987+ } . to_ref ( ) }
988+ } ,
965989
966990 Res :: Local ( var_hir_id) => convert_var ( cx, expr, var_hir_id) ,
967991
0 commit comments