Skip to content

Commit 413456d

Browse files
committed
Make self_id a TransId
1 parent 74a9293 commit 413456d

File tree

2 files changed

+41
-29
lines changed

2 files changed

+41
-29
lines changed

creusot/src/backend/clone_map.rs

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ use rustc_hir::{
1010
def_id::DefId,
1111
};
1212
use rustc_middle::ty::{
13-
self,
14-
subst::{InternalSubsts, SubstsRef},
15-
AliasKind, ParamEnv, Ty, TyCtxt, TyKind, TypeFoldable, TypeSuperVisitable, TypeVisitor,
13+
self, subst::SubstsRef, AliasKind, ParamEnv, Ty, TyCtxt, TyKind, TypeFoldable,
14+
TypeSuperVisitable, TypeVisitor,
1615
};
1716
use rustc_span::{Symbol, DUMMY_SP};
1817
use rustc_target::abi::FieldIdx;
@@ -108,8 +107,8 @@ pub struct CloneMap<'tcx> {
108107
// - Body: Will directly use the full body of dependencies, except for program functions
109108
clone_level: CloneLevel,
110109

111-
// DefId of the item which is cloning. Used for trait resolution
112-
self_id: CloneNode<'tcx>, // TODO should this be a TransId?
110+
// TransId of the item which is cloning. Used for trait resolution
111+
self_id: TransId,
113112
// TODO: Push the graph into an opaque type with tight api boundary
114113
// Graph which is used to calculate the full clone set
115114
clone_graph: DiGraphMap<DepNode<'tcx>, IndexSet<(Kind, SymbolKind)>>,
@@ -213,23 +212,8 @@ impl<'tcx> CloneMap<'tcx> {
213212
pub(crate) fn new(tcx: TyCtxt<'tcx>, self_id: TransId, clone_level: CloneLevel) -> Self {
214213
let mut names = IndexMap::new();
215214

216-
let self_id = match self_id {
217-
TransId::Item(self_id) => {
218-
let subst = match tcx.def_kind(self_id) {
219-
DefKind::Closure => match tcx.type_of(self_id).subst_identity().kind() {
220-
TyKind::Closure(_, subst) => subst,
221-
_ => unreachable!(),
222-
},
223-
_ => InternalSubsts::identity_for_item(tcx, self_id),
224-
};
225-
226-
CloneNode::new(tcx, (self_id, subst)).erase_regions(tcx)
227-
}
228-
TransId::TyInv(inv_kind) => CloneNode::TyInv(inv_kind.to_skeleton_ty(tcx)),
229-
};
230-
231215
debug!("cloning self: {:?}", self_id);
232-
names.insert(self_id, CloneInfo::hidden());
216+
names.insert(CloneNode::from_trans_id(tcx, self_id), CloneInfo::hidden());
233217

234218
CloneMap {
235219
tcx,
@@ -374,8 +358,15 @@ impl<'tcx> CloneMap<'tcx> {
374358
self.value(def_id, subst)
375359
}
376360

361+
fn self_key(&self) -> CloneNode<'tcx> {
362+
CloneNode::from_trans_id(self.tcx, self.self_id)
363+
}
364+
377365
fn self_did(&self) -> Option<DefId> {
378-
self.self_id.did().map(|(self_did, _)| self_did)
366+
match self.self_id {
367+
TransId::Item(did) | TransId::TyInv(TyInvKind::Adt(did)) => Some(did),
368+
_ => None,
369+
}
379370
}
380371

381372
fn param_env(&self, ctx: &TranslationCtx<'tcx>) -> ParamEnv<'tcx> {
@@ -412,8 +403,9 @@ impl<'tcx> CloneMap<'tcx> {
412403
i += 1;
413404
trace!("update graph with {:?} (public={:?})", key, self.names[&key].public);
414405

415-
if key != self.self_id {
416-
self.add_graph_edge(self.self_id, key);
406+
let self_key = self.self_key();
407+
if key != self_key {
408+
self.add_graph_edge(self_key, key);
417409
}
418410

419411
if self.names[&key].kind == Kind::Hidden {
@@ -448,7 +440,7 @@ impl<'tcx> CloneMap<'tcx> {
448440
}
449441

450442
let inv_kind = TyInvKind::from_ty(ty);
451-
if self.self_id.ty_inv_kind().is_some_and(|self_kind| self_kind == inv_kind) {
443+
if let TransId::TyInv(self_kind) = self.self_id && self_kind == inv_kind {
452444
return;
453445
}
454446

@@ -674,7 +666,7 @@ impl<'tcx> CloneMap<'tcx> {
674666
// Broken because of closures which share a defid for the type *and* function
675667
// debug_assert!(!is_cyclic_directed(&self.clone_graph), "clone graph for {:?} is cyclic", self.self_id );
676668

677-
let mut topo = DfsPostOrder::new(&self.clone_graph, self.self_id);
669+
let mut topo = DfsPostOrder::new(&self.clone_graph, self.self_key());
678670
while let Some(node) = topo.walk_next(&self.clone_graph) {
679671
trace!("processing node {:?}", self.names[&node].kind);
680672

creusot/src/backend/dependency.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use rustc_hir::def_id::DefId;
2-
use rustc_middle::ty::{EarlyBinder, ParamEnv, SubstsRef, Ty, TyCtxt, TyKind};
1+
use rustc_hir::{def::DefKind, def_id::DefId};
2+
use rustc_middle::ty::{EarlyBinder, InternalSubsts, ParamEnv, SubstsRef, Ty, TyCtxt, TyKind};
33
use rustc_span::Symbol;
44
use rustc_type_ir::AliasKind;
55

@@ -9,7 +9,10 @@ use crate::{
99
util::{self, ItemType},
1010
};
1111

12-
use super::ty_inv::{self, TyInvKind};
12+
use super::{
13+
ty_inv::{self, TyInvKind},
14+
TransId,
15+
};
1316

1417
/// Dependencies between items and the resolution logic to find the 'monomorphic' forms accounting
1518
/// for various Creusot hacks like the handling of closures.
@@ -33,6 +36,23 @@ impl<'tcx> Dependency<'tcx> {
3336
}
3437
}
3538

39+
pub(crate) fn from_trans_id(tcx: TyCtxt<'tcx>, trans_id: TransId) -> Self {
40+
match trans_id {
41+
TransId::Item(self_id) => {
42+
let subst = match tcx.def_kind(self_id) {
43+
DefKind::Closure => match tcx.type_of(self_id).subst_identity().kind() {
44+
TyKind::Closure(_, subst) => subst,
45+
_ => unreachable!(),
46+
},
47+
_ => InternalSubsts::identity_for_item(tcx, self_id),
48+
};
49+
50+
Dependency::new(tcx, (self_id, subst)).erase_regions(tcx)
51+
}
52+
TransId::TyInv(inv_kind) => Dependency::TyInv(inv_kind.to_skeleton_ty(tcx)),
53+
}
54+
}
55+
3656
pub(crate) fn resolve(
3757
self,
3858
ctx: &TranslationCtx<'tcx>,

0 commit comments

Comments
 (0)