Skip to content

Commit 1eb7258

Browse files
committed
Auto merge of #94702 - b-naber:static-refs-mir, r=lcnr
Reinstate #93800 #93800 caused a regression in an alt builder with parallel enabled. #94205 reverted that PR because of the regression. For an unknown reason the regression has disappeared, so we reinstate the changes in #93800 here. r? `@Mark-Simulacrum`
2 parents 64187b8 + 9fd2c80 commit 1eb7258

File tree

6 files changed

+30
-21
lines changed

6 files changed

+30
-21
lines changed

compiler/rustc_middle/src/mir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2534,7 +2534,7 @@ pub enum ConstantKind<'tcx> {
25342534

25352535
impl<'tcx> Constant<'tcx> {
25362536
pub fn check_static_ptr(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
2537-
match self.literal.const_for_ty()?.val().try_to_scalar() {
2537+
match self.literal.try_to_scalar() {
25382538
Some(Scalar::Ptr(ptr, _size)) => match tcx.global_alloc(ptr.provenance) {
25392539
GlobalAlloc::Static(def_id) => {
25402540
assert!(!tcx.is_thread_local_static(def_id));

compiler/rustc_middle/src/mir/pretty.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ use rustc_middle::mir::interpret::{
1818
use rustc_middle::mir::visit::Visitor;
1919
use rustc_middle::mir::MirSource;
2020
use rustc_middle::mir::*;
21-
use rustc_middle::ty::{self, TyCtxt, TypeFoldable, TypeVisitor};
21+
use rustc_middle::ty::{self, TyCtxt};
2222
use rustc_target::abi::Size;
23-
use std::ops::ControlFlow;
2423

2524
const INDENT: &str = " ";
2625
/// Alignment for lining up comments following MIR statements
@@ -672,16 +671,27 @@ pub fn write_allocations<'tcx>(
672671
}
673672
}
674673
struct CollectAllocIds(BTreeSet<AllocId>);
675-
impl<'tcx> TypeVisitor<'tcx> for CollectAllocIds {
676-
fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
674+
675+
impl<'tcx> Visitor<'tcx> for CollectAllocIds {
676+
fn visit_const(&mut self, c: ty::Const<'tcx>, _loc: Location) {
677677
if let ty::ConstKind::Value(val) = c.val() {
678678
self.0.extend(alloc_ids_from_const(val));
679679
}
680-
c.super_visit_with(self)
680+
}
681+
682+
fn visit_constant(&mut self, c: &Constant<'tcx>, loc: Location) {
683+
match c.literal {
684+
ConstantKind::Ty(c) => self.visit_const(c, loc),
685+
ConstantKind::Val(val, _) => {
686+
self.0.extend(alloc_ids_from_const(val));
687+
}
688+
}
681689
}
682690
}
691+
683692
let mut visitor = CollectAllocIds(Default::default());
684-
body.visit_with(&mut visitor);
693+
visitor.visit_body(body);
694+
685695
// `seen` contains all seen allocations, including the ones we have *not* printed yet.
686696
// The protocol is to first `insert` into `seen`, and only if that returns `true`
687697
// then push to `todo`.

compiler/rustc_middle/src/thir.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc_index::newtype_index;
1717
use rustc_index::vec::IndexVec;
1818
use rustc_middle::infer::canonical::Canonical;
1919
use rustc_middle::middle::region;
20+
use rustc_middle::mir::interpret::AllocId;
2021
use rustc_middle::mir::{
2122
BinOp, BorrowKind, FakeReadCause, Field, Mutability, UnOp, UserTypeProjection,
2223
};
@@ -419,7 +420,8 @@ pub enum ExprKind<'tcx> {
419420
/// This is only distinguished from `Literal` so that we can register some
420421
/// info for diagnostics.
421422
StaticRef {
422-
literal: Const<'tcx>,
423+
alloc_id: AllocId,
424+
ty: Ty<'tcx>,
423425
def_id: DefId,
424426
},
425427
/// Inline assembly, i.e. `asm!()`.

compiler/rustc_middle/src/thir/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Exp
123123
}
124124
Closure { closure_id: _, substs: _, upvars: _, movability: _, fake_reads: _ } => {}
125125
Literal { literal, user_ty: _, const_id: _ } => visitor.visit_const(literal),
126-
StaticRef { literal, def_id: _ } => visitor.visit_const(literal),
126+
StaticRef { alloc_id: _, ty: _, def_id: _ } => {}
127127
InlineAsm { ref operands, template: _, options: _, line_spans: _ } => {
128128
for op in &**operands {
129129
use InlineAsmOperand::*;

compiler/rustc_mir_build/src/build/expr/as_constant.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! See docs in build/expr/mod.rs
22
33
use crate::build::Builder;
4+
use rustc_middle::mir::interpret::{ConstValue, Scalar};
45
use rustc_middle::mir::*;
56
use rustc_middle::thir::*;
67
use rustc_middle::ty::CanonicalUserTypeAnnotation;
@@ -26,8 +27,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
2627
assert_eq!(literal.ty(), ty);
2728
Constant { span, user_ty, literal: literal.into() }
2829
}
29-
ExprKind::StaticRef { literal, .. } => {
30-
Constant { span, user_ty: None, literal: literal.into() }
30+
ExprKind::StaticRef { alloc_id, ty, .. } => {
31+
let const_val =
32+
ConstValue::Scalar(Scalar::from_pointer(alloc_id.into(), &this.tcx));
33+
let literal = ConstantKind::Val(const_val, ty);
34+
35+
Constant { span, user_ty: None, literal }
3136
}
3237
ExprKind::ConstBlock { value } => {
3338
Constant { span: span, user_ty: None, literal: value.into() }

compiler/rustc_mir_build/src/thir/cx/expr.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use rustc_middle::hir::place::Place as HirPlace;
88
use rustc_middle::hir::place::PlaceBase as HirPlaceBase;
99
use rustc_middle::hir::place::ProjectionKind as HirProjectionKind;
1010
use rustc_middle::middle::region;
11-
use rustc_middle::mir::interpret::Scalar;
1211
use rustc_middle::mir::{BinOp, BorrowKind, Field, UnOp};
1312
use rustc_middle::thir::*;
1413
use rustc_middle::ty::adjustment::{
@@ -941,15 +940,8 @@ impl<'tcx> Cx<'tcx> {
941940
let kind = if self.tcx.is_thread_local_static(id) {
942941
ExprKind::ThreadLocalRef(id)
943942
} else {
944-
let ptr = self.tcx.create_static_alloc(id);
945-
ExprKind::StaticRef {
946-
literal: ty::Const::from_scalar(
947-
self.tcx,
948-
Scalar::from_pointer(ptr.into(), &self.tcx),
949-
ty,
950-
),
951-
def_id: id,
952-
}
943+
let alloc_id = self.tcx.create_static_alloc(id);
944+
ExprKind::StaticRef { alloc_id, ty, def_id: id }
953945
};
954946
ExprKind::Deref {
955947
arg: self.thir.exprs.push(Expr { ty, temp_lifetime, span: expr.span, kind }),

0 commit comments

Comments
 (0)