Skip to content

impl Trait capturing lifetime of type parameter #79415

Closed
@Plecra

Description

@Plecra

I tried this code:

fn erase<T>(_: T) -> impl core::fmt::Debug + 'static {}
fn create(v: ()) -> Box<dyn core::fmt::Debug> {
    Box::new(erase(&v))
}

I expected it to compile, but there was an error:

error[E0515]: cannot return value referencing function parameter `v`
 --> src/lib.rs:3:5
  |
3 |     Box::new(erase(&v))
  |     ^^^^^^^^^^^^^^^--^^
  |     |              |
  |     |              `v` is borrowed here
  |     returns a value referencing data owned by the current function

Meta

rustc --version --verbose:

rustc 1.48.0 (7eac88abb 2020-11-16)
binary: rustc
commit-hash: 7eac88abb2e57e752f3302f02be5f3ce3d7adfb4
commit-date: 2020-11-16
host: x86_64-pc-windows-msvc
release: 1.48.0
LLVM version: 11.0
rustc 1.50.0-nightly (a0d664bae 2020-11-22)       
binary: rustc
commit-hash: a0d664bae6ca79c54cc054aa2403198e105190a2
commit-date: 2020-11-22
host: x86_64-pc-windows-msvc
release: 1.50.0-nightly

The issue also affects -Zpolonius

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.A-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.fixed-by-TAITFixed by the feature `type_alias_impl_trait`.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions