Skip to content

Commit 2ce2303

Browse files
committed
(c2rust-analyze) Replace AnalysisCtxt::const_ref_{tys: HashMap<ConstantKind, LTy> => locs: Vec<Location> that indexes into rvalue_tys, which actually stores the LTys.
1 parent 0025b02 commit 2ce2303

File tree

3 files changed

+31
-43
lines changed

3 files changed

+31
-43
lines changed

c2rust-analyze/src/context.rs

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use bitflags::bitflags;
1010
use rustc_hir::def_id::DefId;
1111
use rustc_index::vec::IndexVec;
1212
use rustc_middle::mir::{
13-
Body, CastKind, ConstantKind, Field, HasLocalDecls, Local, LocalDecls, Location, Operand,
14-
Place, PlaceElem, PlaceRef, Rvalue,
13+
Body, CastKind, Field, HasLocalDecls, Local, LocalDecls, Location, Operand, Place, PlaceElem,
14+
PlaceRef, Rvalue,
1515
};
1616
use rustc_middle::ty::adjustment::PointerCast;
1717
use rustc_middle::ty::{self, AdtDef, FieldDef, Ty, TyCtxt, TyKind};
@@ -66,10 +66,6 @@ impl PermissionSet {
6666
panic!("expected an array, str, or primitive type: {ty:?}");
6767
}
6868
}
69-
70-
pub fn for_const_ref(constant: ConstantKind) -> Self {
71-
Self::for_const_ref_ty(constant.ty())
72-
}
7369
}
7470

7571
bitflags! {
@@ -118,15 +114,18 @@ pub struct AnalysisCtxt<'a, 'tcx> {
118114
/// those `PointerId`s consistent, the `Rvalue`'s type must be stored rather than recomputed on
119115
/// the fly.
120116
pub rvalue_tys: HashMap<Location, LTy<'tcx>>,
121-
pub const_ref_tys: HashMap<ConstantKind<'tcx>, LTy<'tcx>>,
117+
118+
/// [`Location`]s of const ref [`rvalue_tys`](Self::rvalue_tys).
119+
pub const_ref_locs: Vec<Location>,
120+
122121
next_ptr_id: NextLocalPointerId,
123122
}
124123

125124
pub struct AnalysisCtxtData<'tcx> {
126125
local_tys: IndexVec<Local, LTy<'tcx>>,
127126
addr_of_local: IndexVec<Local, PointerId>,
128127
rvalue_tys: HashMap<Location, LTy<'tcx>>,
129-
const_ref_tys: HashMap<ConstantKind<'tcx>, LTy<'tcx>>,
128+
const_ref_locs: Vec<Location>,
130129
next_ptr_id: NextLocalPointerId,
131130
}
132131

@@ -219,7 +218,7 @@ impl<'a, 'tcx> AnalysisCtxt<'a, 'tcx> {
219218
c_void_casts: CVoidCasts::new(mir, tcx),
220219
addr_of_local: IndexVec::new(),
221220
rvalue_tys: HashMap::new(),
222-
const_ref_tys: HashMap::new(),
221+
const_ref_locs: Default::default(),
223222
next_ptr_id: NextLocalPointerId::new(),
224223
}
225224
}
@@ -233,7 +232,7 @@ impl<'a, 'tcx> AnalysisCtxt<'a, 'tcx> {
233232
local_tys,
234233
addr_of_local,
235234
rvalue_tys,
236-
const_ref_tys,
235+
const_ref_locs,
237236
next_ptr_id,
238237
} = data;
239238
AnalysisCtxt {
@@ -243,7 +242,7 @@ impl<'a, 'tcx> AnalysisCtxt<'a, 'tcx> {
243242
c_void_casts: CVoidCasts::default(),
244243
addr_of_local,
245244
rvalue_tys,
246-
const_ref_tys,
245+
const_ref_locs,
247246
next_ptr_id,
248247
}
249248
}
@@ -253,7 +252,7 @@ impl<'a, 'tcx> AnalysisCtxt<'a, 'tcx> {
253252
local_tys: self.local_tys,
254253
addr_of_local: self.addr_of_local,
255254
rvalue_tys: self.rvalue_tys,
256-
const_ref_tys: self.const_ref_tys,
255+
const_ref_locs: self.const_ref_locs,
257256
next_ptr_id: self.next_ptr_id,
258257
}
259258
}
@@ -442,13 +441,13 @@ impl<'tcx> AnalysisCtxtData<'tcx> {
442441
map: PointerTable<PointerId>,
443442
counter: NextLocalPointerId,
444443
) {
445-
let AnalysisCtxtData {
446-
ref mut local_tys,
447-
ref mut addr_of_local,
448-
ref mut rvalue_tys,
449-
ref mut const_ref_tys,
450-
ref mut next_ptr_id,
451-
} = *self;
444+
let Self {
445+
local_tys,
446+
addr_of_local,
447+
rvalue_tys,
448+
const_ref_locs: _,
449+
next_ptr_id,
450+
} = self;
452451

453452
for lty in local_tys {
454453
*lty = remap_lty_pointers(lcx, &map, lty);
@@ -464,10 +463,6 @@ impl<'tcx> AnalysisCtxtData<'tcx> {
464463
*lty = remap_lty_pointers(lcx, &map, lty);
465464
}
466465

467-
for lty in const_ref_tys.values_mut() {
468-
*lty = remap_lty_pointers(lcx, &map, lty);
469-
}
470-
471466
*next_ptr_id = counter;
472467
}
473468

@@ -533,13 +528,7 @@ impl<'tcx> TypeOf<'tcx> for Operand<'tcx> {
533528
fn type_of(&self, acx: &AnalysisCtxt<'_, 'tcx>) -> LTy<'tcx> {
534529
match *self {
535530
Operand::Move(pl) | Operand::Copy(pl) => acx.type_of(pl),
536-
Operand::Constant(ref c) => {
537-
let c = &**c;
538-
match acx.const_ref_tys.get(&c.literal) {
539-
Some(lty) => lty,
540-
None => label_no_pointers(acx, c.ty()),
541-
}
542-
}
531+
Operand::Constant(ref c) => label_no_pointers(acx, c.ty()),
543532
}
544533
}
545534
}

c2rust-analyze/src/dataflow/type_check.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,9 +465,12 @@ pub fn visit<'tcx>(
465465
equiv_constraints: Vec::new(),
466466
};
467467

468-
for (&constant, const_lty) in &acx.const_ref_tys {
469-
tc.constraints
470-
.add_all_perms(const_lty.label, PermissionSet::for_const_ref(constant));
468+
for loc in &acx.const_ref_locs {
469+
let const_ref_lty = acx.rvalue_tys[loc];
470+
tc.constraints.add_all_perms(
471+
const_ref_lty.label,
472+
PermissionSet::for_const_ref_ty(const_ref_lty.ty),
473+
);
471474
}
472475

473476
for (bb, bb_data) in mir.basic_blocks().iter_enumerated() {

c2rust-analyze/src/main.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,6 @@ fn run(tcx: TyCtxt) {
454454
assert_eq!(local, l);
455455
}
456456

457-
let mut const_ref_tys = HashMap::new();
458457
for (bb, bb_data) in mir.basic_blocks().iter_enumerated() {
459458
for (i, stmt) in bb_data.statements.iter().enumerate() {
460459
let (_, rv) = match &stmt.kind {
@@ -496,13 +495,11 @@ fn run(tcx: TyCtxt) {
496495
// The [`Constant`] is an inline value and thus local to this function,
497496
// as opposed to a global, named `const`s, for example.
498497
// This might miss local, named `const`s,
499-
const_ref_tys
500-
.entry(c.literal)
501-
.or_insert_with(|| acx.assign_pointer_ids(ty));
498+
acx.assign_pointer_ids(ty)
502499
} else {
503500
// TODO: Handle global, named `const`s.
501+
continue;
504502
}
505-
continue;
506503
}
507504
_ => continue,
508505
};
@@ -513,8 +510,6 @@ fn run(tcx: TyCtxt) {
513510
acx.rvalue_tys.insert(loc, lty);
514511
}
515512
}
516-
assert!(acx.const_ref_tys.is_empty());
517-
acx.const_ref_tys = const_ref_tys;
518513

519514
// Compute local equivalence classes and dataflow constraints.
520515
let (dataflow, equiv_constraints) = dataflow::generate_constraints(&acx, &mir);
@@ -625,9 +620,10 @@ fn run(tcx: TyCtxt) {
625620
let mut asn = gasn.and(&mut info.lasn);
626621
info.dataflow.propagate_cell(&mut asn);
627622

628-
for (&constant, const_lty) in &acx.const_ref_tys {
629-
let ptr_id = const_lty.label;
630-
let expected_perms = PermissionSet::for_const_ref(constant);
623+
for loc in &acx.const_ref_locs {
624+
let const_ref_lty = acx.rvalue_tys[loc];
625+
let ptr_id = const_ref_lty.label;
626+
let expected_perms = PermissionSet::for_const_ref_ty(const_ref_lty.ty);
631627
let mut actual_perms = asn.perms()[ptr_id];
632628
// Ignore `UNIQUE` as it gets automatically added to all permissions
633629
// and then removed later if it can't apply.

0 commit comments

Comments
 (0)