Skip to content

Commit

Permalink
Auto merge of rust-lang#94702 - b-naber:static-refs-mir, r=lcnr
Browse files Browse the repository at this point in the history
Reinstate rust-lang#93800

rust-lang#93800 caused a regression in an alt builder with parallel enabled. rust-lang#94205 reverted that PR because of the regression. For an unknown reason the regression has disappeared, so we reinstate the changes in rust-lang#93800 here.

r? `@Mark-Simulacrum`
  • Loading branch information
bors committed Mar 8, 2022
2 parents 64187b8 + 9fd2c80 commit 1eb7258
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 21 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2534,7 +2534,7 @@ pub enum ConstantKind<'tcx> {

impl<'tcx> Constant<'tcx> {
pub fn check_static_ptr(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
match self.literal.const_for_ty()?.val().try_to_scalar() {
match self.literal.try_to_scalar() {
Some(Scalar::Ptr(ptr, _size)) => match tcx.global_alloc(ptr.provenance) {
GlobalAlloc::Static(def_id) => {
assert!(!tcx.is_thread_local_static(def_id));
Expand Down
22 changes: 16 additions & 6 deletions compiler/rustc_middle/src/mir/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ use rustc_middle::mir::interpret::{
use rustc_middle::mir::visit::Visitor;
use rustc_middle::mir::MirSource;
use rustc_middle::mir::*;
use rustc_middle::ty::{self, TyCtxt, TypeFoldable, TypeVisitor};
use rustc_middle::ty::{self, TyCtxt};
use rustc_target::abi::Size;
use std::ops::ControlFlow;

const INDENT: &str = " ";
/// Alignment for lining up comments following MIR statements
Expand Down Expand Up @@ -672,16 +671,27 @@ pub fn write_allocations<'tcx>(
}
}
struct CollectAllocIds(BTreeSet<AllocId>);
impl<'tcx> TypeVisitor<'tcx> for CollectAllocIds {
fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {

impl<'tcx> Visitor<'tcx> for CollectAllocIds {
fn visit_const(&mut self, c: ty::Const<'tcx>, _loc: Location) {
if let ty::ConstKind::Value(val) = c.val() {
self.0.extend(alloc_ids_from_const(val));
}
c.super_visit_with(self)
}

fn visit_constant(&mut self, c: &Constant<'tcx>, loc: Location) {
match c.literal {
ConstantKind::Ty(c) => self.visit_const(c, loc),
ConstantKind::Val(val, _) => {
self.0.extend(alloc_ids_from_const(val));
}
}
}
}

let mut visitor = CollectAllocIds(Default::default());
body.visit_with(&mut visitor);
visitor.visit_body(body);

// `seen` contains all seen allocations, including the ones we have *not* printed yet.
// The protocol is to first `insert` into `seen`, and only if that returns `true`
// then push to `todo`.
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_middle/src/thir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use rustc_index::newtype_index;
use rustc_index::vec::IndexVec;
use rustc_middle::infer::canonical::Canonical;
use rustc_middle::middle::region;
use rustc_middle::mir::interpret::AllocId;
use rustc_middle::mir::{
BinOp, BorrowKind, FakeReadCause, Field, Mutability, UnOp, UserTypeProjection,
};
Expand Down Expand Up @@ -419,7 +420,8 @@ pub enum ExprKind<'tcx> {
/// This is only distinguished from `Literal` so that we can register some
/// info for diagnostics.
StaticRef {
literal: Const<'tcx>,
alloc_id: AllocId,
ty: Ty<'tcx>,
def_id: DefId,
},
/// Inline assembly, i.e. `asm!()`.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/thir/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Exp
}
Closure { closure_id: _, substs: _, upvars: _, movability: _, fake_reads: _ } => {}
Literal { literal, user_ty: _, const_id: _ } => visitor.visit_const(literal),
StaticRef { literal, def_id: _ } => visitor.visit_const(literal),
StaticRef { alloc_id: _, ty: _, def_id: _ } => {}
InlineAsm { ref operands, template: _, options: _, line_spans: _ } => {
for op in &**operands {
use InlineAsmOperand::*;
Expand Down
9 changes: 7 additions & 2 deletions compiler/rustc_mir_build/src/build/expr/as_constant.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! See docs in build/expr/mod.rs
use crate::build::Builder;
use rustc_middle::mir::interpret::{ConstValue, Scalar};
use rustc_middle::mir::*;
use rustc_middle::thir::*;
use rustc_middle::ty::CanonicalUserTypeAnnotation;
Expand All @@ -26,8 +27,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
assert_eq!(literal.ty(), ty);
Constant { span, user_ty, literal: literal.into() }
}
ExprKind::StaticRef { literal, .. } => {
Constant { span, user_ty: None, literal: literal.into() }
ExprKind::StaticRef { alloc_id, ty, .. } => {
let const_val =
ConstValue::Scalar(Scalar::from_pointer(alloc_id.into(), &this.tcx));
let literal = ConstantKind::Val(const_val, ty);

Constant { span, user_ty: None, literal }
}
ExprKind::ConstBlock { value } => {
Constant { span: span, user_ty: None, literal: value.into() }
Expand Down
12 changes: 2 additions & 10 deletions compiler/rustc_mir_build/src/thir/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use rustc_middle::hir::place::Place as HirPlace;
use rustc_middle::hir::place::PlaceBase as HirPlaceBase;
use rustc_middle::hir::place::ProjectionKind as HirProjectionKind;
use rustc_middle::middle::region;
use rustc_middle::mir::interpret::Scalar;
use rustc_middle::mir::{BinOp, BorrowKind, Field, UnOp};
use rustc_middle::thir::*;
use rustc_middle::ty::adjustment::{
Expand Down Expand Up @@ -941,15 +940,8 @@ impl<'tcx> Cx<'tcx> {
let kind = if self.tcx.is_thread_local_static(id) {
ExprKind::ThreadLocalRef(id)
} else {
let ptr = self.tcx.create_static_alloc(id);
ExprKind::StaticRef {
literal: ty::Const::from_scalar(
self.tcx,
Scalar::from_pointer(ptr.into(), &self.tcx),
ty,
),
def_id: id,
}
let alloc_id = self.tcx.create_static_alloc(id);
ExprKind::StaticRef { alloc_id, ty, def_id: id }
};
ExprKind::Deref {
arg: self.thir.exprs.push(Expr { ty, temp_lifetime, span: expr.span, kind }),
Expand Down

0 comments on commit 1eb7258

Please sign in to comment.