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

False positive for "lifetime must be declared" error in 2018 preview #51008

Closed
alexcrichton opened this issue May 23, 2018 · 4 comments
Closed
Assignees
Labels
A-edition-2018 Area: The 2018 edition F-rust_2018_preview `#![feature(rust_2018_preview)]`

Comments

@alexcrichton
Copy link
Member

This code:

#![feature(rust_2018_preview)]

trait A {
    
}

impl<F> A for F where F: FnOnce(&i32) {}

fn main() {}

yields:

error[E0687]: lifetimes used in `fn` or `Fn` syntax must be explicitly declared using `<...>` binders
 --> src/main.rs:7:33
  |
7 | impl<F> A for F where F: FnOnce(&i32) {}
  |                                 ^ in-band lifetime definition

error: aborting due to previous error

For more information about this error, try `rustc --explain E0687`.
@alexcrichton alexcrichton added the F-rust_2018_preview `#![feature(rust_2018_preview)]` label May 23, 2018
@nikomatsakis
Copy link
Contributor

cc @cramertj =)

@nikomatsakis
Copy link
Contributor

(I'm investigating a bit)

@nikomatsakis
Copy link
Contributor

Got a fix coming

@nikomatsakis
Copy link
Contributor

#51220

bors added a commit that referenced this issue May 31, 2018
…me-must-be-declared, r=cramertj

reset anonymous-lifetime-mode as we enter `()` scopes

Background:

The anonymous lifetime mode is used to prohibit elided lifetimes where
they didn't used to be permitted, and instead require that `'_` be
used. For example:

```rust
impl Trait for Ref<T> { .. }
//             ^^^^^^ ERROR: should be `Ref<'_, T>`
```

When we are parsing the parts of the impl header, we enter into an alternate mode called `CreateParameter`. In this mode, we give an error for things like `Ref<T>`, but for elided lifetimes in a reference type like `&T` we make the elided lifetime into an in-band lifetime:

https://github.com/rust-lang/rust/blob/4f99f37b7e213d69a489884f651adfc6d217cef5/src/librustc/hir/lowering.rs#L4017-L4035

This was not intended to change behavior because we only enter into that mode in contexts where elision was not historically permitted. However, the problem is that we fail to reset the mode when we enter into bounds like `Fn(&u32)`, where elision *was* allowed -- the same occurs for fn types like `fn(&u32`). This PR restores the original mode in those contexts.

Fixes #51008

r? @cramertj
@fmease fmease added the A-edition-2018 Area: The 2018 edition label Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-edition-2018 Area: The 2018 edition F-rust_2018_preview `#![feature(rust_2018_preview)]`
Projects
None yet
Development

No branches or pull requests

3 participants