rust-lang / rust-lang/rust

member constraints are order-dependent

Open
#140,569 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-impl-trait A-member-constraints A-NLL C-bug T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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. we can implement this separately once https://github.com/rust-lang/rust/pull/139587 landed.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the provided Rust example and confirming that the result depends on the order of member constraints. Read the referenced PR 139587 before locating the member-constraint handling in the compiler. Done means applying constraints to a fixpoint so both orderings produce a consistent valid result.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.