Skip to content

-Zassumptions-on-binders#155887

Open
BoxyUwU wants to merge 9 commits intorust-lang:mainfrom
BoxyUwU:higher_ranked_assumptions_v2
Open

-Zassumptions-on-binders#155887
BoxyUwU wants to merge 9 commits intorust-lang:mainfrom
BoxyUwU:higher_ranked_assumptions_v2

Conversation

@BoxyUwU
Copy link
Copy Markdown
Member

@BoxyUwU BoxyUwU commented Apr 27, 2026

View all comments

r? lcnr

cc https://rust-lang.github.io/rust-project-goals/2026/assumptions_on_binders.html I would cc a tracking issue but the project goals haven't been finalized yet ^^'

Implements -Zassumptions-on-binders. This has a few main components:

  1. We introduce a new form of region constraints for use by the trait solver which supports ORs
  2. When entering binders universally inside of the trait solver we walk the bound thing and compute a list of region requirements. We then track in the InferCtxt all the region outlives and type outlives mentioning a placeholder from the binder for use when handling constraints involving placeholders
  3. Ideally when exiting a binder, but currently actually when computing a response inside the solver, we look through all of region constraints involving placeholders and eagerly handle these region constraints instead of returning them to the caller

This is very much a first-draft impl (though it is vaguely functional), there's a lot we need to change going forwards:

  • We should really be using this new form of region constraint everywhere/more generally we shouldnt have two kinds of region constraints
  • We shouldn't be computing implied bounds when entering binders, instead they should be explicit everywhere and actually checked when instantiating binders. As-is -Zassumptions-on-binders probably widens existing soundness holes around implied bounds due to having significantly more implied bounds
  • We should be eagerly handling placeholders everywhere not just inside of the trait solver. Right now there will still be missing assumptions for placeholders when we do higher ranked type relations during type checking outside of the trait solver.
  • I'm not normalizing our assumptions or our constraints and we should be doing both ✨
  • Handling of alias outlives' involving placeholders is incomplete in a number of ways
  • We should handle placeholders when leaving binders not when computing responses IMO
  • Actually support OR region constraints in borrow checking and region checking ✨ right now all our OR constraints are converted into normal ANDed region constraints in root contexts.
  • Right now diagnostics just point to the whole item which the unsatisfied constraints came from. this is suboptimal! fix this!
  • Move universe information into InferCtxtInner so it can be rolled back by probes
  • we should make some kind of test suite helper so we can directly write universal/existential quantifiers and assumptions rather than having to go through rust syntax :')

How do we actually eagerly handle constraints?

The general idea is that we have some function (eagerly_handle_placeholders_in_universe) which takes:

  • A set of region constraints
  • The universe which we want to eagerly handle constraints in
  • A set of outlives assumptions associated with that universe/binder

This function will rewrite all of the region constraints which involve placeholders from the passed in universe to be in terms of variables from smaller universes (or drop the constraints if we know them to be satisfied). For example:

for<'a> where('a: 'b) {
    prove ('a: 'c)
}

when exiting for<'a> we want to handle the '!a: 'c constraint somehow and we can do that by requiring that any of the lifetimes which '!a outlives, themselves outlive 'c. In this case we can require Or('b: 'c) and instead of '!a: 'c which gives us a constraint that makes sense after exiting the forall.

some more examples:

for<'a> where('a: 'b, 'a: 'c) {
    prove('a: 'd)
}
// rewritten to Or('b: 'd, 'c: 'd)

for<'a> where('b: 'a) {
    prove(T: 'a)
}
// rewritten to Or(T: 'b)

The tricky thing here is that we want/need to avoid the trait solver knowing about all type outlives/region outlives assumptions. So this algorithm is implemented with only knowing about the assumptions coming from the binder that is being exited.

We want to avoid passing all outlives assumptions through the trait solver for two main reasons. The first is just perf, augmenting the ParamEnv with significant amounts of outlives assumptions could easily mess up caching.

The second is that it's Not Possible:tm: to implement. During type checking we can be doing trait solving inside of a closure which has an uninferred signature, this means that there are some set of implied bounds that we just don't know about yet because we only know some inference variable is well formed and nothing else.


Long term we should be able to wholly rip out placeholder handling from borrow checking and we won't ever encounter placeholders outside of the binders they were produced from.

@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. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Apr 27, 2026
@rust-log-analyzer

This comment has been minimized.

@BoxyUwU BoxyUwU force-pushed the higher_ranked_assumptions_v2 branch 2 times, most recently from 8e8c7e5 to a3098f3 Compare April 28, 2026 09:12
@rust-log-analyzer

This comment has been minimized.

@BoxyUwU BoxyUwU force-pushed the higher_ranked_assumptions_v2 branch 2 times, most recently from e24ed3d to c7de204 Compare April 28, 2026 09:33
@rust-log-analyzer

This comment has been minimized.

@BoxyUwU BoxyUwU force-pushed the higher_ranked_assumptions_v2 branch from c7de204 to 642d5dc Compare April 28, 2026 09:44
@rust-log-analyzer

This comment has been minimized.

@BoxyUwU BoxyUwU force-pushed the higher_ranked_assumptions_v2 branch from 642d5dc to aba599d Compare April 28, 2026 09:56
@rust-log-analyzer

This comment has been minimized.

@BoxyUwU BoxyUwU force-pushed the higher_ranked_assumptions_v2 branch from aba599d to 19c7f1d Compare April 28, 2026 10:15
@rust-log-analyzer

This comment has been minimized.

@BoxyUwU BoxyUwU force-pushed the higher_ranked_assumptions_v2 branch from 19c7f1d to 02a859d Compare April 28, 2026 10:22
@rust-log-analyzer

This comment has been minimized.

Comment thread compiler/rustc_infer/src/infer/context.rs Outdated
Comment thread compiler/rustc_infer/src/infer/mod.rs Outdated
Comment thread compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs
Comment thread compiler/rustc_trait_selection/src/solve/delegate.rs Outdated
pub mod lang_items;
pub mod lift;
pub mod outlives;
pub mod region_constraint;
Copy link
Copy Markdown
Member Author

@BoxyUwU BoxyUwU Apr 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weird module name

View changes since the review

}

#[derive_where(Clone, Hash, PartialEq, Debug; I: Interner)]
pub enum RegionConstraint<I: Interner> {
Copy link
Copy Markdown
Member Author

@BoxyUwU BoxyUwU Apr 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread compiler/rustc_type_ir/src/region_constraint.rs Outdated
Comment thread compiler/rustc_type_ir/src/region_constraint.rs Outdated
Comment thread compiler/rustc_type_ir/src/region_constraint.rs Outdated
Comment thread compiler/rustc_type_ir/src/region_constraint.rs Outdated
Comment thread compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs Outdated
Comment thread compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs Outdated
Comment thread compiler/rustc_type_ir/src/infer_ctxt.rs Outdated
Comment thread compiler/rustc_type_ir/src/region_constraint.rs
Comment thread compiler/rustc_type_ir/src/region_constraint.rs
Comment thread compiler/rustc_type_ir/src/region_constraint.rs Outdated
Comment thread compiler/rustc_type_ir/src/region_constraint.rs Outdated
Comment thread compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs Outdated
@BoxyUwU BoxyUwU force-pushed the higher_ranked_assumptions_v2 branch from 2b2a9a2 to 23a9728 Compare May 8, 2026 14:24
@BoxyUwU BoxyUwU marked this pull request as ready for review May 8, 2026 14:53
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 8, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 8, 2026

Some changes occurred to the core trait solver

cc @rust-lang/initiative-trait-system-refactor

@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 May 8, 2026
@BoxyUwU BoxyUwU changed the title [WIP] -Zassumptions-on-binders -Zassumptions-on-binders May 8, 2026
Comment thread compiler/rustc_borrowck/src/region_infer/mod.rs Outdated
@rust-log-analyzer

This comment has been minimized.

@BoxyUwU BoxyUwU force-pushed the higher_ranked_assumptions_v2 branch from deb19c7 to 1c60d77 Compare May 8, 2026 15:51
Comment thread compiler/rustc_borrowck/src/region_infer/mod.rs Outdated
@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@BoxyUwU BoxyUwU force-pushed the higher_ranked_assumptions_v2 branch from 38786ea to 27da5f2 Compare May 8, 2026 18:45
@BoxyUwU BoxyUwU force-pushed the higher_ranked_assumptions_v2 branch from 27da5f2 to c42e7ea Compare May 8, 2026 18:53
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 8, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@BoxyUwU BoxyUwU force-pushed the higher_ranked_assumptions_v2 branch from 44bca3e to 00502c6 Compare May 8, 2026 20:45
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@BoxyUwU BoxyUwU force-pushed the higher_ranked_assumptions_v2 branch from f2524ee to 29c1906 Compare May 9, 2026 10:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants