about summary refs log tree commit diff
path: root/compiler/rustc_borrowck/src/region_infer/mod.rs
AgeCommit message (Collapse)AuthorLines
2025-09-27Rename various "concrete opaque type" terminology to say "hidden type"Boxy Uwu-2/+2
2025-09-24Rollup merge of #146711 - lcnr:fix-placeholder-ice, r=lqdMatthias Krüger-3/+4
fix 2 borrowck issues fixes https://github.com/rust-lang/rust/issues/146467 cc ``@amandasystems`` our understanding here is as follows: region constraints from computing implied bounds gets `ConstraintCategory::Internal`. If there's a higher-ranked subtyping errors while computing implied bounds we then ended up with only `ConstraintCategory::Internal` and `ConstraintCategory::OutlivesUnnameablePlaceholder(_)` constraints. The path was something like - `'placeholderU2: 'placeholderU1` (`Internal`) - `'placeholderU1: 'static` (`OutlivesUnnameablePlaceholder('placeholderU2)`) It's generally somewhat subtle here as ideally relating placeholders doesn't introduce `'static` constraints. Relating the placeholders themselves will always error regardless, cc https://github.com/rust-lang/rust/pull/142623. --- separately fixes https://github.com/rust-lang/rust/pull/145925#issuecomment-3303733357 by updating the location for deferred closure requirements inside of promoteds. I am not updating their category as doing so is 1) effort and 2) imo actually undesirable :thinking: see the comments in `TypeChecker::check_promoted` cc ``@lqd`` r? lqd
2025-09-18Clean up universe evaluation during type test evaluationAmanda Stjerna-5/+15
The logic was, as the removed comments suggest, hackish and meant to implement previous logic that was factored out. The new logic does exactly what the comments say, and is much less surprising.
2025-09-18internal constraints are better than placeholder outliveslcnr-3/+4
2025-09-09erase_regions to erase_and_anonymize_regionsBoxy-1/+1
2025-08-23Simplify SCC annotations somewhatAmanda Stjerna-13/+1
2025-08-22Region inference: Use outlives-static constraints in constraint searchAmanda Stjerna-109/+76
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.
2025-08-20diagnostics :3lcnr-18/+42
2025-08-20handle opaque types before region inferencelcnr-281/+7
2025-08-15Auto merge of #142071 - lcnr:revealing-use, r=compiler-errorsbors-1/+8
`apply_member_constraints`: fix placeholder check Checking whether the member region is *an existential region from a higher universe* is just wrong and I am pretty sure we've added that check by accident as the naming was just horribly confusing before rust-lang/rust#140466. I've encountered this issue separately while working on rust-lang/rust#139587, but feel like it's probably easier to separately FCP this change. This allows the following code to compile ```rust trait Proj<'a> { type Assoc; } impl<'a, 'b, F: FnOnce() -> &'b ()> Proj<'a> for F { type Assoc = (); } fn is_proj<F: for<'a> Proj<'a>>(f: F) {} fn define<'a>() -> impl Sized + use<'a> { // This adds a use of `opaque::<'a>` with hidden type `&'unconstrained_b ()`. // 'unconstrained_b is an inference variable from a higher universe as it gets // created inside of the binder of `F: for<'a> Proj<'a>`. This previously // caused us to not apply member constraints. We now do, constraining // it to `'a`. is_proj(define::<'a>); &() } fn main() {} ``` This should not be breaking change, even in theory. Applying member constraints is incomplete in rare circumstances which means that applying them in more cases can cause spurious errors, cc rust-lang/rust#140569/rust-lang/rust#142073. However, as we always skipped these member regions in `apply_member_constraints` the skipped region is guaranteed to cause an error in `check_member_constraints` later on.
2025-08-11remove `from_forall`lcnr-4/+9
2025-08-09Rollup merge of #145115 - lcnr:less-borrowck-tainting, r=compiler-errorsStuart Cook-1/+1
defer opaque type errors, generally greatly reduce tainting fixes the test for rust-lang/rust#135528, does not actually fix that issue properly. This is useful as it causes the migration to rust-lang/rust#139587 to be a lot easier.
2025-08-08borrowck: defer opaque type errorslcnr-1/+1
2025-08-08apply_member_constraints: fix placeholder checklcnr-1/+8
2025-08-06Track names of existentialsAmanda Stjerna-2/+2
2025-08-02Rollup merge of #144478 - joshtriplett:doc-code-formatting-prep, r=AmanieuSamuel Tardieu-1/+1
Improve formatting of doc code blocks We don't currently apply automatic formatting to doc comment code blocks. As a result, it has built up various idiosyncracies, which make such automatic formatting difficult. Some of those idiosyncracies also make things harder for human readers or other tools. This PR makes a few improvements to doc code formatting, in the hopes of making future automatic formatting easier, as well as in many cases providing net readability improvements. I would suggest reading each commit separately, as each commit contains one class of changes.
2025-07-28use let chains in ast, borrowck, codegen, const_evalKivooeo-36/+32
2025-07-25Skip formatting for some compiler documentation codeJosh Triplett-1/+1
These examples feature Rust code that's presented primarily to illustrate how its compilation would be handled, and these examples are formatted to highlight those aspects in ways that rustfmt wouldn't preserve. Turn formatting off in those examples. (Doc code isn't formatted yet, but this will make it easier to enable doc code formatting in the future.)
2025-06-05Auto merge of #140466 - amandasystems:move-to-preprocessing-step, r=lcnrbors-196/+54
Move placeholder handling to a proper preprocessing step This commit breaks out the logic of placheolder rewriting into its own preprocessing step. It's one of the more boring parts of #130227. The only functional change from this is that the preprocessing step (where extra `r: 'static` constraints are added) is performed upstream of Polonius legacy, finally affecting Polonius. That is mostly a by-product, though. This should be reviewable by anyone in the compiler team, so r? rust-lang/compiler
2025-06-04Use an enum for SCC representatives, plus other code reviewAmanda Stjerna-36/+54
Co-authored-by: lcnr <rust@lcnr.de>
2025-06-03Move placeholder handling to a proper preprocessing stepAmanda Stjerna-184/+24
This commit breaks out the logic of placheolder rewriting into its own preprocessing step. The only functional change from this is that the preprocessing step (where extra `r: 'static` constraints are added) is performed upstream of Polonius legacy, finally affecting Polonius. That is mostly a by-product, though.
2025-05-31Drive-by refactor: use `OnceCell` for the reverse region SCC graphAmanda Stjerna-7/+5
2025-04-28Use associated types for SCC annotations, per code review suggestionAmanda Stjerna-13/+10
2025-04-28Decouple SCC annotations from SCCsAmanda Stjerna-10/+43
This rewires SCC annotations to have them be a separate, visitor-type data structure. It was broken out of #130227, which needed them to be able to remove unused annotations after computation without recomputing the SCCs themselves. As a drive-by it also removes some redundant code from the hot loop in SCC construction for a performance improvement.
2025-04-25Rollup merge of #140257 - amandasystems:housecleaning, r=wesleywiserMatthias Krüger-22/+19
Some drive-by housecleaning in `rustc_borrowck` This commit picks up a few odd ends discovered during the work on #130227. It adds some documentation and renames a few methods with too generic names to describe what they actually do. It also adds some debug output that was helpful during bug hunting and generally cleans up a few things (for my values of "clean"). r? lcnr
2025-04-24Some drive-by housecleaning in `rustc_borrowck`Amanda Stjerna-22/+19
This commit picks up a few odd ends discovered during the work on #130227. It adds some documentation and renames a few methods with too generic names to describe what they actually do. It also adds some debug output that was helpful during bug hunting.
2025-04-14Use `newtype_index!`-generated types more idiomaticallyYotam Ofek-2/+1
2025-04-11eagerly initialize `definitions` in sub-fnlcnr-20/+23
2025-04-08Auto merge of #139536 - matthiaskrgr:rollup-j6goald, r=matthiaskrgrbors-4/+1
Rollup of 7 pull requests Successful merges: - #139476 (rm `RegionInferenceContext::var_infos`) - #139485 (compiletest: Stricter parsing for diagnostic kinds) - #139491 (Update books) - #139500 (document panic behavior of Vec::resize and Vec::resize_with) - #139501 (Fix stack overflow in exhaustiveness due to recursive HIR opaque hidden types) - #139504 (add missing word in doc comment) - #139509 (clean: remove Deref<Target=RegionKind> impl for Region and use `.kind()`) r? `@ghost` `@rustbot` modify labels: rollup
2025-04-08Rollup merge of #139476 - lcnr:rm-var_infos, r=compiler-errorsMatthias Krüger-4/+1
rm `RegionInferenceContext::var_infos` we already track this info in the `definitions` field r? types
2025-04-08move `ClosureRegionRequirements` to `rustc_borrowck`lcnr-4/+6
2025-04-07rm `RegionInferenceContext::var_infos`lcnr-4/+1
we already track this info in the `definitions` field
2025-04-07Trivial tweaks to stop tracking source span directlyMichael Goulet-14/+18
2025-03-15Squash fold into tyMichael Goulet-2/+1
2025-02-28Optimize `Trace`.Nicholas Nethercote-36/+51
`Trace::FromOutlivesConstraint` contains an `OutlivesConstraint`, which is 72 bytes. Lots of these are created but never used. This commit splits `Trace::FromOutlivesConstraint` into three new variants: `FromVanilla`, `FromStatic`, `FromMember`. Each of these contains just enough data to construct an `OutlivesConstraint`, if necessary. This reduces the size of `Trace` from 72 bytes to 16 bytes. All this avoids some memory traffic.
2025-02-28Split the `Edges` iterator.Nicholas Nethercote-12/+28
The `Edges` iterator returns `OutlivesConstraint` elements, which are 72 bytes. This is big enough to affect performance. Return `&OutlivesConstraint` would be better. However, each `Edges` iterator is really one of two different iterators. The "from graph" case does a graph traversal and could return `&OutlivesConstraint`. But the "from static" case just does a `0..n` iteration and constructs a new `OutlivesConstraint` from that, so it can't return a reference. This commit splits `Edges into `EdgesFromGraph` and `EdgesFromStatic`, which allows them to have different return types. This is a perf win for the `wg-grammar` benchmark.
2025-02-22Greatly simplify lifetime captures in edition 2024Michael Goulet-6/+4
2025-02-18eval_outlives: bail out early if both regions are in the same SCCAmanda Stjerna-0/+5
2025-01-12replace location-insensitive analysis with location-sensitive analysisRémy Rakic-22/+15
we're in in the endgame now set up the location-sensitive analysis end to end: - stop recording inflowing loans and loan liveness in liveness - replace location-insensitive liveness data with live loans computed by reachability - remove equivalence between polonius scopes and NLL scopes, and only run one scope computation
2025-01-08Rollup merge of #135250 - lqd:simple-cleanups, r=matthewjasperMatthias Krüger-2/+2
A couple simple borrowck cleanups This PR has a couple simple renamings: - it's been a long time since the mapping from `Location`s to `PointIndex`es was extracted from `RegionElements` into the `DenseLocationMap`, but only the types were renamed at the time. borrowck still refers to this map as `elements`. That's confusing, especially since sometimes we also use the mapping via `LivenessValues`, and makes more sense as `location_map` instead. - to clarify `LocationTable` is not as general as it sounds, and is only for datalog polonius. In this branch I didn't rename the handful of `location_table` fields and params to `polonius_table`, but can do that to differentiate it even more from `location_map`. I did try it locally and it looks worthwhile, so if you'd prefer I can also push it here. (Or we could even switch these datalog types and fields to even more explicit names) - to clarify the incomprehensible `AllFacts`, it is renamed to `PoloniusFacts`. These can be referred to as `facts` within the legacy polonius module, but as `polonius_facts` outside of it to make it clear that they're not about NLLs (nor are they about in-tree polonius but that'll be magically fixed when they're removed in the future) r? `@matthewjasper`
2025-01-08Auto merge of #133858 - dianne:better-blame-constraints-for-static, r=lcnrbors-127/+106
`best_blame_constraint`: Blame better constraints when the region graph has cycles from invariance or `'static` This fixes #132749 by changing which constraint is blamed for region errors in several cases. `best_blame_constraint` had a heuristic that tried to pinpoint the constraint causing an error by filtering out any constraints where the outliving region is unified with the ultimate target region being outlived. However, it used the SCCs of the region graph to do this, which is unreliable; in particular, if the target region is `'static`, or if there are cycles from the presence of invariant types, it was skipping over the constraints it should be blaming. As is the case in that issue, this could lead to confusing diagnostics. The simplest fix seems to work decently, judging by test stderr: this makes `best_blame_constraint` no longer filter constraints by their outliving region's SCC. There are admittedly some quirks in the test output. In many cases, subdiagnostics that depend on the particular constraint being blamed have either started or stopped being emitted. After starting at this for quite a while, I think anything too fickle about whether it outputs based on the particular constraint being blamed should instead be looking at the constraint path as a whole, similar to what's done for [the placeholder-from-predicate note](https://github.com/rust-lang/rust/compare/master...dianne:rust:better-blame-constraints-for-static#diff-3c0de6462469af483c9ecdf2c4b00cb26192218ef2d5c62a0fde75107a74caaeR506). Very many tests involving invariant types gained a note pointing out the types' invariance, but in a few cases it was lost. A particularly illustrative example is [tests/ui/lifetimes/copy_modulo_regions.stderr](https://github.com/rust-lang/rust/compare/master...dianne:rust:better-blame-constraints-for-static?expand=1#diff-96e1f8b29789b3c4ce2f77a5e0fba248829b97ef9d1ce39e7d2b4aa57b2cf4f0); I'd argue the new constraint is a better one to blame, but it lacks the variance diagnostic information that's elsewhere in the constraint path. If desired, I can try making that note check the whole path rather than just the blamed constraint. The subdiagnostic [`BorrowExplanation::add_object_lifetime_default_note`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_borrowck/diagnostics/explain_borrow/enum.BorrowExplanation.html#method.add_object_lifetime_default_note) depends on a `Cast` being blamed, so [a special case](https://github.com/rust-lang/rust/pull/133858/commits/364ca7f99c12fb5220e6b568ac391979317ce878) was necessary to keep it from disappearing from tests specifically testing for it. However, see the FIXME comment in that commit; I think the special case should be removed once that subdiagnostic works properly, but it's nontrivial enough to warrant a separate PR. Incidentally, this removes the note from a test where it was being added erroneously: in [tests/ui/borrowck/two-phase-surprise-no-conflict.stderr](https://github.com/rust-lang/rust/compare/master...dianne:rust:better-blame-constraints-for-static?expand=1#diff-8cf085af8203677de6575a45458c9e6b03412a927df879412adec7e4f7ff5e14), the object lifetime is explicitly provided and it's not `'static`.
2025-01-08stop calling `DenseLocationMap` "elements"Rémy Rakic-2/+2
"Elements" are `RegionElement`s. The dense location mapping was removed from the element containers a while ago but didn't rename its use-sites. Most of the old naming only used the mapping, and are better named `location_map`.
2025-01-07Rollup merge of #133810 - lcnr:remove-verify_bound, r=compiler-errorsMatthias Krüger-18/+8
remove unnecessary `eval_verify_bound` This does not impact any tests. I feel like any cases where this could useful should instead be fixed by a general improvement to `eval_verify_bound` to avoid having to promote this `TypeTest` in the first place :thinking: r? types cc ``@nikomatsakis``
2025-01-06only avoid blaming assignments from argument patternsdianne-10/+7
2025-01-06make outlives constraints from generic arguments less boringdianne-10/+16
2025-01-06`best_blame_constraint`: prioritize blaming interesting-seeming constraintsdianne-72/+87
2025-01-06`best_blame_constraint`: avoid blaming assignments without user-provided typesdianne-0/+1
2025-01-06`best_blame_constraint`: avoid blaming constraints from MIR generated by ↵dianne-1/+8
desugaring
2025-01-06`best_blame_constraint`: add a special case to recover object lifetime ↵dianne-1/+21
default notes
2025-01-06`best_blame_constraint`: don't filter constraints by sup SCCdianne-49/+11
The SCCs of the region graph are not a reliable heuristic to use for blaming an interesting constraint for diagnostics. For region errors, if the outlived region is `'static`, or the involved types are invariant in their lifetiems, there will be cycles in the constraint graph containing both the target region and the most interesting constraints to blame. To get better diagnostics in these cases, this commit removes that heuristic.