Skip to content

Commit a240ccd

Browse files
committed
Auto merge of rust-lang#93800 - b-naber:static-initializers-mir-val, r=oli-obk
Treat static refs as `mir::ConstantKind::Val` With the upcoming introduction of Valtrees we want to treat more values as `mir::ConstantKind::Val` directly. r? `@lcnr` cc `@oli-obk`
2 parents 393fdc1 + db019f2 commit a240ccd

18 files changed

+38
-64
lines changed

compiler/rustc_middle/src/mir/mod.rs

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

25232523
impl<'tcx> Constant<'tcx> {
25242524
pub fn check_static_ptr(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
2525-
match self.literal.const_for_ty()?.val().try_to_scalar() {
2525+
match self.literal.try_to_scalar() {
25262526
Some(Scalar::Ptr(ptr, _size)) => match tcx.global_alloc(ptr.provenance) {
25272527
GlobalAlloc::Static(def_id) => {
25282528
assert!(!tcx.is_thread_local_static(def_id));

compiler/rustc_middle/src/mir/pretty.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ use rustc_middle::mir::interpret::{
1717
use rustc_middle::mir::visit::Visitor;
1818
use rustc_middle::mir::MirSource;
1919
use rustc_middle::mir::*;
20-
use rustc_middle::ty::{self, TyCtxt, TypeFoldable, TypeVisitor};
20+
use rustc_middle::ty::{self, TyCtxt};
2121
use rustc_target::abi::Size;
22-
use std::ops::ControlFlow;
2322

2423
const INDENT: &str = " ";
2524
/// Alignment for lining up comments following MIR statements
@@ -669,6 +668,7 @@ pub fn write_allocations<'tcx>(
669668
fn alloc_ids_from_alloc(alloc: &Allocation) -> impl DoubleEndedIterator<Item = AllocId> + '_ {
670669
alloc.relocations().values().map(|id| *id)
671670
}
671+
672672
fn alloc_ids_from_const(val: ConstValue<'_>) -> impl Iterator<Item = AllocId> + '_ {
673673
match val {
674674
ConstValue::Scalar(interpret::Scalar::Ptr(ptr, _size)) => {
@@ -682,17 +682,29 @@ pub fn write_allocations<'tcx>(
682682
}
683683
}
684684
}
685+
685686
struct CollectAllocIds(BTreeSet<AllocId>);
686-
impl<'tcx> TypeVisitor<'tcx> for CollectAllocIds {
687-
fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
687+
688+
impl<'tcx> Visitor<'tcx> for CollectAllocIds {
689+
fn visit_const(&mut self, c: ty::Const<'tcx>, _loc: Location) {
688690
if let ty::ConstKind::Value(val) = c.val() {
689691
self.0.extend(alloc_ids_from_const(val));
690692
}
691-
c.super_visit_with(self)
693+
}
694+
695+
fn visit_constant(&mut self, c: &Constant<'tcx>, loc: Location) {
696+
match c.literal {
697+
ConstantKind::Ty(c) => self.visit_const(c, loc),
698+
ConstantKind::Val(val, _) => {
699+
self.0.extend(alloc_ids_from_const(val));
700+
}
701+
}
692702
}
693703
}
704+
694705
let mut visitor = CollectAllocIds(Default::default());
695-
body.visit_with(&mut visitor);
706+
visitor.visit_body(body);
707+
696708
// `seen` contains all seen allocations, including the ones we have *not* printed yet.
697709
// The protocol is to first `insert` into `seen`, and only if that returns `true`
698710
// 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 { .. } => {}
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::{
@@ -943,15 +942,8 @@ impl<'tcx> Cx<'tcx> {
943942
let kind = if self.tcx.is_thread_local_static(id) {
944943
ExprKind::ThreadLocalRef(id)
945944
} else {
946-
let ptr = self.tcx.create_static_alloc(id);
947-
ExprKind::StaticRef {
948-
literal: ty::Const::from_scalar(
949-
self.tcx,
950-
Scalar::from_pointer(ptr.into(), &self.tcx),
951-
ty,
952-
),
953-
def_id: id,
954-
}
945+
let alloc_id = self.tcx.create_static_alloc(id);
946+
ExprKind::StaticRef { alloc_id, ty, def_id: id }
955947
};
956948
ExprKind::Deref {
957949
arg: self.thir.exprs.push(Expr { ty, temp_lifetime, span: expr.span, kind }),

src/test/mir-opt/const_allocation.main.ConstProp.after.32bit.mir

+1-4
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,9 @@ fn main() -> () {
99
StorageLive(_1); // scope 0 at $DIR/const_allocation.rs:8:5: 8:8
1010
StorageLive(_2); // scope 0 at $DIR/const_allocation.rs:8:5: 8:8
1111
_2 = const {alloc1: &&[(Option<i32>, &[&str])]}; // scope 0 at $DIR/const_allocation.rs:8:5: 8:8
12-
// ty::Const
13-
// + ty: &&[(std::option::Option<i32>, &[&str])]
14-
// + val: Value(Scalar(alloc1))
1512
// mir::Constant
1613
// + span: $DIR/const_allocation.rs:8:5: 8:8
17-
// + literal: Const { ty: &&[(std::option::Option<i32>, &[&str])], val: Value(Scalar(alloc1)) }
14+
// + literal: Const { ty: &&[(Option<i32>, &[&str])], val: Value(Scalar(alloc1)) }
1815
_1 = (*_2); // scope 0 at $DIR/const_allocation.rs:8:5: 8:8
1916
StorageDead(_2); // scope 0 at $DIR/const_allocation.rs:8:8: 8:9
2017
StorageDead(_1); // scope 0 at $DIR/const_allocation.rs:8:8: 8:9

src/test/mir-opt/const_allocation.main.ConstProp.after.64bit.mir

+1-4
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,9 @@ fn main() -> () {
99
StorageLive(_1); // scope 0 at $DIR/const_allocation.rs:8:5: 8:8
1010
StorageLive(_2); // scope 0 at $DIR/const_allocation.rs:8:5: 8:8
1111
_2 = const {alloc1: &&[(Option<i32>, &[&str])]}; // scope 0 at $DIR/const_allocation.rs:8:5: 8:8
12-
// ty::Const
13-
// + ty: &&[(std::option::Option<i32>, &[&str])]
14-
// + val: Value(Scalar(alloc1))
1512
// mir::Constant
1613
// + span: $DIR/const_allocation.rs:8:5: 8:8
17-
// + literal: Const { ty: &&[(std::option::Option<i32>, &[&str])], val: Value(Scalar(alloc1)) }
14+
// + literal: Const { ty: &&[(Option<i32>, &[&str])], val: Value(Scalar(alloc1)) }
1815
_1 = (*_2); // scope 0 at $DIR/const_allocation.rs:8:5: 8:8
1916
StorageDead(_2); // scope 0 at $DIR/const_allocation.rs:8:8: 8:9
2017
StorageDead(_1); // scope 0 at $DIR/const_allocation.rs:8:8: 8:9

src/test/mir-opt/const_allocation2.main.ConstProp.after.32bit.mir

+1-4
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,9 @@ fn main() -> () {
99
StorageLive(_1); // scope 0 at $DIR/const_allocation2.rs:5:5: 5:8
1010
StorageLive(_2); // scope 0 at $DIR/const_allocation2.rs:5:5: 5:8
1111
_2 = const {alloc1: &&[(Option<i32>, &[&u8])]}; // scope 0 at $DIR/const_allocation2.rs:5:5: 5:8
12-
// ty::Const
13-
// + ty: &&[(std::option::Option<i32>, &[&u8])]
14-
// + val: Value(Scalar(alloc1))
1512
// mir::Constant
1613
// + span: $DIR/const_allocation2.rs:5:5: 5:8
17-
// + literal: Const { ty: &&[(std::option::Option<i32>, &[&u8])], val: Value(Scalar(alloc1)) }
14+
// + literal: Const { ty: &&[(Option<i32>, &[&u8])], val: Value(Scalar(alloc1)) }
1815
_1 = (*_2); // scope 0 at $DIR/const_allocation2.rs:5:5: 5:8
1916
StorageDead(_2); // scope 0 at $DIR/const_allocation2.rs:5:8: 5:9
2017
StorageDead(_1); // scope 0 at $DIR/const_allocation2.rs:5:8: 5:9

src/test/mir-opt/const_allocation2.main.ConstProp.after.64bit.mir

+1-4
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,9 @@ fn main() -> () {
99
StorageLive(_1); // scope 0 at $DIR/const_allocation2.rs:5:5: 5:8
1010
StorageLive(_2); // scope 0 at $DIR/const_allocation2.rs:5:5: 5:8
1111
_2 = const {alloc1: &&[(Option<i32>, &[&u8])]}; // scope 0 at $DIR/const_allocation2.rs:5:5: 5:8
12-
// ty::Const
13-
// + ty: &&[(std::option::Option<i32>, &[&u8])]
14-
// + val: Value(Scalar(alloc1))
1512
// mir::Constant
1613
// + span: $DIR/const_allocation2.rs:5:5: 5:8
17-
// + literal: Const { ty: &&[(std::option::Option<i32>, &[&u8])], val: Value(Scalar(alloc1)) }
14+
// + literal: Const { ty: &&[(Option<i32>, &[&u8])], val: Value(Scalar(alloc1)) }
1815
_1 = (*_2); // scope 0 at $DIR/const_allocation2.rs:5:5: 5:8
1916
StorageDead(_2); // scope 0 at $DIR/const_allocation2.rs:5:8: 5:9
2017
StorageDead(_1); // scope 0 at $DIR/const_allocation2.rs:5:8: 5:9

src/test/mir-opt/const_allocation3.main.ConstProp.after.32bit.mir

-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ fn main() -> () {
99
StorageLive(_1); // scope 0 at $DIR/const_allocation3.rs:5:5: 5:8
1010
StorageLive(_2); // scope 0 at $DIR/const_allocation3.rs:5:5: 5:8
1111
_2 = const {alloc1: &&Packed}; // scope 0 at $DIR/const_allocation3.rs:5:5: 5:8
12-
// ty::Const
13-
// + ty: &&Packed
14-
// + val: Value(Scalar(alloc1))
1512
// mir::Constant
1613
// + span: $DIR/const_allocation3.rs:5:5: 5:8
1714
// + literal: Const { ty: &&Packed, val: Value(Scalar(alloc1)) }

src/test/mir-opt/const_allocation3.main.ConstProp.after.64bit.mir

-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ fn main() -> () {
99
StorageLive(_1); // scope 0 at $DIR/const_allocation3.rs:5:5: 5:8
1010
StorageLive(_2); // scope 0 at $DIR/const_allocation3.rs:5:5: 5:8
1111
_2 = const {alloc1: &&Packed}; // scope 0 at $DIR/const_allocation3.rs:5:5: 5:8
12-
// ty::Const
13-
// + ty: &&Packed
14-
// + val: Value(Scalar(alloc1))
1512
// mir::Constant
1613
// + span: $DIR/const_allocation3.rs:5:5: 5:8
1714
// + literal: Const { ty: &&Packed, val: Value(Scalar(alloc1)) }

src/test/mir-opt/const_promotion_extern_static.BAR-promoted[0].SimplifyCfg-elaborate-drops.after.mir

-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ promoted[0] in BAR: &[&i32; 1] = {
88

99
bb0: {
1010
_3 = const {alloc1: &i32}; // scope 0 at $DIR/const-promotion-extern-static.rs:9:33: 9:34
11-
// ty::Const
12-
// + ty: &i32
13-
// + val: Value(Scalar(alloc1))
1411
// mir::Constant
1512
// + span: $DIR/const-promotion-extern-static.rs:9:33: 9:34
1613
// + literal: Const { ty: &i32, val: Value(Scalar(alloc1)) }

src/test/mir-opt/const_promotion_extern_static.BAR.PromoteTemps.diff

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
- StorageLive(_5); // scope 0 at $DIR/const-promotion-extern-static.rs:9:33: 9:34
1919
- _5 = const {alloc1: &i32}; // scope 0 at $DIR/const-promotion-extern-static.rs:9:33: 9:34
2020
+ _6 = const BAR::promoted[0]; // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:44
21-
// ty::Const
22-
- // + ty: &i32
23-
- // + val: Value(Scalar(alloc1))
21+
+ // ty::Const
2422
+ // + ty: &[&i32; 1]
2523
+ // + val: Unevaluated(BAR, [], Some(promoted[0]))
2624
// mir::Constant

src/test/mir-opt/const_promotion_extern_static.FOO-promoted[0].SimplifyCfg-elaborate-drops.after.mir

-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ promoted[0] in FOO: &[&i32; 1] = {
88

99
bb0: {
1010
_3 = const {alloc3: *const i32}; // scope 0 at $DIR/const-promotion-extern-static.rs:13:42: 13:43
11-
// ty::Const
12-
// + ty: *const i32
13-
// + val: Value(Scalar(alloc3))
1411
// mir::Constant
1512
// + span: $DIR/const-promotion-extern-static.rs:13:42: 13:43
1613
// + literal: Const { ty: *const i32, val: Value(Scalar(alloc3)) }

src/test/mir-opt/const_promotion_extern_static.FOO.PromoteTemps.diff

+1-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
- StorageLive(_5); // scope 1 at $DIR/const-promotion-extern-static.rs:13:42: 13:43
2121
- _5 = const {alloc3: *const i32}; // scope 1 at $DIR/const-promotion-extern-static.rs:13:42: 13:43
2222
+ _6 = const FOO::promoted[0]; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:55
23-
// ty::Const
24-
- // + ty: *const i32
25-
- // + val: Value(Scalar(alloc3))
23+
+ // ty::Const
2624
+ // + ty: &[&i32; 1]
2725
+ // + val: Unevaluated(FOO, [], Some(promoted[0]))
2826
// mir::Constant

src/test/mir-opt/const_prop/mutable_variable_no_prop.main.ConstProp.diff

-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
StorageLive(_3); // scope 2 at $DIR/mutable_variable_no_prop.rs:9:13: 9:19
2525
StorageLive(_4); // scope 2 at $DIR/mutable_variable_no_prop.rs:9:13: 9:19
2626
_4 = const {alloc1: *mut u32}; // scope 2 at $DIR/mutable_variable_no_prop.rs:9:13: 9:19
27-
// ty::Const
28-
// + ty: *mut u32
29-
// + val: Value(Scalar(alloc1))
3027
// mir::Constant
3128
// + span: $DIR/mutable_variable_no_prop.rs:9:13: 9:19
3229
// + literal: Const { ty: *mut u32, val: Value(Scalar(alloc1)) }

src/test/mir-opt/const_prop/read_immutable_static.main.ConstProp.diff

-6
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
StorageLive(_2); // scope 0 at $DIR/read_immutable_static.rs:7:13: 7:16
1818
StorageLive(_3); // scope 0 at $DIR/read_immutable_static.rs:7:13: 7:16
1919
_3 = const {alloc1: &u8}; // scope 0 at $DIR/read_immutable_static.rs:7:13: 7:16
20-
// ty::Const
21-
// + ty: &u8
22-
// + val: Value(Scalar(alloc1))
2320
// mir::Constant
2421
// + span: $DIR/read_immutable_static.rs:7:13: 7:16
2522
// + literal: Const { ty: &u8, val: Value(Scalar(alloc1)) }
@@ -28,9 +25,6 @@
2825
StorageLive(_4); // scope 0 at $DIR/read_immutable_static.rs:7:19: 7:22
2926
StorageLive(_5); // scope 0 at $DIR/read_immutable_static.rs:7:19: 7:22
3027
_5 = const {alloc1: &u8}; // scope 0 at $DIR/read_immutable_static.rs:7:19: 7:22
31-
// ty::Const
32-
// + ty: &u8
33-
// + val: Value(Scalar(alloc1))
3428
// mir::Constant
3529
// + span: $DIR/read_immutable_static.rs:7:19: 7:22
3630
// + literal: Const { ty: &u8, val: Value(Scalar(alloc1)) }

0 commit comments

Comments
 (0)