Skip to content

type-alias-impl-trait with lifetime param can't be used in fn input #96564

Closed
@aliemjay

Description

@aliemjay

I tried this code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=f758d0536248cf9a970114bf75257429

#![feature(type_alias_impl_trait)]
type Ty<'a> = impl FnOnce() -> &'a str;
fn defining(s: &str) -> Ty<'_> { move || s }
fn execute(ty: Ty<'_>) -> &str { ty() }

I expected to see this happen: program compiles

Instead, this happened:

error[E0581]: return type references an anonymous lifetime, which is not constrained by the fn input types
 --> src/lib.rs:4:27
  |
4 | fn execute(ty: Ty<'_>) -> &str { ty() }
  |                           ^^^^
  |
  = note: lifetimes appearing in an associated type are not considered constrained

Same code compiles if type params are used instead:

#![feature(type_alias_impl_trait)]
type Ty<T> = impl FnOnce() -> T;
fn defining<T>(s: T) -> Ty<T> { move || s }
fn execute<T>(ty: Ty<T>) -> T { ty() }

Meta

Nightly playground : 1.62.0-nightly (2022-04-28 e85edd9)

Backtrace

<backtrace>

Metadata

Metadata

Assignees

Labels

A-borrow-checkerArea: The borrow checkerC-bugCategory: This is a bug.F-type_alias_impl_trait`#[feature(type_alias_impl_trait)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions