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`
This commit is contained in:
commit
a240ccd81c
18 changed files with 38 additions and 64 deletions
|
@ -2522,7 +2522,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));
|
||||
|
|
|
@ -17,9 +17,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
|
||||
|
@ -669,6 +668,7 @@ pub fn write_allocations<'tcx>(
|
|||
fn alloc_ids_from_alloc(alloc: &Allocation) -> impl DoubleEndedIterator<Item = AllocId> + '_ {
|
||||
alloc.relocations().values().map(|id| *id)
|
||||
}
|
||||
|
||||
fn alloc_ids_from_const(val: ConstValue<'_>) -> impl Iterator<Item = AllocId> + '_ {
|
||||
match val {
|
||||
ConstValue::Scalar(interpret::Scalar::Ptr(ptr, _size)) => {
|
||||
|
@ -682,17 +682,29 @@ 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`.
|
||||
|
|
|
@ -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,
|
||||
};
|
||||
|
@ -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!()`.
|
||||
|
|
|
@ -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 { .. } => {}
|
||||
InlineAsm { ref operands, template: _, options: _, line_spans: _ } => {
|
||||
for op in &**operands {
|
||||
use InlineAsmOperand::*;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue