Skip to content

Confusing error message with conservative impl trait #39722

@iopq

Description

@iopq
#![feature(conservative_impl_trait)]

fn main() {
    let mut x = &mut 1i32;
    let closure = return_closure(x);
    closure();
}


fn return_closure(x: &mut i32) -> impl Fn() {
    move || println!("{}", x)
}

this gives the error message

error[E0564]: only named lifetimes are allowed in `impl Trait`, but `` was found in the type `[closure@<anon>:11:5: 11:30 x:&mut i32]`
  --> <anon>:10:35
   |
10 | fn return_closure(x: &mut i32) -> impl Fn() {
   |                                   ^^^^^^^^^

but that's wrong, if we amend the code to

#![feature(conservative_impl_trait)]

fn main() {
    let mut x = &mut 1i32;
    let closure = return_closure(x);
    closure();
}


fn return_closure<'a>(x: &'a mut i32) -> impl Fn() {
    move || println!("{}", x)
}

it compiles and runs just fine, so the previous error message is incorrect

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions