Skip to content

Unify lifetime elision for impl Trait arguments between async and non-async functions #85417

Open

Description

@LukasKalbertodt

This compiles:

async fn foo(_: impl Iterator<Item = &u32>) {}

But this doesn't:

fn foo(_: impl Iterator<Item = &u32>) {}
error[E0106]: missing lifetime specifier
 --> src/lib.rs:1:32
  |
1 | fn foo(_: impl Iterator<Item = &u32>) {}
  |                                ^ expected named lifetime parameter
  |
help: consider introducing a named lifetime parameter
  |
1 | fn foo<'a>(_: impl Iterator<Item = &'a u32>) {}
  |       ^^^^                         ^^^

This difference strikes me as very odd. Is this intended? If so, why?

This is formulated as a question, but if I had to guess, I would classify this as oversight or bug -- that's why I created an issue instead of askig in the forum.

(originally asked here)

Activity

  1. added
    A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.
    A-lifetimesArea: Lifetimes / regions
    AsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.
    on May 21, 2021
  2. tmandry commented on May 21, 2021

    @tmandry
    Member

    We discussed this in our meeting today. The difference in behavior comes from this code:

    let lt_mode = if make_ret_async.is_some() {
    // In `async fn`, argument-position elided lifetimes
    // must be transformed into fresh generic parameters so that
    // they can be applied to the opaque `impl Trait` return type.
    AnonymousLifetimeMode::CreateParameter
    } else {
    self.anonymous_lifetime_mode
    };

    While we decided that async fn probably exhibits the right behavior, it's possible this could affect other things too which could lead to bugs. Before declaring this fixed we should account for anything that might appear in arguments and be affected by this code.

    We also noticed that in sync code, we aren't consistent in the behavior of impl Trait and dyn Trait. For example:

    trait Something { type Item; }
    fn foo(x: &dyn Something<Item = &u32>) { }   // works fine
    fn foo(x: &impl Something<Item = &u32>) { }  // error

    We should be more consistent and document the elision rules for all these cases in the reference. Since dyn already accepts this, we decided we should go ahead and accept this for impl Trait too.

  3. eholk commented on Oct 4, 2021

    @eholk
    Contributor

    This seems sort of related to #74256 and #74597.

  4. estebank commented on Aug 16, 2022

    @estebank
    Contributor

    The output of this is regressing in 1.64.

    1.63:

    error[[E0106]](https://doc.rust-lang.org/stable/error-index.html#E0106): missing lifetime specifier
     --> src/lib.rs:1:32
      |
    1 | fn foo(_: impl Iterator<Item = &u32>) {}
      |                                ^ expected named lifetime parameter
      |
    help: consider introducing a named lifetime parameter
      |
    1 | fn foo<'a>(_: impl Iterator<Item = &'a u32>) {}
      |       ++++                          ++
    

    1.64:

    error[[E0658]](https://doc.rust-lang.org/beta/error-index.html#E0658): anonymous lifetimes in `impl Trait` are unstable
     --> src/lib.rs:1:32
      |
    1 | fn foo(_: impl Iterator<Item = &u32>) {}
      |                                ^
    
  5. cjgillot commented on Sep 1, 2022

    @cjgillot
    Contributor

    #97720 originally suggested to remove the difference between the async and regular fns. In order to avoid and insta-stable change, the choice was made to introduce the feature gate instead in #97720 (comment).

    Removing this feature gate is trivial: revert 5a20834.
    Re-introducing suggestions there is more complicated: the feature gate happens after the suggestion code. (AST-based resolution accepts the code and does not suggest anything, and HIR-based lifetime resolution issues the feature gate later.)

  6. moved this to On deck in wg-async workon Dec 8, 2022
  7. added
    C-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.
    on Mar 20, 2026
  8. changed the title [-]Is this difference in lifetime elision for `impl Trait` arguments between async and non-async function intended?[/-] [+]Unify lifetime elision for `impl Trait` arguments between async and non-async functions[/+] on Mar 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-awaitArea: Async & AwaitA-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.A-lifetimesArea: Lifetimes / regionsAsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.C-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

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions