about summary refs log tree commit diff
path: root/compiler/rustc_borrowck/src/nll.rs
AgeCommit message (Collapse)AuthorLines
2025-09-01Auto merge of #145925 - lcnr:revealing-use-closures-2, r=BoxyUwUbors-0/+32
`-Znext-solver`: support non-defining uses in closures Cleaned up version of rust-lang/rust#139587, finishing the implementation of https://github.com/rust-lang/types-team/issues/129. This does not affect stable. The reasoning for why this is the case is subtle however. ## What does it do We split `do_mir_borrowck` into `borrowck_collect_region_constraints` and `borrowck_check_region_constraints`, where `borrowck_collect_region_constraints` returns an enormous `CollectRegionConstraintsResult` struct which contains all the relevant data to actually handle opaque type uses and to check the region constraints later on. `query mir_borrowck` now simply calls `BorrowCheckRootCtxt::do_mir_borrowck` which starts by iterating over all nested bodies of the current function - visiting nested bodies before their parents - and computing their `CollectRegionConstraintsResult`. After we've collected all constraints it's time to actually compute the concrete types for the opaques defined by this function. With this PR we now compute the concrete types of opaques for each body before using them to check the non-defining uses of any of them. After we've computed the concrete types by using all bodies, we use `apply_computed_concrete_opaque_types` for each body to constrain non-defining uses, before finally finishing with `borrowck_check_region_constraints`. We always visit nested bodies before their parents when doing this. ## `ClosureRegionRequirements` As we only call `borrowck_collect_region_constraints` for nested bodies before type checking the parent, we can't simply use the final `ClosureRegionRequirements` of the nested body during MIR type check. We instead track that we need to apply these requirements in `deferred_closure_requirements`. We now manually apply the final closure requirements to each body after handling opaque types. This works, except that we may need the region constraints of nested bodies to successfully define an opaque type in the parent. This is handled by using a new `fn compute_closure_requirements_modulo_opaques` which duplicates region checking - while ignoring any errors - before we've added the constraints from `apply_computed_concrete_opaque_types`. This is necessary for a lot of async tests, as pretty much the entire function is inside of an async block while the opaque type gets defined in the parent. As an performance optimization we only use `fn compute_closure_requirements_modulo_opaques` in case the nested body actually depends on any opaque types. Otherwise we eagerly call `borrowck_check_region_constraints` and apply the final closure region requirements right away. ## Impact on stable code Handling the opaque type uses in the parent function now only uses the closure requirements *modulo opaques*, while it previously also considered member constraints from nested bodies. `External` regions are never valid choice regions. Also, member constraints will never constrain a member region if it is required to be outlived by an external region, as that fails the upper-bound check. https://github.com/rust-lang/rust/blob/564ee219127b796d56f74767366fd359758b97de/compiler/rustc_borrowck/src/region_infer/opaque_types/member_constraints.rs#L90-L96 Member constraints therefore never add constraints for external regions :> r? `@BoxyUwU`
2025-09-01use defining uses of all bodies to constrain non-defining useslcnr-0/+32
support non-defining uses in closures
2025-09-01Introduce `MirDumper` and `MirWriter`.Nicholas Nethercote-19/+16
MIR dumping is a mess. There are lots of functions and entry points, e.g. `dump_mir`, `dump_mir_with_options`, `dump_polonius_mir`, `dump_mir_to_writer`. Also, it's crucial that `create_dump_file` is never called without `dump_enabled` first being checked, but there is no mechanism for ensuring this and it's hard to tell if it is satisfied on all paths. (`dump_enabled` is checked twice on some paths, however!) This commit introduces `MirWriter`, which controls the MIR writing, and encapsulates the `extra_data` closure and `options`. Two existing functions are now methods of this type. It sets reasonable defaults, allowing the removal of many `|_, _| Ok(())` closures. The commit also introduces `MirDumper`, which is layered on top of `MirWriter`, and which manages the creation of the dump files, encapsulating pass names, disambiguators, etc. Four existing functions are now methods of this type. - `MirDumper::new` will only succeed if dumps are enabled, and will return `None` otherwise, which makes it impossible to dump when you shouldn't. - It also sets reasonable defaults for various things like disambiguators, which means you no longer need to specify them in many cases. When they do need to be specified, it's now done via setter methods. - It avoids some repetition. E.g. `dump_nll_mir` previously specifed the pass name `"nll"` four times and the disambiguator `&0` three times; now it specifies them just once, to put them in the `MirDumper`. - For Polonius, the `extra_data` closure can now be specified earlier, which avoids having to pass some arguments through some functions.
2025-09-01Use trait object references for closures.Nicholas Nethercote-2/+2
The dynamic dispatch cost doesn't matter for MIR dumping, which is perf-insensitive. And it's necessary for the next commit, which will store some `extra_data` closures in a struct.
2025-08-08borrowck: defer opaque type errorslcnr-5/+0
2025-08-07move `type_check` out of `compute_regions`lcnr-34/+12
2025-07-08Expose nested bodies in rustc_borrowck::consumersNico Lehmann-4/+2
2025-07-04Remove Symbol for Named LateParam/Bound variantsMichael Goulet-2/+2
2025-06-05Auto merge of #140466 - amandasystems:move-to-preprocessing-step, r=lcnrbors-3/+14
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-2/+2
Co-authored-by: lcnr <rust@lcnr.de>
2025-06-03Move placeholder handling to a proper preprocessing stepAmanda Stjerna-3/+14
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-27move `MaybeInitializedPlaces` computation to where it's usedRémy Rakic-5/+1
This dataflow analysis is only used by `liveness::trace`. We move it there to make it lazy.
2025-04-11eagerly initialize `definitions` in sub-fnlcnr-12/+2
2025-04-11do not buffer `#[rustc_regions]` dumplcnr-5/+2
2025-04-08move `ClosureRegionRequirements` to `rustc_borrowck`lcnr-5/+5
2025-04-08borrowck typeck children together with their parentlcnr-13/+6
2025-03-26Add environment variable tracking in places where it was convenientMads Marquart-4/+3
This won't work with Cargo's change tracking, but it should work with incremental.
2025-03-19merge opaque types of nested bodieslcnr-9/+11
2025-01-31record boring locals in polonius contextRémy Rakic-1/+1
this is used in diagnostics to focus on relevant live locals to match NLL diagnostics
2025-01-31create context for errors and diagnostics for last borrowck phaseRémy Rakic-5/+6
2025-01-12replace location-insensitive analysis with location-sensitive analysisRémy Rakic-1/+1
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-12introduce reachability to the constraint graphRémy Rakic-5/+5
2025-01-08rename `AllFacts` to `PoloniusFacts`Rémy Rakic-10/+12
This is another strangely named struct (and associated fields) that is hard to see was related to datalog polonius.
2025-01-08rename `LocationTable` to `PoloniusLocationTable`Rémy Rakic-2/+2
Its original naming hides the fact that it's related to datalog polonius, and bound to be deleted in the near future. It also conflicts with the expected name for the actual NLL location map, and prefixing it with its use will make the differentiation possible.
2025-01-08stop calling `DenseLocationMap` "elements"Rémy Rakic-3/+3
"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-01localize typeck constraintsRémy Rakic-3/+3
it's still partially a skeleton, but works well enough for almost all tests to pass
2024-12-30rename `diags` fieldRémy Rakic-2/+2
2024-12-30clean up `BorrowckDiags`Rémy Rakic-2/+2
- rename it to what it does, buffer diagnostics - remove single-use functions - use derives
2024-12-30merge `diags` module into `diagnostics`Rémy Rakic-2/+2
it's a more natural place for diagnostics-related structures and functions
2024-12-30move `facts` module to polonius legacy moduleRémy Rakic-4/+1
this is specific to the old datalog implementation and wasn't noticed in the previous module move
2024-12-30move `location` module to polonius legacy moduleRémy Rakic-1/+1
this is specific to the old datalog implementation and wasn't noticed in the previous module move
2024-12-30fix a couple nitsRémy Rakic-2/+2
- remove unneeded type ascription - fix variable name - fix typo in comment - fix `var_origins` var and function name: these are `VarInfos`
2024-12-29introduce polonius contextRémy Rakic-19/+20
This context struct will hold data to help creating localized constraints: - the live regions, with the shape matching a CFG walk, indexed per point - the variance of these live regions, represented as the direction we'll add the appropriate We also add this structure to the mir typeck to record liveness data, and make it responsible for localized constraint creation.
2024-12-18address review commentsRémy Rakic-10/+7
- move constraints to an Option - check `-Zpolonius=next` only once - rewrite fixme comments to make the actionable part clear
2024-12-18extract main NLL MIR dump functionRémy Rakic-34/+46
this will allow calling from polonius MIR
2024-12-18set up skeleton for localized constraints conversionRémy Rakic-0/+17
2024-12-18Auto merge of #134243 - nnethercote:re-export-more-rustc_span, r=jieyouxubors-1/+1
Re-export more `rustc_span::symbol` things from `rustc_span`. `rustc_span::symbol` defines some things that are re-exported from `rustc_span`, such as `Symbol` and `sym`. But it doesn't re-export some closely related things such as `Ident` and `kw`. So you can do `use rustc_span::{Symbol, sym}` but you have to do `use rustc_span::symbol::{Ident, kw}`, which is inconsistent for no good reason. This commit re-exports `Ident`, `kw`, and `MacroRulesNormalizedIdent`, and changes many `rustc_span::symbol::` qualifiers to `rustc_span::`. This is a 300+ net line of code reduction, mostly because many files with two `use rustc_span` items can be reduced to one. r? `@jieyouxu`
2024-12-18Re-export more `rustc_span::symbol` things from `rustc_span`.Nicholas Nethercote-1/+1
`rustc_span::symbol` defines some things that are re-exported from `rustc_span`, such as `Symbol` and `sym`. But it doesn't re-export some closely related things such as `Ident` and `kw`. So you can do `use rustc_span::{Symbol, sym}` but you have to do `use rustc_span::symbol::{Ident, kw}`, which is inconsistent for no good reason. This commit re-exports `Ident`, `kw`, and `MacroRulesNormalizedIdent`, and changes many `rustc_span::symbol::` qualifiers in `compiler/` to `rustc_span::`. This is a 200+ net line of code reduction, mostly because many files with two `use rustc_span` items can be reduced to one.
2024-12-15simplify `emit_outlives_facts`Rémy Rakic-0/+1
- integrate into `emit_facts` and remove from typeck
2024-12-14move datalog fact generation into a legacy moduleRémy Rakic-1/+1
2024-11-19Pass `flow_inits` by value.Nicholas Nethercote-1/+1
It's simpler that way, and we don't need the explicit `drop`.
2024-11-19Put `param_env` into `infcx`.Nicholas Nethercote-5/+2
Because they get passed around together a lot.
2024-11-19Pass `constraints` to `RegionInferenceContext::new`.Nicholas Nethercote-26/+2
Instead of destructuring it in advance and passing all the components individually. It's less code that way.
2024-11-19Don't refcount `PlaceholderIndices`.Nicholas Nethercote-1/+0
It's not necessary.
2024-11-19Compute `upvars` lazily.Nicholas Nethercote-2/+0
It can be computed from `tcx` on demand, instead of computing it eagerly and passing it around.
2024-11-19Clean up `UniversalRegions`.Nicholas Nethercote-6/+2
There is an `Rc<UniversalRegions>` within `UniversalRegionRelations`, and yet the two types get passed around in tandem a lot. This commit makes `UniversalRegionRelations` own `UniversalRegions`, removing the `Rc` (which wasn't truly needed) and the tandem-passing. This requires adding a `universal_relations` method to `UniversalRegionRelations`, and renaming a couple of existing methods producing iterators to avoid a name clash.
2024-10-28fix clippy::clone_on_ref_ptr for compilerklensy-2/+2
2024-10-12yeet some clonesMatthias Krüger-1/+1
2024-10-04Avoid `&Rc<T>` arguments.Nicholas Nethercote-4/+4
Either `&T` or `Rc<T>` is preferable.
2024-10-04Use `Box` instead of `Rc` for `polonius_output`.Nicholas Nethercote-2/+2
Refcounting isn't needed.