Skip to content

Commit a1d04cc

Browse files
oli-obkmatthewjasper
authored andcommitted
Remove statics from HAIR by lowering them to a pointer constant
1 parent 35ef33a commit a1d04cc

File tree

6 files changed

+26
-15
lines changed

6 files changed

+26
-15
lines changed

src/librustc_mir/build/expr/as_place.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
197197
};
198198
block.and(place_builder)
199199
}
200-
ExprKind::StaticRef { id } => block.and(PlaceBuilder::from(
201-
PlaceBase::Static(Box::new(Static {
202-
ty: expr.ty,
203-
kind: StaticKind::Static,
204-
def_id: id,
205-
}))
206-
)),
207200

208201
ExprKind::PlaceTypeAscription { source, user_ty } => {
209202
let source = this.hir.mirror(source);

src/librustc_mir/build/expr/as_rvalue.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
288288
| ExprKind::Continue { .. }
289289
| ExprKind::Return { .. }
290290
| ExprKind::InlineAsm { .. }
291-
| ExprKind::StaticRef { .. }
292291
| ExprKind::PlaceTypeAscription { .. }
293292
| ExprKind::ValueTypeAscription { .. } => {
294293
// these do not have corresponding `Rvalue` variants,

src/librustc_mir/build/expr/category.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ impl Category {
4040
| ExprKind::Index { .. }
4141
| ExprKind::SelfRef
4242
| ExprKind::VarRef { .. }
43-
| ExprKind::StaticRef { .. }
4443
| ExprKind::PlaceTypeAscription { .. }
4544
| ExprKind::ValueTypeAscription { .. } => Some(Category::Place),
4645

src/librustc_mir/build/expr/into.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
384384
// Avoid creating a temporary
385385
ExprKind::VarRef { .. } |
386386
ExprKind::SelfRef |
387-
ExprKind::StaticRef { .. } |
388387
ExprKind::PlaceTypeAscription { .. } |
389388
ExprKind::ValueTypeAscription { .. } => {
390389
debug_assert!(Category::of(&expr.kind) == Some(Category::Place));

src/librustc_mir/hair/cx/expr.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::hair::cx::to_ref::ToRef;
55
use crate::hair::util::UserAnnotatedTyHelpers;
66
use rustc_index::vec::Idx;
77
use rustc::hir::def::{CtorOf, Res, DefKind, CtorKind};
8-
use rustc::mir::interpret::{GlobalId, ErrorHandled};
8+
use rustc::mir::interpret::{GlobalId, ErrorHandled, ConstValue, Scalar};
99
use rustc::ty::{self, AdtKind, Ty};
1010
use rustc::ty::adjustment::{Adjustment, Adjust, AutoBorrow, AutoBorrowMutability, PointerCast};
1111
use 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

src/librustc_mir/hair/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,6 @@ pub enum ExprKind<'tcx> {
208208
},
209209
/// first argument, used for self in a closure
210210
SelfRef,
211-
StaticRef {
212-
id: DefId,
213-
},
214211
Borrow {
215212
borrow_kind: BorrowKind,
216213
arg: ExprRef<'tcx>,

0 commit comments

Comments
 (0)