Stream: wg-traits

Topic: captures-trait


Alexander Regueiro (Nov 20 2018 at 19:15, on Zulip):

@nikomatsakis Have you seen https://github.com/rust-lang/rust/pull/56047 yet BTW? I was hoping to get some clarification on https://github.com/rust-lang/rust/issues/34511#issuecomment-373423999 from you if that's alright.

nikomatsakis (Nov 20 2018 at 20:38, on Zulip):

@Alexander Regueiro I had not seen that -- I do actually think we should try to solve this at the language level

Alexander Regueiro (Nov 20 2018 at 23:09, on Zulip):

@nikomatsakis oh I see

Alexander Regueiro (Nov 20 2018 at 23:10, on Zulip):

@nikomatsakis what do you think the approach should be then?

Alexander Regueiro (Nov 21 2018 at 17:03, on Zulip):

@nikomatsakis hey, around by chance?

nikomatsakis (Nov 21 2018 at 17:09, on Zulip):

sort of

nikomatsakis (Nov 21 2018 at 17:24, on Zulip):

@Alexander Regueiro what's up

Alexander Regueiro (Nov 21 2018 at 17:24, on Zulip):

@nikomatsakis wanted to get your thoughts on the lifetime capturing issue if you have a minute

nikomatsakis (Nov 21 2018 at 17:25, on Zulip):

ah

Alexander Regueiro (Nov 21 2018 at 17:25, on Zulip):

re that comment of yours, from a while back

Alexander Regueiro (Nov 21 2018 at 17:25, on Zulip):

https://github.com/rust-lang/rust/issues/34511#issuecomment-373423999

nikomatsakis (Nov 21 2018 at 17:27, on Zulip):

left a quick comment on the RFC

nikomatsakis (Nov 21 2018 at 17:27, on Zulip):

the tl;dr is that Captures is not something I want users to have to think about :)

nikomatsakis (Nov 21 2018 at 17:27, on Zulip):

but I'm not sure what is the best way to fix

nikomatsakis (Nov 21 2018 at 17:27, on Zulip):

or to think about the fix, that is

nikomatsakis (Nov 21 2018 at 17:28, on Zulip):

we could certainly make the compiler accept the code

nikomatsakis (Nov 21 2018 at 17:28, on Zulip):

I am...like 99.5% sure that would be fine :) but I'm nervous about accepting a bit too much

Alexander Regueiro (Nov 21 2018 at 17:32, on Zulip):

@nikomatsakis yeah...

Alexander Regueiro (Nov 21 2018 at 17:32, on Zulip):

@nikomatsakis "The key thing here is that TyCtxt is invariant w/r/t 'tcx and 'gcx, so they must appear in the return type." -- could you clarify why they must appear in the return type because they're invariant?

Alexander Regueiro (Nov 21 2018 at 17:34, on Zulip):

but generally, behaviour akin to dyn Trait sounds sensible to me

nikomatsakis (Nov 21 2018 at 17:34, on Zulip):

well

nikomatsakis (Nov 21 2018 at 17:35, on Zulip):

let's say the type was &'a T

nikomatsakis (Nov 21 2018 at 17:35, on Zulip):

in that case, there is a subtyping relationship: &'a T <: &'b T if 'a: 'b

nikomatsakis (Nov 21 2018 at 17:35, on Zulip):

in other words, you can view a &'a T as an &'b T where 'b is some shorter lifetime

nikomatsakis (Nov 21 2018 at 17:36, on Zulip):

however, you cannot view a TyCtxt<.., 'gcx, 'tcx> as a TyCtxt<.., 'gcx1, 'tcx1> where '[gt]cx1 are shorter

nikomatsakis (Nov 21 2018 at 17:36, on Zulip):

this is because we have shared, mutable state in the TyCtxt (e.g., RefCell<HashMap<..>>)

nikomatsakis (Nov 21 2018 at 17:36, on Zulip):

if you were to just approximate the 'tcx lifetime, you could store some random stuff from your stack into one of those hashmaps

nikomatsakis (Nov 21 2018 at 17:37, on Zulip):

point is, if we are trying to view a &'a T as a Foo<'b> where the hidden type is &'b u32, that's ok

nikomatsakis (Nov 21 2018 at 17:37, on Zulip):

since everything of type &'a T is also of type &'b T (so long as 'b is shorter)

nikomatsakis (Nov 21 2018 at 17:38, on Zulip):

(if it helps, think of Java: basically, if the hidden type were Object, it'd be ok to put a String in there)

nikomatsakis (Nov 21 2018 at 17:38, on Zulip):

(because a String is an Object)

Alexander Regueiro (Nov 21 2018 at 17:39, on Zulip):

hmmm

Alexander Regueiro (Nov 21 2018 at 17:40, on Zulip):

I know what invariant means, but why must they appear in the return type @nikomatsakis ?

nikomatsakis (Nov 21 2018 at 17:44, on Zulip):

point is: the return type is some supertype of TyCtxt<'tcx> (ignoring the others for now since they don't matter)

nikomatsakis (Nov 21 2018 at 17:45, on Zulip):

but all such supertypes still talk about 'tcx

nikomatsakis (Nov 21 2018 at 17:45, on Zulip):

or 'gcx, whichever one we are trying to hide

nikomatsakis (Nov 21 2018 at 17:45, on Zulip):

so, the hidden type will always include 'gcx

Alexander Regueiro (Nov 21 2018 at 17:46, on Zulip):

makes sense @nikomatsakis. but then why are we allowed to do it with dyn Trait, which is also a supertype, surely?

nikomatsakis (Nov 21 2018 at 18:29, on Zulip):

it is not a supertype

nikomatsakis (Nov 21 2018 at 18:30, on Zulip):

you can construct a dyn (Trait + 'b) from any type T where (a) T: Trait and T: 'b (there is always a bound 'b, though it often defaults to 'static)

nikomatsakis (Nov 21 2018 at 18:30, on Zulip):

in other words, converting into a dyn Trait loses static type information

nikomatsakis (Nov 21 2018 at 18:30, on Zulip):

we don't know exactly which lifetimes are contained within

nikomatsakis (Nov 21 2018 at 18:30, on Zulip):

this is not true for impl Trait, actually, at least not presently

nikomatsakis (Nov 21 2018 at 18:31, on Zulip):

I think the "proper" way to view this is that we would have some kind of "exists<'gcx: 'tcx> T` operator that lets you hide a lifetime from any type

Alexander Regueiro (Nov 21 2018 at 19:21, on Zulip):

yeah, I discussed that with @centril before briefly. I think we could even reuse for<>?

Alexander Regueiro (Nov 21 2018 at 19:21, on Zulip):

and good point about it not being a supertype

Alexander Regueiro (Jan 18 2019 at 21:21, on Zulip):

@Taylor Cramer some prior discussion I just remembered, if you're interested. ^

Last update: Nov 12 2019 at 16:20UTC