charon icon indicating copy to clipboard operation
charon copied to clipboard

Add support for `dyn Trait`

Open Nadrieril opened this issue 1 year ago • 9 comments

Nadrieril avatar Apr 12 '24 17:04 Nadrieril

I investigated a bit. The issue is that we need to handle ExistentialPredicate which isn't quite a TraitRef. If I understand, after https://github.com/AeneasVerif/charon/issues/127 we might be able to use a TraitRef there.

Nadrieril avatar May 31 '24 09:05 Nadrieril

What do you mean by "we might be able to use a TraitRef there?

sonmarcho avatar Jun 02 '24 08:06 sonmarcho

dyn Trait refers to a trait with an ExistentialPredicate, which seems to be a mapping from Self to a list of clauses. E.g. dyn Iterator<Item=u32> would be represented as lambda Self -> Self: Iterator, Self::Item = u8. If we do #127, I think this simplifies to just a list of trait clauses on Self, which we can represent as a list TraitRefs.

Nadrieril avatar Jun 03 '24 08:06 Nadrieril

I think a dyn trait really existentially quantifies a type, and we need to represent that: lambda Self -> Self: Iterator, Self::Item = u8 is a predicate over some type Self, but must itself be quantified. If you have a function fn foo(...) -> dyn Trait in Lean we would generate something of the shape:

def foo ... : exists T, Trait T

or, written with a dependent tuple (we return a pair of a type T and the fact that it satisfies Trait T):

def foo ... : (T:Type, Trait T)

sonmarcho avatar Jun 03 '24 08:06 sonmarcho

Yes, but with #127 the existentially quantified predicate will always be of the form lambda Self -> Self: Trait1, Self: Trait2, which we can represent as a list [Trait1, Trait2].

Nadrieril avatar Jun 03 '24 09:06 Nadrieril

By which I mean, the TypeKind::Dyn variant will contain Vec<TraitRef> instead of having to define a new ExistentialPredicate thing and having to deal with substitution.

Nadrieril avatar Jun 03 '24 09:06 Nadrieril

I was wrong about that: a TraitRef looks like T: Into<u64>, but what we need for dyn is just the Into<u64> part. In other words, it's a TraitRef where the Self argument is not supplied. In yet other words, it's an existentially quantified TraitRef.

Nadrieril avatar Jun 28 '24 08:06 Nadrieril

Partial support here: https://github.com/AeneasVerif/charon/pull/276. Needs more work to support fully.

Nadrieril avatar Jun 28 '24 12:06 Nadrieril