Skip to content

Commit

Permalink
Remove statics from HAIR by lowering them to a pointer constant
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk authored and matthewjasper committed Nov 21, 2019
1 parent 35ef33a commit a1d04cc
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
7 changes: 0 additions & 7 deletions src/librustc_mir/build/expr/as_place.rs
Expand Up @@ -197,13 +197,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}; };
block.and(place_builder) block.and(place_builder)
} }
ExprKind::StaticRef { id } => block.and(PlaceBuilder::from(
PlaceBase::Static(Box::new(Static {
ty: expr.ty,
kind: StaticKind::Static,
def_id: id,
}))
)),


ExprKind::PlaceTypeAscription { source, user_ty } => { ExprKind::PlaceTypeAscription { source, user_ty } => {
let source = this.hir.mirror(source); let source = this.hir.mirror(source);
Expand Down
1 change: 0 additions & 1 deletion src/librustc_mir/build/expr/as_rvalue.rs
Expand Up @@ -288,7 +288,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
| ExprKind::Continue { .. } | ExprKind::Continue { .. }
| ExprKind::Return { .. } | ExprKind::Return { .. }
| ExprKind::InlineAsm { .. } | ExprKind::InlineAsm { .. }
| ExprKind::StaticRef { .. }
| ExprKind::PlaceTypeAscription { .. } | ExprKind::PlaceTypeAscription { .. }
| ExprKind::ValueTypeAscription { .. } => { | ExprKind::ValueTypeAscription { .. } => {
// these do not have corresponding `Rvalue` variants, // these do not have corresponding `Rvalue` variants,
Expand Down
1 change: 0 additions & 1 deletion src/librustc_mir/build/expr/category.rs
Expand Up @@ -40,7 +40,6 @@ impl Category {
| ExprKind::Index { .. } | ExprKind::Index { .. }
| ExprKind::SelfRef | ExprKind::SelfRef
| ExprKind::VarRef { .. } | ExprKind::VarRef { .. }
| ExprKind::StaticRef { .. }
| ExprKind::PlaceTypeAscription { .. } | ExprKind::PlaceTypeAscription { .. }
| ExprKind::ValueTypeAscription { .. } => Some(Category::Place), | ExprKind::ValueTypeAscription { .. } => Some(Category::Place),


Expand Down
1 change: 0 additions & 1 deletion src/librustc_mir/build/expr/into.rs
Expand Up @@ -384,7 +384,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// Avoid creating a temporary // Avoid creating a temporary
ExprKind::VarRef { .. } | ExprKind::VarRef { .. } |
ExprKind::SelfRef | ExprKind::SelfRef |
ExprKind::StaticRef { .. } |
ExprKind::PlaceTypeAscription { .. } | ExprKind::PlaceTypeAscription { .. } |
ExprKind::ValueTypeAscription { .. } => { ExprKind::ValueTypeAscription { .. } => {
debug_assert!(Category::of(&expr.kind) == Some(Category::Place)); debug_assert!(Category::of(&expr.kind) == Some(Category::Place));
Expand Down
28 changes: 26 additions & 2 deletions src/librustc_mir/hair/cx/expr.rs
Expand Up @@ -5,7 +5,7 @@ use crate::hair::cx::to_ref::ToRef;
use crate::hair::util::UserAnnotatedTyHelpers; use crate::hair::util::UserAnnotatedTyHelpers;
use rustc_index::vec::Idx; use rustc_index::vec::Idx;
use rustc::hir::def::{CtorOf, Res, DefKind, CtorKind}; use rustc::hir::def::{CtorOf, Res, DefKind, CtorKind};
use rustc::mir::interpret::{GlobalId, ErrorHandled}; use rustc::mir::interpret::{GlobalId, ErrorHandled, ConstValue, Scalar};
use rustc::ty::{self, AdtKind, Ty}; use rustc::ty::{self, AdtKind, Ty};
use rustc::ty::adjustment::{Adjustment, Adjust, AutoBorrow, AutoBorrowMutability, PointerCast}; use rustc::ty::adjustment::{Adjustment, Adjust, AutoBorrow, AutoBorrowMutability, PointerCast};
use rustc::ty::subst::{InternalSubsts, SubstsRef}; use rustc::ty::subst::{InternalSubsts, SubstsRef};
Expand Down Expand Up @@ -961,7 +961,31 @@ fn convert_path_expr<'a, 'tcx>(
} }
} }


Res::Def(DefKind::Static, id) => ExprKind::StaticRef { id }, // We encode uses of statics as a `*&STATIC` where the `&STATIC` part is
// a constant reference (or constant raw pointer for `static mut`) in MIR
Res::Def(DefKind::Static, id) => {
let ty = cx.tcx.type_of(id);
let ty = if cx.tcx.is_mutable_static(id) {
cx.tcx.mk_mut_ptr(ty)
} else if cx.tcx.is_foreign_item(id) {
cx.tcx.mk_imm_ptr(ty)
} else {
cx.tcx.mk_imm_ref(cx.tcx.lifetimes.re_static, ty)
};
let ptr = cx.tcx.alloc_map.lock().create_static_alloc(id);
let temp_lifetime = cx.region_scope_tree.temporary_scope(expr.hir_id.local_id);
ExprKind::Deref { arg: Expr {
ty,
temp_lifetime,
span: expr.span,
kind: ExprKind::Literal {
literal: cx.tcx.mk_const(ty::Const {
ty, val: ConstValue::Scalar(Scalar::Ptr(ptr.into())),
}),
user_ty: None,
}
}.to_ref() }
},


Res::Local(var_hir_id) => convert_var(cx, expr, var_hir_id), Res::Local(var_hir_id) => convert_var(cx, expr, var_hir_id),


Expand Down
3 changes: 0 additions & 3 deletions src/librustc_mir/hair/mod.rs
Expand Up @@ -208,9 +208,6 @@ pub enum ExprKind<'tcx> {
}, },
/// first argument, used for self in a closure /// first argument, used for self in a closure
SelfRef, SelfRef,
StaticRef {
id: DefId,
},
Borrow { Borrow {
borrow_kind: BorrowKind, borrow_kind: BorrowKind,
arg: ExprRef<'tcx>, arg: ExprRef<'tcx>,
Expand Down

0 comments on commit a1d04cc

Please sign in to comment.