trait_selection: Keep type-op region constraints in borrowck - #161423
trait_selection: Keep type-op region constraints in borrowck#161423Dnreikronos wants to merge 9 commits into
Conversation
Keep rust-lang#158588 focused on reporting solver region constraints. The type-op behavior and its borrowck coverage now live in rust-lang#161423.
|
r? @davidtwco rustbot has assigned @davidtwco. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
r? BoxyUwU |
|
I think this is a partial revert of #160982 which we don't want. I think that teaching @rustbot author |
|
Reminder, once the PR becomes ready for a review, use |
…tions_binders_diagnostics, r=BoxyUwU trait_selection: fix assumptions-on-binders diagnostics fixes rust-lang#157732 `-Zassumptions-on-binders` was losing the origin of solver region constraints. By the time regionck or borrowck reported them, every constraint received the containing item span, so diagnostics pointed at an entire function or const. Some paths were also still emitting placeholder text (`:3` and `meoow :c`). This keeps canonical solver responses and `ExternalConstraintsData` span-free, so source locations do not participate in candidate equality or caching. When a response is applied, `EvalCtxt::origin_span` is attached to each atomic solver `RegionConstraint` stored in `InferCtxt`. Late region conversion then uses the span attached to each constraint, including for ambiguity diagnostics. The affected paths now report `higher-ranked lifetime bound could not be satisfied` at the type use that introduced the failing constraint. UI coverage checks the rust-lang#157732 call-site diagnostic and the existing regionck alias-outlives case. Unit coverage checks that the spanned evaluator stays in semantic parity with the type-ir evaluator and preserves the first ambiguity origin span. The type-op behavior I ran into while working on this is split into rust-lang#161423. Future work: carry the failed outlives predicate or originating binder far enough through this path to name the exact bound that failed, rather than only pointing at its origin.
…tions_binders_diagnostics, r=BoxyUwU trait_selection: fix assumptions-on-binders diagnostics fixes rust-lang#157732 `-Zassumptions-on-binders` was losing the origin of solver region constraints. By the time regionck or borrowck reported them, every constraint received the containing item span, so diagnostics pointed at an entire function or const. Some paths were also still emitting placeholder text (`:3` and `meoow :c`). This keeps canonical solver responses and `ExternalConstraintsData` span-free, so source locations do not participate in candidate equality or caching. When a response is applied, `EvalCtxt::origin_span` is attached to each atomic solver `RegionConstraint` stored in `InferCtxt`. Late region conversion then uses the span attached to each constraint, including for ambiguity diagnostics. The affected paths now report `higher-ranked lifetime bound could not be satisfied` at the type use that introduced the failing constraint. UI coverage checks the rust-lang#157732 call-site diagnostic and the existing regionck alias-outlives case. Unit coverage checks that the spanned evaluator stays in semantic parity with the type-ir evaluator and preserves the first ambiguity origin span. The type-op behavior I ran into while working on this is split into rust-lang#161423. Future work: carry the failed outlives predicate or originating binder far enough through this path to name the exact bound that failed, rather than only pointing at its origin.
…tions_binders_diagnostics, r=BoxyUwU trait_selection: fix assumptions-on-binders diagnostics fixes rust-lang#157732 `-Zassumptions-on-binders` was losing the origin of solver region constraints. By the time regionck or borrowck reported them, every constraint received the containing item span, so diagnostics pointed at an entire function or const. Some paths were also still emitting placeholder text (`:3` and `meoow :c`). This keeps canonical solver responses and `ExternalConstraintsData` span-free, so source locations do not participate in candidate equality or caching. When a response is applied, `EvalCtxt::origin_span` is attached to each atomic solver `RegionConstraint` stored in `InferCtxt`. Late region conversion then uses the span attached to each constraint, including for ambiguity diagnostics. The affected paths now report `higher-ranked lifetime bound could not be satisfied` at the type use that introduced the failing constraint. UI coverage checks the rust-lang#157732 call-site diagnostic and the existing regionck alias-outlives case. Unit coverage checks that the spanned evaluator stays in semantic parity with the type-ir evaluator and preserves the first ambiguity origin span. The type-op behavior I ran into while working on this is split into rust-lang#161423. Future work: carry the failed outlives predicate or originating binder far enough through this path to name the exact bound that failed, rather than only pointing at its origin.
…tions_binders_diagnostics, r=BoxyUwU trait_selection: fix assumptions-on-binders diagnostics fixes rust-lang#157732 `-Zassumptions-on-binders` was losing the origin of solver region constraints. By the time regionck or borrowck reported them, every constraint received the containing item span, so diagnostics pointed at an entire function or const. Some paths were also still emitting placeholder text (`:3` and `meoow :c`). This keeps canonical solver responses and `ExternalConstraintsData` span-free, so source locations do not participate in candidate equality or caching. When a response is applied, `EvalCtxt::origin_span` is attached to each atomic solver `RegionConstraint` stored in `InferCtxt`. Late region conversion then uses the span attached to each constraint, including for ambiguity diagnostics. The affected paths now report `higher-ranked lifetime bound could not be satisfied` at the type use that introduced the failing constraint. UI coverage checks the rust-lang#157732 call-site diagnostic and the existing regionck alias-outlives case. Unit coverage checks that the spanned evaluator stays in semantic parity with the type-ir evaluator and preserves the first ambiguity origin span. The type-op behavior I ran into while working on this is split into rust-lang#161423. Future work: carry the failed outlives predicate or originating binder far enough through this path to name the exact bound that failed, rather than only pointing at its origin.
…tions_binders_diagnostics, r=BoxyUwU trait_selection: fix assumptions-on-binders diagnostics fixes rust-lang#157732 `-Zassumptions-on-binders` was losing the origin of solver region constraints. By the time regionck or borrowck reported them, every constraint received the containing item span, so diagnostics pointed at an entire function or const. Some paths were also still emitting placeholder text (`:3` and `meoow :c`). This keeps canonical solver responses and `ExternalConstraintsData` span-free, so source locations do not participate in candidate equality or caching. When a response is applied, `EvalCtxt::origin_span` is attached to each atomic solver `RegionConstraint` stored in `InferCtxt`. Late region conversion then uses the span attached to each constraint, including for ambiguity diagnostics. The affected paths now report `higher-ranked lifetime bound could not be satisfied` at the type use that introduced the failing constraint. UI coverage checks the rust-lang#157732 call-site diagnostic and the existing regionck alias-outlives case. Unit coverage checks that the spanned evaluator stays in semantic parity with the type-ir evaluator and preserves the first ambiguity origin span. The type-op behavior I ran into while working on this is split into rust-lang#161423. Future work: carry the failed outlives predicate or originating binder far enough through this path to name the exact bound that failed, rather than only pointing at its origin.
…tions_binders_diagnostics, r=BoxyUwU trait_selection: fix assumptions-on-binders diagnostics fixes rust-lang#157732 `-Zassumptions-on-binders` was losing the origin of solver region constraints. By the time regionck or borrowck reported them, every constraint received the containing item span, so diagnostics pointed at an entire function or const. Some paths were also still emitting placeholder text (`:3` and `meoow :c`). This keeps canonical solver responses and `ExternalConstraintsData` span-free, so source locations do not participate in candidate equality or caching. When a response is applied, `EvalCtxt::origin_span` is attached to each atomic solver `RegionConstraint` stored in `InferCtxt`. Late region conversion then uses the span attached to each constraint, including for ambiguity diagnostics. The affected paths now report `higher-ranked lifetime bound could not be satisfied` at the type use that introduced the failing constraint. UI coverage checks the rust-lang#157732 call-site diagnostic and the existing regionck alias-outlives case. Unit coverage checks that the spanned evaluator stays in semantic parity with the type-ir evaluator and preserves the first ambiguity origin span. The type-op behavior I ran into while working on this is split into rust-lang#161423. Future work: carry the failed outlives predicate or originating binder far enough through this path to name the exact bound that failed, rather than only pointing at its origin.
…tions_binders_diagnostics, r=BoxyUwU trait_selection: fix assumptions-on-binders diagnostics fixes rust-lang#157732 `-Zassumptions-on-binders` was losing the origin of solver region constraints. By the time regionck or borrowck reported them, every constraint received the containing item span, so diagnostics pointed at an entire function or const. Some paths were also still emitting placeholder text (`:3` and `meoow :c`). This keeps canonical solver responses and `ExternalConstraintsData` span-free, so source locations do not participate in candidate equality or caching. When a response is applied, `EvalCtxt::origin_span` is attached to each atomic solver `RegionConstraint` stored in `InferCtxt`. Late region conversion then uses the span attached to each constraint, including for ambiguity diagnostics. The affected paths now report `higher-ranked lifetime bound could not be satisfied` at the type use that introduced the failing constraint. UI coverage checks the rust-lang#157732 call-site diagnostic and the existing regionck alias-outlives case. Unit coverage checks that the spanned evaluator stays in semantic parity with the type-ir evaluator and preserves the first ambiguity origin span. The type-op behavior I ran into while working on this is split into rust-lang#161423. Future work: carry the failed outlives predicate or originating binder far enough through this path to name the exact bound that failed, rather than only pointing at its origin.
…tions_binders_diagnostics, r=BoxyUwU trait_selection: fix assumptions-on-binders diagnostics fixes rust-lang#157732 `-Zassumptions-on-binders` was losing the origin of solver region constraints. By the time regionck or borrowck reported them, every constraint received the containing item span, so diagnostics pointed at an entire function or const. Some paths were also still emitting placeholder text (`:3` and `meoow :c`). This keeps canonical solver responses and `ExternalConstraintsData` span-free, so source locations do not participate in candidate equality or caching. When a response is applied, `EvalCtxt::origin_span` is attached to each atomic solver `RegionConstraint` stored in `InferCtxt`. Late region conversion then uses the span attached to each constraint, including for ambiguity diagnostics. The affected paths now report `higher-ranked lifetime bound could not be satisfied` at the type use that introduced the failing constraint. UI coverage checks the rust-lang#157732 call-site diagnostic and the existing regionck alias-outlives case. Unit coverage checks that the spanned evaluator stays in semantic parity with the type-ir evaluator and preserves the first ambiguity origin span. The type-op behavior I ran into while working on this is split into rust-lang#161423. Future work: carry the failed outlives predicate or originating binder far enough through this path to name the exact bound that failed, rather than only pointing at its origin.
Rollup merge of #158588 - Dnreikronos:trait_selection/assumptions_binders_diagnostics, r=BoxyUwU trait_selection: fix assumptions-on-binders diagnostics fixes #157732 `-Zassumptions-on-binders` was losing the origin of solver region constraints. By the time regionck or borrowck reported them, every constraint received the containing item span, so diagnostics pointed at an entire function or const. Some paths were also still emitting placeholder text (`:3` and `meoow :c`). This keeps canonical solver responses and `ExternalConstraintsData` span-free, so source locations do not participate in candidate equality or caching. When a response is applied, `EvalCtxt::origin_span` is attached to each atomic solver `RegionConstraint` stored in `InferCtxt`. Late region conversion then uses the span attached to each constraint, including for ambiguity diagnostics. The affected paths now report `higher-ranked lifetime bound could not be satisfied` at the type use that introduced the failing constraint. UI coverage checks the #157732 call-site diagnostic and the existing regionck alias-outlives case. Unit coverage checks that the spanned evaluator stays in semantic parity with the type-ir evaluator and preserves the first ambiguity origin span. The type-op behavior I ran into while working on this is split into #161423. Future work: carry the failed outlives predicate or originating binder far enough through this path to name the exact bound that failed, rather than only pointing at its origin.
65f9dd2 to
2959d13
Compare
This comment has been minimized.
This comment has been minimized.
|
Sup,@BoxyUwU :) Yeah, The next solver was storing the tree in the temporary query I also found another place dropping the same tree during implied-bound normalization, before lexical regionck. I fixed that too and added regressions for that path and the borrowck case. I like this version a lot more. It fixes the point where the data was lost and keeps the cache behavior from #160982. The local version worked, but mostly by going around the problem, and that felt wrong once I understood the full path. @rustbot ready |
This comment has been minimized.
This comment has been minimized.
2959d13 to
dc2e6c6
Compare
This comment has been minimized.
This comment has been minimized.
|
|
||
| /// Runs `op` with an empty solver-region-constraint store, restores the | ||
| /// caller's constraints, and returns the constraints produced by `op`. | ||
| pub fn with_fresh_solver_region_constraints<R>( |
There was a problem hiding this comment.
why is this necessary for new style constraints but not old style?
There was a problem hiding this comment.
The difference is that the old obligations and assumptions are taken before the op and checked to be empty, so everything collected afterward belongs to that op. We cannot make that same check for the solver tree. Borrowck may already have constraints from an earlier type op in the same InferCtxt, and those stay there until the end of typeck. If we took the full tree afterward, this response would also contain the caller's older constraints. Clearing it first would lose them. The helper parks the old tree, captures what this op made, then restores the old one.
I do not love the extra swap, but I think this part is a real difference in how the constraints are stored and when they are consumed.
There was a problem hiding this comment.
hmm I wouldn't expect there to be any kind of difference between how we move region constraints around. Do you know where that difference is originating from?
I noticed here in one of the functions you register the constraint to the Infcx instead of to the MirTypeckRegionConstraints: https://github.com/rust-lang/rust/pull/161423/changes#diff-22902f0e92a4c4111643ef62f169a481590d128657daab37c126d41c0b44985cR83.
Is that the underlying cause of this or is that downstream of whatever is causing the difference here? I would expect that we should extend MirTypeckRegionConstraints to also store new-style constraints and then we can push new constraints to that instead of into the InferCtxt
There was a problem hiding this comment.
Yeah, I think you're right, Boxy
The old style constraints get converted into MirTypeckRegionConstraints inside convert_all, so the InferCtxt only ever holds what the current op just made and a take is enough. I was putting the new style tree back into the InferCtxt instead, and nothing drains it there, borrowck reads it once at the end of typeck. So by the second type op the first op's constraints are still sitting in the store. Taking would have put them in the wrong response and clearing would have dropped them, so I parked them and put them back. That's me working around a hole I made myself one file over, which is why it looked weird, because it was weird.
Did it your way. MirTypeckRegionConstraints has a solver_constraints field now, convert_all ands into that like it does with everything else, and scrape_region_constraints just takes from the InferCtxt next to the other two takes. with_fresh_solver_region_constraints is gone.
Two things fell out of it. The destructuring at the end of typeck pulls the tree out of MirTypeckRegionConstraints and passes it in, since borrowck doesn't keep it in the InferCtxt anymore, so destructure_solver_region_constraints takes the constraint as an argument. Regionck still reads it off the InferCtxt like before. And now the store really is empty in both spots where we already assert there are no leftover region obligations, so I added the same assert there instead of leaving it as folklore.
I like this a lot more. The swap worked, but only because I knew exactly who was writing to that store and when, and that's not an invariant anyone should have to carry around in their head.
Ran the ui tests for borrowck, nll, impl-trait, higher-ranked and coroutines on top of the assumptions-on-binders ones, the new asserts hold.
One thing I didn't touch is constraints registered after typeck is done, from the opaque type handling in region_infer, still go nowhere. That was already true before this, they were being registered into the InferCtxt after we had already read it. Now they just sit in the field instead. Happy to look at it, but it felt like a different PR.
| ®ion_constraint_data, | ||
| region_assumptions, | ||
| ); | ||
| region_constraints.solver_constraints = solver_constraints; |
There was a problem hiding this comment.
this generally feels kinda off, why doesn't this just work the same way as or region_obligations/region_assumptions?
There was a problem hiding this comment.
Yeah, agreed. I changed make_query_region_constraints to take the solver tree together with region_obligations and region_assumptions. The normal query path and scrape_region_constraints now build QueryRegionConstraints through the same function, and callers that cannot produce a solver tree pass the empty one.
The temporary swap is still needed so we do not mix a solver tree already registered by the caller into this op's response, but the query response side no longer bolts the field on afterward. I think this reads much better.
| // outlives bound required proving some higher-ranked coroutine obl. | ||
| let QueryRegionConstraints { constraints, assumptions: _ } = constraints; | ||
| let QueryRegionConstraints { constraints, solver_constraints, .. } = constraints; | ||
| if !solver_constraints.is_true() { |
There was a problem hiding this comment.
what's with all of these checks for !constraint.is_true() is this necessary or just a perf opt? if it's a perf opt I'd prefer we drop it.
There was a problem hiding this comment.
The checks do matter with the current representation, but putting them at every caller was the wrong place. true is And([]). Pushing that into the store turns it into And([And([])]); it still means true, but is_true() only recognizes the empty outer And, so an empty query starts looking nonempty.
I moved that check into the store instead. Registering a trivial tree now does nothing and does not add an undo-log entry, so the callers can register unconditionally. There is a unit test for the empty-query case too.
There was a problem hiding this comment.
Is this still the case after rebasing on my PR to always store stuff in canonical form?
There was a problem hiding this comment.
Nope, not anymore. With your PR in, And::new/Or::new keep things canonical, so build_and(true, x) really is x and is_true() still holds after it. The And([And([])]) I was dodging can't happen now.
So it's gone. The callers stopped checking last round and the early return in register_type_outlives_constraint_inner was the last piece of it, all it saves at this point is a realloc and an undo entry.
I did keep the small unit test, but it's pinning a different thing now: registering a trivially true constraint has to leave the query response empty, which is the canonical form guarantee doing the work. Reworded the comment to say that instead.
|
@rustbot author |
|
Some changes occurred to the core trait solver cc @rust-lang/initiative-trait-system-refactor |
|
@rustbot ready |
This comment has been minimized.
This comment has been minimized.
96e94dd to
0f14fc7
Compare
|
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. |
Registering them into the `InferCtxt` instead meant nothing drained them between type ops, as borrowck only reads that store once at the end of MIR type checking. Keep them next to the other borrowck region constraints and hand them to the destructuring step directly.
With borrowck storing its own constraints, the store only holds what the current op produced, so it can be drained like the region obligations next to it instead of being saved and restored around the op. Assert it is empty on entry where we already do so for those.
Anding one into the store is a no-op now that the constraints are always kept in canonical form, so skipping it only saves a realloc and an undo entry.
View all comments
I ran into this while working on #158588.
borrowck_env_failstill had a FIXME because the function body wasn't reporting the outlives error. The next solver creates the region constraint, but the canonical type-op path doesn't put it inQueryResponse. Borrowck never sees it, so the type op looks fine and the error is lost.With
-Zassumptions-on-binders, these type ops now run locally on borrowck'sInferCtxt. The fast path still runs first. That leaves the constraint in the same inference context borrowck reads later.I prefer this over adding more data to the old canonical response. The next solver already caches its work, and teaching the old query path about these constraints felt like extra machinery for something we can avoid. Running locally is pretty boring, but I think that's a good thing here. The old FIXME is now the regression test.