Skip to content

Commit ec7b41b

Browse files
committed
drop shim debug & regions added
1 parent 171b81a commit ec7b41b

File tree

6 files changed

+60
-67
lines changed

6 files changed

+60
-67
lines changed

charon/src/transform/compute_vtable_metadata.rs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -661,11 +661,15 @@ impl<'a> VtableMetadataComputer<'a> {
661661
self.impl_ref.id.with_ctx(&self.ctx.into_fmt())
662662
);
663663
// Get the generics from the trait impl - drop shims should have the same generics
664-
let generics = self.get_trait_impl_generics()?;
664+
let generics = self.get_drop_shim_generic_params()?;
665665

666666
// Create the dyn trait type for the parameter
667667
// For the drop shim, we need &mut (dyn Trait<...>)
668668
let dyn_trait_param_ty = self.get_drop_receiver()?;
669+
let dyn_trait_param_ty = match dyn_trait_param_ty.kind() {
670+
TyKind::Ref(_,ty,_) => Ty::new(TyKind::Ref(Region::Var(DeBruijnVar::new_at_zero(RegionId::ZERO)), ty.clone(), RefKind::Mut)),
671+
_ => unreachable!(),
672+
};
669673

670674
// Create function signature with proper generics
671675
let signature = FunSig {
@@ -675,9 +679,7 @@ impl<'a> VtableMetadataComputer<'a> {
675679
output: Ty::mk_unit(),
676680
};
677681

678-
let body = DropShimCtx::create_drop_shim_body(self, &self.get_drop_receiver()?, concrete_ty, drop_case)?;
679-
680-
// let body = self.old_create_drop_shim_body(&self.get_drop_receiver()?, concrete_ty, drop_case)?;
682+
let body = DropShimCtx::create_drop_shim_body(self, &dyn_trait_param_ty, concrete_ty, drop_case)?;
681683

682684
// Create item meta
683685
let item_meta = ItemMeta {
@@ -770,7 +772,7 @@ impl<'a> VtableMetadataComputer<'a> {
770772
}
771773
}
772774

773-
fn get_trait_impl_generics(&self) -> Result<GenericParams, Error> {
775+
fn get_drop_shim_generic_params(&self) -> Result<GenericParams, Error> {
774776
let Some(trait_impl) = self.ctx.translated.trait_impls.get(self.impl_ref.id) else {
775777
raise_error!(
776778
self.ctx,
@@ -784,13 +786,9 @@ impl<'a> VtableMetadataComputer<'a> {
784786
// but with at least one region binder for the receiver
785787
let mut generics = trait_impl.generics.clone();
786788

787-
// Ensure we have at least one region for the receiver parameter
788-
if generics.regions.is_empty() {
789-
let _ = generics.regions.push_with(|id| RegionVar {
790-
index: id,
791-
name: None,
792-
});
793-
}
789+
// Insert the region for the drop shim receiver
790+
generics.regions.iter_mut().for_each(|reg| reg.index += 1);
791+
generics.regions.insert_and_shift_ids(RegionId::ZERO, RegionVar { index: RegionId::ZERO, name: None });
794792

795793
Ok(generics)
796794
}
@@ -987,7 +985,7 @@ impl<'a> DropShimCtx<'a> {
987985
// Create a new variable with var := &mut drop_place
988986
// This is the argument to the drop function
989987
let drop_place_ref = self.new_var(None, Ty::new(TyKind::Ref(
990-
Region::Erased,
988+
Region::Var(DeBruijnVar::new_at_zero(RegionId::ZERO)),
991989
drop_place.ty().clone(),
992990
RefKind::Mut,
993991
)));
@@ -1255,7 +1253,7 @@ impl<'a> DropShimCtx<'a> {
12551253
}
12561254
_ => {
12571255
let concrete_place = locals.new_var(Some("concrete".into()), Ty::new(TyKind::Ref(
1258-
Region::Erased,
1256+
Region::Var(DeBruijnVar::new_at_zero(RegionId::ZERO)),
12591257
concrete_ty.clone(),
12601258
RefKind::Mut,
12611259
)));
@@ -1267,12 +1265,7 @@ impl<'a> DropShimCtx<'a> {
12671265
Rvalue::UnaryOp(
12681266
UnOp::Cast(CastKind::Concretize(
12691267
dyn_trait_param_ty.clone(),
1270-
TyKind::Ref(
1271-
Region::Erased,
1272-
concrete_ty.clone(),
1273-
RefKind::Mut,
1274-
)
1275-
.into_ty(),
1268+
concrete_place.ty.clone(),
12761269
)),
12771270
Operand::Move(locals.place_for_var(LocalId::new(1))),
12781271
),

charon/tests/ui/monomorphization/dyn-trait.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ note: the error occurred when translating `core::marker::MetaSized::<type_error(
124124
7 | x.to_string()
125125
| -------------
126126

127-
thread 'rustc' panicked at /home/ssyram/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/index_vec-0.1.4/src/indexing.rs:128:10:
127+
thread 'rustc' panicked at /Users/ssyram/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/index_vec-0.1.4/src/indexing.rs:128:10:
128128
index out of bounds: the len is 0 but the index is 0
129129
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
130130
error: Thread panicked when extracting item `alloc::string::{impl#23}`.

charon/tests/ui/monomorphization/unsatisfied-method-bounds.out

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ error: Error during trait resolution: Could not find a clause for `Binder { valu
5757

5858
disabled backtrace
5959
warning[E9999]: Could not find a clause for `Binder { value: <std::string::String as std::marker::Copy>, bound_vars: [] }` in the current context: `Unimplemented`
60-
--> /home/ssyram/.rustup/toolchains/nightly-2025-07-08-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/drop.rs:208:1
60+
--> /Users/ssyram/.rustup/toolchains/nightly-2025-07-08-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/ops/drop.rs:208:1
6161
|
6262
208 | pub trait Drop {
6363
| ^^^^^^^^^^^^^^
@@ -67,7 +67,7 @@ warning[E9999]: Could not find a clause for `Binder { value: <std::string::Strin
6767

6868
disabled backtrace
6969
warning[E9999]: Could not find a clause for `Binder { value: <std::string::String as std::marker::Copy>, bound_vars: [] }` in the current context: `Unimplemented`
70-
--> /home/ssyram/.rustup/toolchains/nightly-2025-07-08-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/drop.rs:241:5
70+
--> /Users/ssyram/.rustup/toolchains/nightly-2025-07-08-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/ops/drop.rs:241:5
7171
|
7272
241 | fn drop(&mut self);
7373
| ^^^^^^^^^^^^^^^^^^^
@@ -77,7 +77,7 @@ warning[E9999]: Could not find a clause for `Binder { value: <std::string::Strin
7777

7878
disabled backtrace
7979
warning[E9999]: Could not find a clause for `Binder { value: <std::string::String as std::marker::Copy>, bound_vars: [] }` in the current context: `Unimplemented`
80-
--> /home/ssyram/.rustup/toolchains/nightly-2025-07-08-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/marker.rs:179:1
80+
--> /Users/ssyram/.rustup/toolchains/nightly-2025-07-08-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/marker.rs:179:1
8181
|
8282
179 | pub trait MetaSized: PointeeSized {
8383
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

charon/tests/ui/unsize.out

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,16 +196,16 @@ impl Clone for String {
196196
// Full name: alloc::string::{impl Display for String}::fmt
197197
pub fn {impl Display for String}::fmt<'_0, '_1, '_2>(@1: &'_0 (String), @2: &'_1 mut (Formatter<'_2>)) -> Result<(), Error>[Sized<()>, Sized<Error>]
198198

199-
fn {{impl Display for String}}::{vtable}::{drop_method}<'_0>(@1: &'_ mut ((dyn exists<_dyn> [@TraitClause0]: Display<_dyn> + _dyn : '_)))
199+
fn {{impl Display for String}}::{vtable}::{drop_method}<'_0>(@1: &'_0 mut ((dyn exists<_dyn> [@TraitClause0]: Display<_dyn> + _dyn : '_)))
200200
{
201201
let ret@0: (); // return
202-
let self@1: &'_ mut ((dyn exists<_dyn> [@TraitClause0]: Display<_dyn> + _dyn : '_)); // arg #1
203-
let concrete@2: &'_ mut (String); // local
204-
let @3: &'_ mut (String); // anonymous local
202+
let self@1: &'_0 mut ((dyn exists<_dyn> [@TraitClause0]: Display<_dyn> + _dyn : '_)); // arg #1
203+
let concrete@2: &'_0 mut (String); // local
204+
let @3: &'_0 mut (String); // anonymous local
205205

206206
storage_live(concrete@2)
207207
storage_live(@3)
208-
concrete@2 := concretize<&'_ mut ((dyn exists<_dyn> [@TraitClause0]: Display<_dyn> + _dyn : '_)), &'_ mut (String)>(move (self@1))
208+
concrete@2 := concretize<&'_0 mut ((dyn exists<_dyn> [@TraitClause0]: Display<_dyn> + _dyn : '_)), &'_0 mut (String)>(move (self@1))
209209
@3 := &mut *(concrete@2)
210210
ret@0 := {impl Drop for String}::drop<'_0>(move (@3))
211211
ret@0 := ()

charon/tests/ui/vtable-simple.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@ where
8787
return
8888
}
8989

90-
fn {{impl Modifiable<T> for i32}}::{vtable}::{drop_method}<'_0, T>(@1: &'_ mut ((dyn exists<_dyn> [@TraitClause0]: Modifiable<_dyn, T> + _dyn : '_)))
90+
fn {{impl Modifiable<T> for i32}}::{vtable}::{drop_method}<'_0, T>(@1: &'_0 mut ((dyn exists<_dyn> [@TraitClause0]: Modifiable<_dyn, T> + _dyn : '_)))
9191
where
9292
[@TraitClause0]: Sized<T>,
9393
[@TraitClause1]: Clone<T>,
9494
{
9595
let ret@0: (); // return
96-
let self@1: &'_ mut ((dyn exists<_dyn> [@TraitClause0]: Modifiable<_dyn, T> + _dyn : '_)); // arg #1
96+
let self@1: &'_0 mut ((dyn exists<_dyn> [@TraitClause0]: Modifiable<_dyn, T> + _dyn : '_)); // arg #1
9797

9898
ret@0 := ()
9999
return

0 commit comments

Comments
 (0)