Skip to content
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

nll allows &'static Fn() to contain function local upvars #52384

Closed
oli-obk opened this issue Jul 14, 2018 · 5 comments
Closed

nll allows &'static Fn() to contain function local upvars #52384

oli-obk opened this issue Jul 14, 2018 · 5 comments
Assignees
Labels
A-NLL Area: Non-lexical lifetimes (NLL) NLL-sound Working towards the "invalid code does not compile" goal

Comments

@oli-obk
Copy link
Contributor

oli-obk commented Jul 14, 2018

#![feature(nll)]

fn main() {
    let x = 42;
    let _: &'static _ = &|| { let x = x; x };
}

I don't think this should compile. I didn't test whether we can actually abuse this to do unsoundness, but this snippet seems very unintuitive

@oli-obk oli-obk added A-NLL Area: Non-lexical lifetimes (NLL) NLL-sound Working towards the "invalid code does not compile" goal labels Jul 14, 2018
@matthewjasper
Copy link
Contributor

I think this is #47184, as this doesn't compile

fn is_static<F: 'static>(f: F) -> F { f }

fn foo() {
    let x = 42;
    is_static(&|| { let x = x; x });
}

@nikomatsakis
Copy link
Contributor

I don't think this is #47184 exactly -- we are enforcing user-given types in this particular case, but I think we are probably not requiring them to be well-formed. As a result, if the type of _ is &'static T, we don't end up requiring that T: 'static.

I suspect that you would have a similar error with something like struct Foo<T: 'static> and an annotation let x: Foo<_> = ....

This ought to be fixed for EP2. Nominating.

@nikomatsakis nikomatsakis added this to the Rust 2018 Preview 2 milestone Jul 17, 2018
@nikomatsakis nikomatsakis self-assigned this Jul 17, 2018
@nikomatsakis
Copy link
Contributor

Assigning to myself as this intersects my branch for #48071 quite a bit.

@nikomatsakis
Copy link
Contributor

Actually, I think @matthewjasper was right! In particular, I think we don't currently enforce type annotations that are placed on patterns that are not variable bindings. So e.g. this does not compile:

#![feature(nll)]
#![allow(warnings)]

fn main() {
    let x = 42;
    let y: &'static _ = &|| { let x = x; x };
}

@nikomatsakis
Copy link
Contributor

Going to close as a duplicate of #47184

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-NLL Area: Non-lexical lifetimes (NLL) NLL-sound Working towards the "invalid code does not compile" goal
Projects
None yet
Development

No branches or pull requests

4 participants