Skip to content

borrowck: Represent 'best blame constraint' as index into Vec<OutlivesConstraint>#158623

Draft
Enselic wants to merge 4 commits into
rust-lang:mainfrom
Enselic:best-blame
Draft

borrowck: Represent 'best blame constraint' as index into Vec<OutlivesConstraint>#158623
Enselic wants to merge 4 commits into
rust-lang:mainfrom
Enselic:best-blame

Conversation

@Enselic

@Enselic Enselic commented Jun 30, 2026

Copy link
Copy Markdown
Member

This makes the code simpler and easier to understand, since we don't have to partially copy an OutlivesConstraint into the BlameConstraint struct (which we can remove entirely) and the BorrowExplanation::MustBeValidFor enum variant (which we can simplify).

Instead, we reference the "best" OutlivesConstraint by an index into Vec<OutlivesConstraint>. This is not only simpler, it also opens up the possibility for diagnostics to access the full OutlivesConstraint info.

I discovered this cleanup while investigating ways to solve #138408.

Draft

This PR is not ready for review. I will split this up into smaller PRs for easier review. The purpose of this PR is to:

  • See if CI pass.
  • Illustrate where the smaller PRs will take us.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 30, 2026
@Enselic Enselic added the C-cleanup Category: PRs that clean code up or issues documenting cleanup. label Jun 30, 2026
@rust-log-analyzer

This comment has been minimized.

@Enselic Enselic closed this Jun 30, 2026
@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jun 30, 2026
@Enselic Enselic reopened this Jun 30, 2026
@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jun 30, 2026
@rust-log-analyzer

This comment has been minimized.

Enselic added 4 commits June 30, 2026 23:58
…g new one

So that we in an upcoming commit can return the index into path instead
of a "duplicate".

This passes:

    ./x test --bless tests/ui --keep-stage-std 1 --set rust.deny-warnings=false
…esConstraint>`

This makes the code simpler and easier to understand, since we don't
have to partially copy an `OutlivesConstraint` into the
`BlameConstraint` struct (which we can remove entirely) and the
`BorrowExplanation::MustBeValidFor` enum variant (which we can
simplify).

Instead, we reference the "best" `OutlivesConstraint` by an index into
`Vec<OutlivesConstraint>`. This is not only simpler, it also  opens up
the possibility for diagnostics to access the full `OutlivesConstraint`
info.
jhpratt added a commit to jhpratt/rust that referenced this pull request Jul 3, 2026
…ath, r=JohnTitor

borrowck: Introduce BlameConstraint::to_obligation_cause_from_path()

Before this commit, `BlameConstraint` is a bit hard to understand at first, because of its `ObligationCause` field. In practice, `BlameConstraint` is just a slimmed down version of an `OutlivesConstraint` from `Vec<OutlivesConstraint>`.

The artificial `ObligationCause` is only needed in one place. Rather than having that exception polute the lower level logic, bubble it up as much as we can. That way, it becomes clearer what `BlameConstraint` actually is.

For reference, `ObligationCause` was introdued by rust-lang#89249, but the codebase looked quite different back then.

This takes us one step closer (this is commit 1 of 4) towards the end result illustrated in rust-lang#158623.

# Notes

I have removed the comment

```rs
        // We try to avoid reporting a `ConstraintCategory::Predicate` as our best constraint.
        // Instead, we use it to produce an improved `ObligationCauseCode`.
````

because I think [this](https://github.com/rust-lang/rust/blob/e2af9c03ef4cfb769e391968a5cfbb1afe6f01e2/compiler/rustc_borrowck/src/region_infer/mod.rs#L1763-L1762) existing comment is sufficient:

```rs
                // We handle predicates and opaque types specially; don't prioritize them here.
                ConstraintCategory::Predicate(_) | ConstraintCategory::OpaqueType => 4,
```
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jul 5, 2026
…ath, r=JohnTitor

borrowck: Introduce BlameConstraint::to_obligation_cause_from_path()

Before this commit, `BlameConstraint` is a bit hard to understand at first, because of its `ObligationCause` field. In practice, `BlameConstraint` is just a slimmed down version of an `OutlivesConstraint` from `Vec<OutlivesConstraint>`.

The artificial `ObligationCause` is only needed in one place. Rather than having that exception polute the lower level logic, bubble it up as much as we can. That way, it becomes clearer what `BlameConstraint` actually is.

For reference, `ObligationCause` was introdued by rust-lang#89249, but the codebase looked quite different back then.

This takes us one step closer (this is commit 1 of 4) towards the end result illustrated in rust-lang#158623.

# Notes

I have removed the comment

```rs
        // We try to avoid reporting a `ConstraintCategory::Predicate` as our best constraint.
        // Instead, we use it to produce an improved `ObligationCauseCode`.
````

because I think [this](https://github.com/rust-lang/rust/blob/e2af9c03ef4cfb769e391968a5cfbb1afe6f01e2/compiler/rustc_borrowck/src/region_infer/mod.rs#L1763-L1762) existing comment is sufficient:

```rs
                // We handle predicates and opaque types specially; don't prioritize them here.
                ConstraintCategory::Predicate(_) | ConstraintCategory::OpaqueType => 4,
```
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jul 5, 2026
…ath, r=JohnTitor

borrowck: Introduce BlameConstraint::to_obligation_cause_from_path()

Before this commit, `BlameConstraint` is a bit hard to understand at first, because of its `ObligationCause` field. In practice, `BlameConstraint` is just a slimmed down version of an `OutlivesConstraint` from `Vec<OutlivesConstraint>`.

The artificial `ObligationCause` is only needed in one place. Rather than having that exception polute the lower level logic, bubble it up as much as we can. That way, it becomes clearer what `BlameConstraint` actually is.

For reference, `ObligationCause` was introdued by rust-lang#89249, but the codebase looked quite different back then.

This takes us one step closer (this is commit 1 of 4) towards the end result illustrated in rust-lang#158623.

# Notes

I have removed the comment

```rs
        // We try to avoid reporting a `ConstraintCategory::Predicate` as our best constraint.
        // Instead, we use it to produce an improved `ObligationCauseCode`.
````

because I think [this](https://github.com/rust-lang/rust/blob/e2af9c03ef4cfb769e391968a5cfbb1afe6f01e2/compiler/rustc_borrowck/src/region_infer/mod.rs#L1763-L1762) existing comment is sufficient:

```rs
                // We handle predicates and opaque types specially; don't prioritize them here.
                ConstraintCategory::Predicate(_) | ConstraintCategory::OpaqueType => 4,
```
@rust-bors

rust-bors Bot commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #158811) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

rust-timer added a commit that referenced this pull request Jul 5, 2026
Rollup merge of #158624 - Enselic:to_obligation_cause_from_path, r=JohnTitor

borrowck: Introduce BlameConstraint::to_obligation_cause_from_path()

Before this commit, `BlameConstraint` is a bit hard to understand at first, because of its `ObligationCause` field. In practice, `BlameConstraint` is just a slimmed down version of an `OutlivesConstraint` from `Vec<OutlivesConstraint>`.

The artificial `ObligationCause` is only needed in one place. Rather than having that exception polute the lower level logic, bubble it up as much as we can. That way, it becomes clearer what `BlameConstraint` actually is.

For reference, `ObligationCause` was introdued by #89249, but the codebase looked quite different back then.

This takes us one step closer (this is commit 1 of 4) towards the end result illustrated in #158623.

# Notes

I have removed the comment

```rs
        // We try to avoid reporting a `ConstraintCategory::Predicate` as our best constraint.
        // Instead, we use it to produce an improved `ObligationCauseCode`.
````

because I think [this](https://github.com/rust-lang/rust/blob/e2af9c03ef4cfb769e391968a5cfbb1afe6f01e2/compiler/rustc_borrowck/src/region_infer/mod.rs#L1763-L1762) existing comment is sufficient:

```rs
                // We handle predicates and opaque types specially; don't prioritize them here.
                ConstraintCategory::Predicate(_) | ConstraintCategory::OpaqueType => 4,
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-cleanup Category: PRs that clean code up or issues documenting cleanup. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants