Skip to content

Commit 91c8fcb

Browse files
authored
Merge pull request #1490 from Nadrieril/fix-1485
In mono mode, avoid poly items as much as possible
2 parents aca22db + bb3e82c commit 91c8fcb

15 files changed

Lines changed: 540 additions & 12 deletions

‎charon/src/bin/charon-driver/translate/translate_crate.rs‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -719,19 +719,12 @@ impl<'tcx, 'ctx> ItemTransCtx<'tcx, 'ctx> {
719719
item: &hax::ItemRef,
720720
kind: TransItemSourceKind,
721721
) -> T {
722-
let item = if self.monomorphize() && item.has_param {
722+
let mono = self.should_monomorphize_item(item, kind);
723+
let item = if mono && item.has_param {
723724
item.erase(self.hax_state_with_id())
724725
} else {
725726
item.clone()
726727
};
727-
// In mono mode:
728-
// 1. If the item being registered is a `trait decl`, we construct a
729-
// `monomorphic_trait` item source.
730-
// 2. Otherwise, if the current `item_trans_ctx` is under a `trait decl`
731-
// or a `vtable`, we construct a `poly` item.
732-
// 3. In all other cases, we construct a `mono` item.
733-
let mono =
734-
self.monomorphize() && (kind.is_for_trait() || !self.item_src.kind.is_for_trait());
735728
let item_src = TransItemSource::from_item(&item, kind, mono);
736729
if enqueue {
737730
self.register_and_enqueue(span, item_src)
@@ -779,7 +772,8 @@ impl<'tcx, 'ctx> ItemTransCtx<'tcx, 'ctx> {
779772

780773
let id: ItemId = self.register_item_maybe_enqueue(span, enqueue, hax_item, kind);
781774
// In mono mode, we keep trait decls generic.
782-
let mut generics = if self.monomorphize() && !matches!(kind, TransItemSourceKind::TraitDecl)
775+
let mut generics = if self.should_monomorphize_item(hax_item, kind)
776+
&& !matches!(kind, TransItemSourceKind::TraitDecl)
783777
{
784778
GenericArgs::empty()
785779
} else {

‎charon/src/bin/charon-driver/translate/translate_ctx.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,13 @@ impl<'tcx, 'ctx> ItemTransCtx<'tcx, 'ctx> {
245245
)
246246
}
247247

248+
/// Whether to translate this particular item monomorphically.
249+
pub fn should_monomorphize_item(&self, item: &hax::ItemRef, kind: TransItemSourceKind) -> bool {
250+
// Traits and vtables use `MonoTrait` in mono mode. Other items are translated
251+
// monomorphically when their arguments are concrete, even if the context is poly.
252+
self.options.monomorphize_with_hax && (kind.is_for_trait() || !item.has_non_lt_param)
253+
}
254+
248255
pub fn span_err(&self, span: Span, msg: &str, level: Level) -> Error {
249256
self.t_ctx.span_err(span, msg, level)
250257
}

‎charon/src/bin/charon-driver/translate/translate_items.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,8 +1130,10 @@ impl<'tcx> ItemTransCtx<'tcx, '_> {
11301130
let is_negative = !trait_pred.is_positive;
11311131
let is_unsafe = timpl.is_unsafe();
11321132

1133-
// Translate the bare minimum needed for names: `impl_trait`.
1133+
// Translate the bare minimum needed for names.
11341134
if self.is_poly_in_mono(&self.item_src) {
1135+
let implied_trait_refs =
1136+
self.translate_trait_proofs(span, timpl.implied_trait_proofs())?;
11351137
return Ok(TraitImpl {
11361138
def_id,
11371139
item_meta,
@@ -1140,7 +1142,7 @@ impl<'tcx> ItemTransCtx<'tcx, '_> {
11401142
is_negative,
11411143
is_unsafe,
11421144
generics: self.into_generics(),
1143-
implied_trait_refs: Default::default(),
1145+
implied_trait_refs,
11441146
consts: Default::default(),
11451147
types: Default::default(),
11461148
methods: Default::default(),

‎charon/src/transform/typecheck_and_unify.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ impl TypeCheckVisitor<'_> {
9999
(TyKind::Adt(a), TyKind::Adt(b)) if a.id == b.id => {
100100
self.match_generics(&a.generics, &b.generics)?
101101
}
102+
(TyKind::Adt(a), TyKind::Adt(b))
103+
if self.ctx.options.monomorphize_with_hax
104+
&& self.ctx.translated.item_name(a.id).mono_args().is_some()
105+
!= self.ctx.translated.item_name(b.id).mono_args().is_some() =>
106+
{
107+
// FIXME(#856): We translate traits polymorphiclaly, which can give rise to having
108+
// both the polymorphic and monomophized version of a same type, see e.g.
109+
// supertrait-with-generic-type-args.rs. We ignore that for now.
110+
}
102111
(TyKind::Scalar(a), TyKind::Scalar(b)) if a == b => {}
103112
(TyKind::Never, TyKind::Never) => {}
104113
(TyKind::Array(aty, ..), TyKind::Array(bty, ..)) => {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Final LLBC before serialization:
2+
3+
struct () {}
4+
5+
fn test_crate::{Wrapper<T>[TraitClause0]}::inherent::VALUE() -> u8
6+
{
7+
let _0: u8; // return
8+
9+
storage_live(_0)
10+
_0 = const 1u8
11+
return
12+
}
13+
14+
const test_crate::{Wrapper<T>[TraitClause0]}::inherent::VALUE: u8 = test_crate::{Wrapper<T>[TraitClause0]}::inherent::VALUE()
15+
16+
// Full name: test_crate::{Wrapper::<u8>}::inherent::<u8>
17+
fn inherent::<u8>() -> u8
18+
{
19+
let _0: u8; // return
20+
21+
storage_live(_0)
22+
_0 = copy test_crate::{Wrapper<T>[TraitClause0]}::inherent::VALUE
23+
return
24+
}
25+
26+
// Full name: test_crate::{impl Trait for Wrapper<T>[TraitClause0]}::method::VALUE
27+
fn impl_Trait_for_Wrapper::method::VALUE() -> u8
28+
{
29+
let _0: u8; // return
30+
31+
storage_live(_0)
32+
_0 = const 2u8
33+
return
34+
}
35+
36+
// Full name: test_crate::{impl Trait for Wrapper<T>[TraitClause0]}::method::VALUE
37+
const impl_Trait_for_Wrapper::method::VALUE: u8 = impl_Trait_for_Wrapper::method::VALUE()
38+
39+
// Full name: test_crate::{impl Trait for Wrapper::<u16>}::method::<u16>
40+
fn method::<u16>() -> u8
41+
{
42+
let _0: u8; // return
43+
44+
storage_live(_0)
45+
_0 = copy impl_Trait_for_Wrapper::method::VALUE
46+
return
47+
}
48+
49+
// Full name: test_crate::main
50+
fn main()
51+
{
52+
let _0: (); // return
53+
let _1: u8; // anonymous local
54+
let _2: u8; // anonymous local
55+
56+
storage_live(_0)
57+
_0 = ()
58+
storage_live(_1)
59+
_1 = inherent::<u8>()
60+
↳⚡ unwind_continue
61+
storage_dead(_1)
62+
storage_live(_2)
63+
_2 = method::<u16>()
64+
↳⚡ unwind_continue
65+
storage_dead(_2)
66+
_0 = ()
67+
return
68+
}
69+
70+
71+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//@ charon-args=--monomorphize --start-from=crate::main
2+
3+
struct Wrapper<T>(T);
4+
5+
impl<T> Wrapper<T> {
6+
fn inherent() -> u8 {
7+
const VALUE: u8 = 1;
8+
VALUE
9+
}
10+
}
11+
12+
trait Trait {
13+
fn method() -> u8;
14+
}
15+
16+
impl<T> Trait for Wrapper<T> {
17+
fn method() -> u8 {
18+
const VALUE: u8 = 2;
19+
VALUE
20+
}
21+
}
22+
23+
fn main() {
24+
Wrapper::<u8>::inherent();
25+
<Wrapper<u16> as Trait>::method();
26+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Final LLBC before serialization:
2+
3+
// Full name: core::marker::MetaSized
4+
#[fundamental]
5+
#[lang_item(MetaSized)]
6+
pub trait MetaSized<Self>
7+
8+
struct () {}
9+
10+
// Full name: test_crate::Concrete
11+
pub struct Concrete {}
12+
13+
// Full name: test_crate::Wrapper::<u8>
14+
pub struct Wrapper::<u8> {
15+
_0: u8,
16+
}
17+
18+
// Full name: test_crate::Parent
19+
pub trait Parent<Self, T>
20+
{
21+
proof ImpliedClause0: (Self: MetaSized)
22+
proof ImpliedClause1: (T: MetaSized)
23+
vtable: test_crate::Parent::{vtable}
24+
}
25+
26+
// Full name: test_crate::Child
27+
pub trait Child<Self>
28+
{
29+
proof ImpliedClause0: (Self: MetaSized)
30+
proof ImpliedClause1: (Self: Parent<Concrete>)
31+
proof ImpliedClause2: (Self: Parent<Wrapper::<u8>>)
32+
vtable: test_crate::Child::{vtable}
33+
}
34+
35+
// Full name: test_crate::{impl Parent<Concrete> for ()}
36+
impl "impl_Parent_Concrete_for_unit" Parent<Concrete> for () {
37+
proof ImpliedClause0: ((): MetaSized) = {built_in impl MetaSized for ()}
38+
proof ImpliedClause1: (Concrete: MetaSized) = {built_in impl MetaSized for Concrete}
39+
vtable: lazy
40+
}
41+
42+
// Full name: test_crate::{impl Parent<Wrapper::<u8>> for ()}
43+
impl "impl_Parent_for_unit" Parent<Wrapper::<u8>> for () {
44+
proof ImpliedClause0: ((): MetaSized) = {built_in impl MetaSized for ()}
45+
proof ImpliedClause1: (Wrapper::<u8>: MetaSized) = {built_in impl MetaSized for Wrapper::<u8>}
46+
vtable: lazy
47+
}
48+
49+
// Full name: test_crate::{impl Child for ()}
50+
impl "impl_Child_for_unit" Child for () {
51+
proof ImpliedClause0: ((): MetaSized) = {built_in impl MetaSized for ()}
52+
proof ImpliedClause1: ((): Parent<Concrete>) = impl_Parent_Concrete_for_unit
53+
proof ImpliedClause2: ((): Parent<Wrapper::<u8>>) = impl_Parent_for_unit
54+
vtable: lazy
55+
}
56+
57+
58+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Final LLBC before serialization:
2+
3+
// Full name: core::marker::MetaSized
4+
#[fundamental]
5+
#[lang_item(MetaSized)]
6+
pub trait MetaSized<Self>
7+
8+
// Full name: core::marker::Sized
9+
#[fundamental]
10+
#[lang_item(Sized)]
11+
pub trait Sized<Self>
12+
{
13+
proof ImpliedClause0: (Self: MetaSized)
14+
non-dyn-compatible
15+
}
16+
17+
struct () {}
18+
19+
// Full name: test_crate::Concrete
20+
pub struct Concrete {}
21+
22+
// Full name: test_crate::Wrapper
23+
pub struct Wrapper<T>
24+
where
25+
TraitClause0: (T: Sized),
26+
{
27+
_0: T,
28+
}
29+
30+
// Full name: test_crate::Parent
31+
pub trait Parent<Self, T>
32+
{
33+
proof ImpliedClause0: (Self: MetaSized)
34+
proof ImpliedClause1: (T: MetaSized)
35+
vtable: test_crate::Parent::{vtable}<T>
36+
}
37+
38+
// Full name: test_crate::Child
39+
pub trait Child<Self>
40+
{
41+
proof ImpliedClause0: (Self: MetaSized)
42+
proof ImpliedClause1: (Self: Parent<Concrete>)
43+
proof ImpliedClause2: (Self: Parent<Wrapper<u8>[{built_in impl Sized for u8}]>)
44+
vtable: test_crate::Child::{vtable}
45+
}
46+
47+
// Full name: test_crate::{impl Parent<Concrete> for ()}
48+
impl "impl_Parent_Concrete_for_unit" Parent<Concrete> for () {
49+
proof ImpliedClause0: ((): MetaSized) = {built_in impl MetaSized for ()}
50+
proof ImpliedClause1: (Concrete: MetaSized) = {built_in impl MetaSized for Concrete}
51+
vtable: lazy
52+
}
53+
54+
// Full name: test_crate::{impl Parent<Wrapper<u8>[{built_in impl Sized for u8}]> for ()}
55+
impl "impl_Parent_Wrapper_for_unit" Parent<Wrapper<u8>[{built_in impl Sized for u8}]> for () {
56+
proof ImpliedClause0: ((): MetaSized) = {built_in impl MetaSized for ()}
57+
proof ImpliedClause1: (Wrapper<u8>[{built_in impl Sized for u8}]: MetaSized) = {built_in impl MetaSized for Wrapper<u8>[{built_in impl Sized for u8}]}
58+
vtable: lazy
59+
}
60+
61+
// Full name: test_crate::{impl Child for ()}
62+
impl "impl_Child_for_unit" Child for () {
63+
proof ImpliedClause0: ((): MetaSized) = {built_in impl MetaSized for ()}
64+
proof ImpliedClause1: ((): Parent<Concrete>) = impl_Parent_Concrete_for_unit
65+
proof ImpliedClause2: ((): Parent<Wrapper<u8>[{built_in impl Sized for u8}]>) = impl_Parent_Wrapper_for_unit
66+
vtable: lazy
67+
}
68+
69+
70+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@ revisions=poly,mono
2+
//@[mono] charon-args=--monomorphize
3+
pub struct Concrete;
4+
pub struct Wrapper<T>(T);
5+
6+
pub trait Parent<T: ?Sized> {}
7+
8+
pub trait Child: Parent<Concrete> + Parent<Wrapper<u8>> {}
9+
10+
impl Parent<Concrete> for () {}
11+
impl Parent<Wrapper<u8>> for () {}
12+
impl Child for () {}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Final LLBC before serialization:
2+
3+
// Full name: core::marker::MetaSized
4+
#[fundamental]
5+
#[lang_item(MetaSized)]
6+
pub trait MetaSized<Self>
7+
8+
// Full name: core::marker::Sized
9+
#[fundamental]
10+
#[lang_item(Sized)]
11+
pub trait Sized<Self>
12+
{
13+
proof ImpliedClause0: (Self: MetaSized)
14+
non-dyn-compatible
15+
}
16+
17+
struct () {}
18+
19+
// Full name: test_crate::Wrapper::<u8>
20+
pub struct Wrapper::<u8> {
21+
_0: u8,
22+
}
23+
24+
// Full name: test_crate::Parent
25+
pub trait Parent<Self, T>
26+
{
27+
proof ImpliedClause0: (Self: MetaSized)
28+
proof ImpliedClause1: (T: Sized)
29+
vtable: test_crate::Parent::{vtable}
30+
}
31+
32+
// Full name: test_crate::Child
33+
pub trait Child<Self, T>
34+
{
35+
proof ImpliedClause0: (Self: MetaSized)
36+
proof ImpliedClause1: (Self: Parent<Wrapper<T>[Self::ImpliedClause2]>)
37+
proof ImpliedClause2: (T: Sized)
38+
vtable: test_crate::Child::{vtable}
39+
}
40+
41+
// Full name: test_crate::{impl Parent<Wrapper::<u8>> for ()}
42+
impl "impl_Parent_for_unit" Parent<Wrapper::<u8>> for () {
43+
proof ImpliedClause0: ((): MetaSized) = {built_in impl MetaSized for ()}
44+
proof ImpliedClause1: (Wrapper::<u8>: Sized) = {built_in impl Sized for Wrapper::<u8>}
45+
vtable: lazy
46+
}
47+
48+
// Full name: test_crate::{impl Child<u8> for ()}
49+
impl "impl_Child_u8_for_unit" Child<u8> for () {
50+
proof ImpliedClause0: ((): MetaSized) = {built_in impl MetaSized for ()}
51+
proof ImpliedClause1: ((): Parent<Wrapper::<u8>>) = impl_Parent_for_unit
52+
proof ImpliedClause2: (u8: Sized) = {built_in impl Sized for u8}
53+
vtable: lazy
54+
}
55+
56+
57+

0 commit comments

Comments
 (0)