Skip to content

Conflicting error messages for HRTB in impl Trait #69112

@JustAPerson

Description

@JustAPerson

Rust declares the lifetime 'a is both undeclared and that it is a shadowing declaration

#[derive(Debug)]
struct Ctx<'d> {
    phantom: std::marker::PhantomData<&'d ()>   
}
fn map<'c, 'd: 'c>(ctx: &'c mut Ctx<'d>, data: Vec<()>) -> impl for<'a> Iterator<Item = ()> + 'a where 'c: 'a {
    data.into_iter().inspect(|t| println!("{:?}", ctx))
}

My more complicated example is trying to use the &'c mut Ctx<'d> reference in one of the closures passed to Iterator::map() so I need to somehow incorporate the lifetime 'c into the return type.

Instead I get this confusing error:

error[E0261]: use of undeclared lifetime name `'a`
 --> src/lib.rs:5:95
  |
5 | fn map<'c, 'd: 'c>(ctx: &'c mut Ctx<'d>, data: Vec<()>) -> impl for<'a> Iterator<Item = ()> + 'a where 'c: 'a {
  |                                                                                               ^^ undeclared lifetime

error[E0496]: lifetime name `'a` shadows a lifetime name that is already in scope
 --> src/lib.rs:5:69
  |
5 | fn map<'c, 'd: 'c>(ctx: &'c mut Ctx<'d>, data: Vec<()>) -> impl for<'a> Iterator<Item = ()> + 'a where 'c: 'a {
  |                                                                     ^^                        -- first declared here
  |                                                                     |
  |                                                                     lifetime 'a already in scope

error[E0261]: use of undeclared lifetime name `'a`
 --> src/lib.rs:5:108
  |
5 | fn map<'c, 'd: 'c>(ctx: &'c mut Ctx<'d>, data: Vec<()>) -> impl for<'a> Iterator<Item = ()> + 'a where 'c: 'a {
  |                                                                                                            ^^ undeclared lifetime

error: aborting due to 3 previous errors

You'll notice the help text of the first error says "undeclared lifetime" but the help of the second says "first declared here" and then gives a bogus "already in scope" hint.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.A-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.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