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

impl Trait capturing lifetime of type parameter #79415

Closed
Plecra opened this issue Nov 25, 2020 · 4 comments
Closed

impl Trait capturing lifetime of type parameter #79415

Plecra opened this issue Nov 25, 2020 · 4 comments
Labels
A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. fixed-by-TAIT Fixed by the feature `type_alias_impl_trait`. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Plecra
Copy link

Plecra commented Nov 25, 2020

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

@Plecra Plecra added the C-bug Category: This is a bug. label Nov 25, 2020
@jonas-schievink
Copy link
Contributor

I think this works as intended. impl Trait captures all type parameters, and T is not 'static, so the lifetime of the parameter is assumed to be contained in the returned value.

@Plecra
Copy link
Author

Plecra commented Nov 25, 2020

Does that mean impl Trait can't express types that are separate from its arguments in any way? Is the 'static bound in the return position somehow ambiguous?

This behaviour seems very strange to me, and isn't mentioned in the original RFC, the reference or the Book. Why would impl Trait be defined to silently ignore its lifetime?

@camelid camelid added A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. A-lifetimes Area: Lifetimes / regions labels Nov 25, 2020
@estebank estebank added D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 16, 2021
@estebank
Copy link
Contributor

#82171 (comment):

There's no way currently to express that an impl Trait doesn't capture all the input lifetimes. Once type Ret<'a> = impl Trait + 'a; is stabilized you could express the behavior you're looking for, but for now the only option are the ones identified in the report (explicit type and Box<dyn Trait>).

@estebank estebank added the F-type_alias_impl_trait `#[feature(type_alias_impl_trait)]` label Feb 16, 2021
@oli-obk oli-obk added fixed-by-TAIT Fixed by the feature `type_alias_impl_trait`. and removed F-type_alias_impl_trait `#[feature(type_alias_impl_trait)]` labels Feb 18, 2021
@estebank
Copy link
Contributor

estebank commented Jan 8, 2023

The original code now compiles. Closing this in favor of #82171.

@estebank estebank closed this as completed Jan 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. fixed-by-TAIT Fixed by the feature `type_alias_impl_trait`. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants