Skip to content

"non-defining opaque type use" with equal bound regions due to closure #112841

Closed
@lcnr

Description

@lcnr
Contributor
#![feature(type_alias_impl_trait)]
trait Trait<'a, 'b> {}
impl Trait<'_, '_> for () {}
type Tait<'a, 'b> = impl Trait<'a, 'b>;
fn fail<'a: 'b, 'b: 'a>() -> Tait<'a, 'b> {
    (|| {})()
}

results in the following but should ideally compile.

error: non-defining opaque type use in defining scope
 --> src/main.rs:6:5
  |
6 |     (|| {})()
  |     ^^^^^^^^^
  |
note: lifetime used multiple times
 --> src/main.rs:4:11
  |
4 | type Tait<'a, 'b> = impl Trait<'a, 'b>;
  |           ^^  ^^

Activity

changed the title [-]non-defining opaque type use with equal bound regions[/-] [+]non-defining opaque type use with equal bound regions due to closure[/+] on Jun 20, 2023
lcnr

lcnr commented on Jun 20, 2023

@lcnr
ContributorAuthor

I think the issue is that the closure erases regions in its signature and does not have any user type, so its generic params for the opaque aren't really sensible.

I feel like maybe closures should return their opaque constraints via https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/struct.BorrowCheckResult.html#structfield.closure_requirements instead of dealing with them itself?

moved this to Can do after stabilization in type alias impl trait stabilizationon Jun 20, 2023
changed the title [-]non-defining opaque type use with equal bound regions due to closure[/-] [+]"non-defining opaque type use" with equal bound regions due to closure[/+] on Jun 20, 2023
compiler-errors

compiler-errors commented on Jun 21, 2023

@compiler-errors
Member

why does this fail even if a closure is just defined in-scope? 🤔

#![feature(type_alias_impl_trait)]

trait Trait<'a, 'b> {}

impl Trait<'_, '_> for () {}

type Tait<'a, 'b> = impl Trait<'a, 'b>;

fn fail<'a: 'b, 'b: 'a>() -> Tait<'a, 'b> {
    let x = || {};

    ()
}

fn main() {}
compiler-errors

compiler-errors commented on Jun 21, 2023

@compiler-errors
Member

idk, maybe pinging @aliemjay who knows more about borrowck + closures 🤣

aliemjay

aliemjay commented on Jun 21, 2023

@aliemjay
Member

I disagree that the OP's code should pass. Even more, I think it is a bug that we accept this:

#![feature(type_alias_impl_trait)]
trait Trait<'a, 'b> {}
impl Trait<'_, '_> for () {}
type Tait<'a, 'b> = impl Trait<'a, 'b>;
fn fail<'a: 'b, 'b: 'a>() -> Tait<'a, 'b> {
    ()
}

because it is in no way different than writing fn fail<'a>() -> Tait<'a, 'a> { ... }, which we currently reject and for a good reason.

The reason we accept these illegal defining uses of TAIT is due to a bug in

let to_universal_region = |vid, subst_regions: &mut Vec<_>| {

which evaluates lifetime equality based on the constraint graph alone, ignoring the the environment bounds. This is why this trivial change triggers the error:

fn fail<'a: 'b, 'b: 'a>() -> Tait<'a, 'b> {
+   let _ = None::<&'a &'b &'a ()>; // Now 'a and 'b are equal in the constraint graph
    ()
+   //~^ ERROR non-defining opaque type use in defining scope
}

and this explains why using closures anywhere in the body triggers the error, because it somehow adds the environmet bounds (on early-bound lifetimes exclusively) to the constraint graph.

added 3 commits that reference this issue on Oct 19, 2023

Auto merge of rust-lang#116891 - aliemjay:opaque-region-infer-rework-…

c679690

Auto merge of rust-lang#116891 - aliemjay:opaque-region-infer-rework-…

e287a7d

Auto merge of rust-lang#116891 - aliemjay:opaque-region-infer-rework-…

66abdf7
added a commit that references this issue on Mar 28, 2024

Auto merge of rust-lang#116891 - aliemjay:opaque-region-infer-rework-…

551abd6
moved this from Can do after stabilization to Done in type alias impl trait stabilizationon Mar 28, 2024
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

    C-bugCategory: This is a bug.F-type_alias_impl_trait`#[feature(type_alias_impl_trait)]`

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @compiler-errors@aliemjay@lcnr

      Issue actions

        "non-defining opaque type use" with equal bound regions due to closure · Issue #112841 · rust-lang/rust