New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow hidden lifetimes in `impl Trait` #57870

Open
wants to merge 1 commit into
base: master
from

Conversation

Projects
None yet
8 participants
@cramertj
Copy link
Member

cramertj commented Jan 24, 2019

This is a revert of fc3c90c, which made it an error
to have a hidden lifetime inside an impl Trait return
type even when the hidden lifetime outlived other lifetimes
mentioned by name in the impl Trait bound.

Partial revert of #49041.
cc @alexreg

r? @nikomatsakis

Allow hidden lifetimes in `impl Trait`
This is a revert of fc3c90c, which made it an error
to have a hidden lifetime inside an `impl Trait` return
type even when the hidden lifetime outlived other lifetimes
mentioned by name in the `impl Trait` bound.
@alexreg

This comment has been minimized.

Copy link
Contributor

alexreg commented Jan 24, 2019

Looks good. Should we include a comment with some theoretical justification for this behaviour, as per @nikomatsakis's explanation on Zulip (implicit existential quantification over hidden lifetimes, etc.)?

@Centril

This comment has been minimized.

Copy link
Contributor

Centril commented Jan 24, 2019

@cramertj @nikomatsakis This seems right; but do we want to discuss this on a meeting / fcp-merge since it affects user observable type system behavior? Should also document this in the reference or some place less ephemeral than the code?

EDIT: I've tentatively added needs-fcp but if for some reason you don't think so (+ add that reason) feel free to remove it.

@matthewjasper

This comment has been minimized.

Copy link
Contributor

matthewjasper commented Jan 24, 2019

This isn't sound:

trait Swap: Sized {
    fn swap(self, other: Self);
}

impl<T> Swap for &mut T {
    fn swap(self, other: Self) {
        std::mem::swap(self, other);
    }
}

fn hide<'a, 'b, T: 'static>(x: &'a mut &'b T) -> impl Swap + 'a {
    x
}

fn dangle() -> &'static [i32; 3] {
    let mut res = &[4, 5, 6];
    let x = [1, 2, 3];
    hide(&mut res).swap(hide(&mut &x));
    res
}

fn main() {
    println!("{:?}", dangle()); // prints nonsense values
}
@alexreg

This comment has been minimized.

Copy link
Contributor

alexreg commented Jan 24, 2019

I think we need to add the condition that the hidden lifetime outlives the lifetime mentioned in the opaque type?

@matthewjasper

This comment has been minimized.

Copy link
Contributor

matthewjasper commented Jan 24, 2019

No, we need the currently implemented condition: 'b: 'a in the above example.

@alexreg

This comment has been minimized.

Copy link
Contributor

alexreg commented Jan 24, 2019

@matthewjasper Huh? Isn't that exactly what I said?

@@ -2011,53 +2011,6 @@ a (non-transparent) struct containing a single float, while `Grams` is a
transparent wrapper around a float. This can make a difference for the ABI.
"##,

E0700: r##"

This comment has been minimized.

@estebank

estebank Jan 25, 2019

Contributor

I don't think we outright remove error code descriptions we don't emit anymore (at least not if they have hit stable, I'm sure).

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Jan 30, 2019

This isn't sound:

Yep. So, my "basis" for believing that we can make something similar to (but presumably not quite the same as) this PR work is that we are able to "hide" lifetimes in a dyn Trait existential. But those types always have a bound and all the hidden lifetimes must outlive that bound. (Effectively, it is the intersection of all the hidden lifetimes). I guess we would want some similar-ish concept for impl Trait.

(In fact, if I recall, we do kind of compute such a region today -- that is, I think we forbid you from doing impl Trait + 'a + 'b unless either 'a: 'b or 'b: 'a, right?)

@Dylan-DPC

This comment has been minimized.

Copy link
Member

Dylan-DPC commented Feb 11, 2019

ping from triage @nikomatsakis what's the update on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment