Closed
Description
#![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>;
| ^^ ^^
Metadata
Metadata
Assignees
Type
Projects
Status
Done
Activity
[-]non-defining opaque type use with equal bound regions[/-][+]non-defining opaque type use with equal bound regions due to closure[/+]lcnr commentedon Jun 20, 2023
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?
[-]non-defining opaque type use with equal bound regions due to closure[/-][+]"non-defining opaque type use" with equal bound regions due to closure[/+]compiler-errors commentedon Jun 21, 2023
why does this fail even if a closure is just defined in-scope? 🤔
compiler-errors commentedon Jun 21, 2023
idk, maybe pinging @aliemjay who knows more about borrowck + closures 🤣
aliemjay commentedon Jun 21, 2023
I disagree that the OP's code should pass. Even more, I think it is a bug that we accept this:
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
rust/compiler/rustc_borrowck/src/region_infer/opaque_types.rs
Line 80 in 776f222
which evaluates lifetime equality based on the constraint graph alone, ignoring the the environment bounds. This is why this trivial change triggers the error:
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.
Auto merge of rust-lang#116891 - aliemjay:opaque-region-infer-rework-…
Auto merge of rust-lang#116891 - aliemjay:opaque-region-infer-rework-…
Auto merge of rust-lang#116891 - aliemjay:opaque-region-infer-rework-…
Auto merge of rust-lang#116891 - aliemjay:opaque-region-infer-rework-…