Skip to content

member constraints are order-dependent #140569

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

Open
lcnr opened this issue May 2, 2025 · 0 comments
Open

member constraints are order-dependent #140569

lcnr opened this issue May 2, 2025 · 0 comments
Assignees
Labels
A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. A-NLL Area: Non-lexical lifetimes (NLL) C-bug Category: This is a bug. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@lcnr
Copy link
Contributor

lcnr commented May 2, 2025

struct Inv<'a>(*mut &'a ());
fn mk<'m>() -> (Inv<'m>, Inv<'m>) {
    loop {}
}
fn ok<'a, 'b: 'a>() -> (impl Sized + use<'a>, impl Sized + use<'b>) {
    mk()
}
fn err<'a, 'b: 'a>() -> (impl Sized + use<'b>, impl Sized + use<'a>) {
    mk()
}

this fails with

error[E0700]: hidden type for `impl Sized` captures lifetime that does not appear in bounds
 --> src/lib.rs:9:5
  |
8 | fn err<'a, 'b: 'a>() -> (impl Sized + use<'b>, impl Sized + use<'a>) {
  |            --                                  -------------------- opaque type defined here
  |            |
  |            hidden type `Inv<'b>` captures the lifetime `'b` as defined here
9 |     mk()
  |     ^^^^
  |
help: add `'b` to the `use<...>` bound to explicitly capture it
  |
8 | fn err<'a, 'b: 'a>() -> (impl Sized + use<'b>, impl Sized + use<'a, 'b>) {
  |                                                                   ++++

We've got 'm member ['a, 'static] and 'm member ['b, 'static]. The final region chosen for 'm depends on the order in which we apply these constraints:

  • 'm member ['b, 'static] chooses 'b
  • 'm member ['a, 'static] as 'a: 'b does not hold, this has to choose 'static
  • we end up with 'm = 'static which satisfies both member constraints

and alternatively:

  • 'm member ['a, 'static] chooses 'a
  • 'm member ['b, 'static] can still choose 'b as 'b: 'a holds
  • we end up with 'm = 'b which means that 'm member ['a, 'static] does not hold

Fixing this is not too difficult, we can "simply" apply member constraints for each member region until we reach a fixpoint. I will implement this separately once #139587 landed.

@lcnr lcnr added A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. A-NLL Area: Non-lexical lifetimes (NLL) labels May 2, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 2, 2025
@lcnr lcnr changed the title `member constraints are order-dependent member constraints are order-dependent May 2, 2025
@lcnr lcnr self-assigned this May 2, 2025
@fmease fmease added C-bug Category: This is a bug. T-types Relevant to the types team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 2, 2025
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-NLL Area: Non-lexical lifetimes (NLL) C-bug Category: This is a bug. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants