Skip to content

Region inference: Use outlives-static constraints in constraint search #140737

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
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

amandasystems
Copy link
Contributor

@amandasystems amandasystems commented May 7, 2025

Revise the extra r: 'static constraints added upon universe issues to add an explanation, and use that explanation during constraint blame search. This greatly simplifies the region inference logic, which now does not need to reverse-engineer the event that caused a region to outlive 'static.

This cosmetically changes the output of two UI tests. I blessed them i separate commits with separate motivations, but that can of course be squashed as desired. We probably want that.

The PR was extracted out of #130227 and consists of one-third of its functional payload.

r? lcnr

@rustbot rustbot added 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. labels May 7, 2025
@amandasystems amandasystems force-pushed the revised-constraint-search branch from f4af776 to 72e81ea Compare May 16, 2025 10:14
@amandasystems amandasystems force-pushed the revised-constraint-search branch from 72e81ea to 9a1face Compare May 27, 2025 10:09
@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rustbot

This comment has been minimized.

@amandasystems amandasystems force-pushed the revised-constraint-search branch from 7902ae9 to 6539053 Compare May 28, 2025 17:05
@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Jun 5, 2025

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

@amandasystems amandasystems force-pushed the revised-constraint-search branch from 6539053 to a209255 Compare June 5, 2025 11:13
@amandasystems amandasystems changed the title [WIP] Region inference: Use outlives-static constraints in constraint search Region inference: Use outlives-static constraints in constraint search Jun 9, 2025
@amandasystems
Copy link
Contributor Author

(We may also want a perf run to see if I messed something up badly)

@lcnr
Copy link
Contributor

lcnr commented Jun 9, 2025

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jun 9, 2025
bors added a commit that referenced this pull request Jun 9, 2025
Region inference: Use outlives-static constraints in constraint search

Revise the extra `r: 'static` constraints added upon universe issues to add an explanation, and use that explanation during constraint blame search. This greatly simplifies the region inference logic, which now does not need to reverse-engineer the event that caused a region to outlive `'static`.

This cosmetically changes the output of two UI tests. I blessed them i separate commits with separate motivations, but that can of course be squashed as desired. We probably want that.

The PR was extracted out of #130227 and consists of one-third of its functional payload. It is based on #140466, so that has to land first.

We probably want a perf run of this. It shouldn't have much of an impact and a positive one if any, but I have been wrong before. In particular, SCC annotations are heavier now.

r? lcnr
@bors
Copy link
Collaborator

bors commented Jun 9, 2025

⌛ Trying commit a209255 with merge 0d3d480...

@bors
Copy link
Collaborator

bors commented Jun 9, 2025

☀️ Try build successful - checks-actions
Build commit: 0d3d480 (0d3d48082efe47a20c953415945cc721105e5f85)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (0d3d480): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.2% [0.1%, 0.3%] 12
Regressions ❌
(secondary)
0.2% [0.1%, 0.3%] 6
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.5% [-0.6%, -0.5%] 6
All ❌✅ (primary) 0.2% [0.1%, 0.3%] 12

Max RSS (memory usage)

Results (primary 0.4%, secondary -1.0%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
1.4% [1.3%, 1.5%] 2
Regressions ❌
(secondary)
3.0% [3.0%, 3.0%] 1
Improvements ✅
(primary)
-1.7% [-1.7%, -1.7%] 1
Improvements ✅
(secondary)
-1.9% [-2.4%, -0.6%] 4
All ❌✅ (primary) 0.4% [-1.7%, 1.5%] 3

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 753.257s -> 756.017s (0.37%)
Artifact size: 372.34 MiB -> 372.33 MiB (-0.00%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Jun 9, 2025
@amandasystems amandasystems force-pushed the revised-constraint-search branch from a209255 to 335fa61 Compare June 18, 2025 10:21
Revise the extra `r: 'static` constraints added upon universe issues
to add an explanation, and use that explanation during constraint blame
search. This greatly simplifies the region inference logic, which
now does not need to reverse-engineer the event that caused a region
to outlive 'static.
@amandasystems amandasystems force-pushed the revised-constraint-search branch from 335fa61 to 6a325fd Compare June 27, 2025 15:44
let blame_to = if annotation.representative.rvid() == max_u_rvid {
// The SCC's representative is not nameable from some region
// that ends up in the SCC.
let small_universed_rvid = find_region(
Copy link
Contributor

Choose a reason for hiding this comment

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

why can we not use the region from max_nameable_universe here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That sometimes slightly worsens the error messages (or at least causes diagnostics regressions) because previous logic picked the first smallest region. It's very annoying.

Copy link
Contributor

Choose a reason for hiding this comment

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

can you dump the changes caused by this somewhere? I personally believe that some amount of diagnostics regression is acceptable if it cleans up the code/removes subtly different variations of the same concept.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here's the an output from the UI tests attached when doing that!

$ ./x test ui --keep-stage 1 > simpler-choice-blame-ui-tests.log

simpler-choice-blame-ui-tests.log

There's a bunch of squiggly lines getting longer, which I think is mostly harmless:

  • tests/ui/higher-ranked/higher-ranked-lifetime-error.rs: I'd say fully harmless, a slightly longer squiggly line
  • tests/ui/higher-ranked/trait-bounds/due-to-where-clause.rs same as above
  • tests/ui/mismatched_types/hr-projection-mismatch.rs#current same as above

Then there is a duplicate of a diagnostic due to a longer span which we may or may not care about, in tests/ui/associated-inherent-types/issue-111404-1.rs. This should be very fixable in the error reporting phase, but that part of the code is absolutely incomprehensible (though I've tried), and I've simply given up on it.

I remember this being much, much worse! Maybe this is acceptable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you think this is ok, say the word and I'll dump like half the file to get rid of this extra logic!

Copy link
Contributor

@lcnr lcnr Jul 30, 2025

Choose a reason for hiding this comment

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

heavy drag on cigarette I don’t know man. I don’t make the constraint graph, I just walk it.

would be good to know 😁 it implies we somehow end up relating something to a placeholder from a higher universe for a boring reason? As in, even without the hr region error due to the predicate we'd have failed to compile this snippet


I feel like mir borrowck generally has to handle the following for when encountering an error:

  • there may be multiple paths causing the bad outlives (idk about terminology)
  • each path has multiple steps

I generally feel like we want to report the most boring and shortest path and of that path, the most exciting step. Or well, ideally we'd be able to point to segments shared by all paths :3

A step is boring if it exists due to reasons the user can't do much about, e.g. for types to be wf

A step is exciting™️ if it corresponds to something which is actively happening in the source, some where-bound, some assignment. The user is likely to be change a specific part of their code to avoid emitting this constraint.

So in this example, if we get an outlives 'static constraint both because its related to a placeholder for "boring" reasons, and also due to a predicate, we want to treat it as if its due to the boring reason, as fixing the predicate issue likely won't handle the boring constraint.

So yeah, I think it's actually good that we use this boring constraint instead of the predicate constraint here :>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is some edge case though, since it only differs in the final step.

But sure, in general constraint blame search needs a lot of overhaul. It has terrible tech debt (check out the huge closure that arbitrarily tweaks priorities to work around some edge case), and I feel like it would be a lot simpler to attach more complete motivations and catch them later during error reporting than the current approach, which ad hoc recreates the event.

I don't know what to do about this note though. I think what I'd want to do in the best of all worlds™ is:

  • Constraint blame search also optionally tells in its result if search was redirected, and if so why. For this solution, we just say "because placeholder outlives unnameable blah blah". That's presumably one enum variant, or a boolean flag, or maybe an Option.
  • We somehow also need to find the span that the note about outliving static due to limitations needs. I don't know how to find that. Or do we need to find that?

Or do you mean the note shouldn't be there at all and this is good actually™?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think given that the outlives 19 exist with a BoringNoLocation, the predicate is not to blame here. Now, I don't see why that boring region outlives placeholder exists, but imo as long as that outlives exists, we shouldn't add a note stating that it's due to a predicate, because there's a separate "non predicate" reason

I would love for u to look into why that other constraint exists.

Constraint blame search also optionally tells in its result if search was redirected

hmm, that is another question 🤔 if we have 'n: 'placeholder as a cause of a 'm: 'static path, why do we compute the 'n: 'placeholder path instead of the full 'm: 'placeholder path (going through n).

Sorry for giving u a bunch of vague "pls look at X" reviews here btw. I think it's really good that we/you are cleaning this up but I want to make sure we're slowly moving towards something desirable while doing so

Copy link
Contributor Author

Choose a reason for hiding this comment

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

why do we compute the 'n: 'placeholder path instead of the full 'm: 'placeholder path (going through n).

Technically, the path could be from a placeholder, to an existential I think? In that case 'n would presumably be a placeholder (always the representative of an SCC), and the to-part of the redirect constraint be whatever region lowered the SCC's universe. In either case, 'm would reach that SCC so restarting the search is probably not doing much, except if there is an even shorter path to the target of the redirect constraint.

The meaning of the added annotation to the outlives-static constraint is meant to be "this edge to static was added because 'a: 'b is a path in the constraint graph".

Copy link
Contributor

Choose a reason for hiding this comment

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

from placeholder to an existential should error, shouldn't it?

we only add outlives-static whenever we've got "scc_which_cannot_name reaches(transitively outlives) placeholder" constraints 🤔

My question above was, why does

// If we are passing through a constraint added because `'lt: 'unnameable`,
// where cannot name `'unnameable`, redirect search towards `'unnameable`.
let due_to_placeholder_outlives = path.iter().find_map(|c| {
if let ConstraintCategory::OutlivesUnnameablePlaceholder(lt, unnameable) = c.category {
Some((lt, unnameable))
} else {
None
}
});
let path = if let Some((lt, unnameable)) = due_to_placeholder_outlives {
// This the `false` argument is what prevents circular reasoning here!
self.constraint_path_to(lt, |r| r == unnameable, false).unwrap().0
} else {
path
};
check constraint_path_to(lt, ..) instead of constraint_path_to(from_region, ..)

@amandasystems amandasystems force-pushed the revised-constraint-search branch 3 times, most recently from ecc1d79 to a34f352 Compare July 6, 2025 08:34
@lcnr lcnr added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 25, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 25, 2025

⚠️ Warning ⚠️

Co-authored-by: lcnr <rust@lcnr.de>
@amandasystems amandasystems force-pushed the revised-constraint-search branch from 8695bb5 to b03635d Compare July 25, 2025 14:42
@bors
Copy link
Collaborator

bors commented Jul 28, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
perf-regression Performance regression. 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.

6 participants