Skip to content

Commit 6452d85

Browse files
borsMark-Simulacrum
authored andcommitted
Auto merge of #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`
1 parent 0b54132 commit 6452d85

File tree

8 files changed

+44
-33
lines changed

8 files changed

+44
-33
lines changed

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 18 additions & 6 deletions
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
@@ -655,6 +654,7 @@ pub fn write_allocations<'tcx>(
655654
fn alloc_ids_from_alloc(alloc: &Allocation) -> impl DoubleEndedIterator<Item = AllocId> + '_ {
656655
alloc.relocations().values().map(|id| *id)
657656
}
657+
658658
fn alloc_ids_from_const(val: ConstValue<'_>) -> impl Iterator<Item = AllocId> + '_ {
659659
match val {
660660
ConstValue::Scalar(interpret::Scalar::Ptr(ptr, _size)) => {
@@ -668,17 +668,29 @@ pub fn write_allocations<'tcx>(
668668
}
669669
}
670670
}
671+
671672
struct CollectAllocIds(BTreeSet<AllocId>);
672-
impl<'tcx> TypeVisitor<'tcx> for CollectAllocIds {
673-
fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
673+
674+
impl<'tcx> Visitor<'tcx> for CollectAllocIds {
675+
fn visit_const(&mut self, c: ty::Const<'tcx>, _loc: Location) {
674676
if let ty::ConstKind::Value(val) = c.val() {
675677
self.0.extend(alloc_ids_from_const(val));
676678
}
677-
c.super_visit_with(self)
679+
}
680+
681+
fn visit_constant(&mut self, c: &Constant<'tcx>, loc: Location) {
682+
match c.literal {
683+
ConstantKind::Ty(c) => self.visit_const(c, loc),
684+
ConstantKind::Val(val, _) => {
685+
self.0.extend(alloc_ids_from_const(val));
686+
}
687+
}
678688
}
679689
}
690+
680691
let mut visitor = CollectAllocIds(Default::default());
681-
body.visit_with(&mut visitor);
692+
visitor.visit_body(body);
693+
682694
// `seen` contains all seen allocations, including the ones we have *not* printed yet.
683695
// The protocol is to first `insert` into `seen`, and only if that returns `true`
684696
// then push to `todo`.

compiler/rustc_middle/src/thir.rs

Lines changed: 3 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 7 additions & 2 deletions
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

Lines changed: 2 additions & 10 deletions
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 }),

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
- // MIR for `BAR` before PromoteTemps
22
+ // MIR for `BAR` after PromoteTemps
3-
3+
44
static mut BAR: *const &i32 = {
55
let mut _0: *const &i32; // return place in scope 0 at $DIR/const-promotion-extern-static.rs:9:17: 9:28
66
let mut _1: &[&i32]; // in scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:44
@@ -9,7 +9,7 @@
99
let mut _4: &i32; // in scope 0 at $DIR/const-promotion-extern-static.rs:9:32: 9:34
1010
let _5: &i32; // in scope 0 at $DIR/const-promotion-extern-static.rs:9:33: 9:34
1111
+ let mut _6: &[&i32; 1]; // in scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:44
12-
12+
1313
bb0: {
1414
StorageLive(_1); // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:44
1515
StorageLive(_2); // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:44
@@ -35,20 +35,20 @@
3535
// + span: $DIR/const-promotion-extern-static.rs:9:36: 9:42
3636
// + literal: Const { ty: for<'r> fn(&'r [&i32]) -> *const &i32 {core::slice::<impl [&i32]>::as_ptr}, val: Value(Scalar(<ZST>)) }
3737
}
38-
38+
3939
bb1: {
4040
- StorageDead(_5); // scope 0 at $DIR/const-promotion-extern-static.rs:9:43: 9:44
4141
- StorageDead(_3); // scope 0 at $DIR/const-promotion-extern-static.rs:9:43: 9:44
4242
StorageDead(_1); // scope 0 at $DIR/const-promotion-extern-static.rs:9:43: 9:44
4343
return; // scope 0 at $DIR/const-promotion-extern-static.rs:9:1: 9:45
4444
}
45-
45+
4646
bb2 (cleanup): {
4747
resume; // scope 0 at $DIR/const-promotion-extern-static.rs:9:1: 9:45
4848
}
4949
- }
50-
-
50+
-
5151
- alloc1 (static: Y, size: 4, align: 4) {
5252
- 2a 00 00 00 │ *...
5353
}
54-
54+

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
- // MIR for `FOO` before PromoteTemps
22
+ // MIR for `FOO` after PromoteTemps
3-
3+
44
static mut FOO: *const &i32 = {
55
let mut _0: *const &i32; // return place in scope 0 at $DIR/const-promotion-extern-static.rs:13:17: 13:28
66
let mut _1: &[&i32]; // in scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:55
@@ -11,7 +11,7 @@
1111
+ let mut _6: &[&i32; 1]; // in scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:55
1212
scope 1 {
1313
}
14-
14+
1515
bb0: {
1616
StorageLive(_1); // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:55
1717
StorageLive(_2); // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:55
@@ -37,18 +37,18 @@
3737
// + span: $DIR/const-promotion-extern-static.rs:13:47: 13:53
3838
// + literal: Const { ty: for<'r> fn(&'r [&i32]) -> *const &i32 {core::slice::<impl [&i32]>::as_ptr}, val: Value(Scalar(<ZST>)) }
3939
}
40-
40+
4141
bb1: {
4242
- StorageDead(_5); // scope 0 at $DIR/const-promotion-extern-static.rs:13:54: 13:55
4343
- StorageDead(_3); // scope 0 at $DIR/const-promotion-extern-static.rs:13:54: 13:55
4444
StorageDead(_1); // scope 0 at $DIR/const-promotion-extern-static.rs:13:54: 13:55
4545
return; // scope 0 at $DIR/const-promotion-extern-static.rs:13:1: 13:56
4646
}
47-
47+
4848
bb2 (cleanup): {
4949
resume; // scope 0 at $DIR/const-promotion-extern-static.rs:13:1: 13:56
5050
}
5151
}
52-
-
52+
-
5353
- alloc3 (extern static: X)
54-
54+

0 commit comments

Comments
 (0)