Skip to content

impl_trait_in_bindings and pick-constraint region bounds #61773

Open
@nikomatsakis

Description

@nikomatsakis

I'm working on #56238. In the process, I'm extending how impl Trait lifetime inference works -- in particular in scenarios involving multiple, unrelated lifetimes, such as impl Trait<'a, 'b>. The challenge here is that each region 'h in the hidden type must be equal to 'a or 'b, but we can't readily express that relationship in terms of our usual "outlives relationships". The solver is thus extended with a "pick constraint", written pick 'h from ['a, 'b], which expresses that 'h must be equal to 'a or 'b. The current integration into the solver, however, requires that the regions involved are lifetime parameters. This is always true for impl Trait used at function boundaries, but it is not true for let bindings.

The challenge is that if you have a program like:

#![feature(impl_trait_in_bindings)]

trait Foo<'a> { }
impl Foo<'_> for &u32 { }

fn main() {
  let _: impl Foo<'_> = &44; // let's call the region variable for `'_` `'1`
}

then we would wind up with pick '0 from ['1, 'static], where '0 is the region variable in the hidden type (&'0 u32) and '1 is the region variable in the bounds Foo<'1>. This is tricky because both '0 and '1 are being inferred -- so making them equal may have other repercussions.

For the time being, I've chosen to include some assertions that this scenario never comes up. I'm tagging a FIXME in the code with this issue number. I was going to create some tests, but owing to the ICE #60473 (not unrelated to this issue, actually), that proved difficult, so I'll just post comments in here instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.F-impl_trait_in_bindings`#![feature(impl_trait_in_bindings)]`F-member_constraints`#[feature(member_constraints)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions