about summary refs log tree commit diff
path: root/compiler/rustc_borrowck/src/dataflow.rs
AgeCommit message (Collapse)AuthorLines
2025-06-13use `MixedBitSet` for borrows-in-scope computationRémy Rakic-3/+3
Some code can have a few thousand loans, and this bitset is better suited to these somewhat sparse cases. That includes a couple of rustc-perf benchmarks.
2025-01-12move out of scope precomputer codeRémy Rakic-34/+32
this addresses review comments while: - keeping the symmetry between the NLL and Polonius out of scope precomputers - keeping the unstable `calculate_borrows_out_of_scope_at_location` function to avoid churn for consumers
2025-01-12replace location-insensitive analysis with location-sensitive analysisRémy Rakic-36/+27
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-11rename `BitSet` to `DenseBitSet`Rémy Rakic-7/+7
This should make it clearer that this bitset is dense, with the advantages and disadvantages that it entails.
2024-12-16Simplify dataflow `SwitchInt` handling.Nicholas Nethercote-11/+1
Current `SwitchInt` handling has complicated control flow. - The dataflow engine calls `Analysis::apply_switch_int_edge_effects`, passing in an "applier" that impls `SwitchIntEdgeEffects`. - `apply_switch_int_edge_effects` possibly calls `apply` on the applier, passing it a closure. - The `apply` method calls the closure on each `SwitchInt` edge. - The closure operates on the edge. I.e. control flow goes from the engine, to the analysis, to the applier (which came from the engine), to the closure (which came from the analysis). It took me a while to work this out. This commit changes to a simpler structure that maintains the important characteristics. - The dataflow engine calls `Analysis::get_switch_int_data`. - `get_switch_int_data` returns an `Option<Self::SwitchIntData>` value. - If that returned value was `Some`, the dataflow engine calls `Analysis::apply_switch_int_edge_effect` on each edge, passing the `Self::SwitchIntData` value. - `Analysis::apply_switch_int_edge_effect` operates on the edge. I.e. control flow goes from the engine, to the analysis, to the engine, to the analysis. Added: - The `Analysis::SwitchIntData` assoc type and the `Analysis::get_switch_int_data` method. Both only need to be defined by analyses that look at `SwitchInt` terminators. - The `MaybePlacesSwitchIntData` struct, which has three fields. Changes: - `Analysis::apply_switch_int_edge_effects` becomes `Analysis::apply_switch_int_edge_effect`, which is a little simpler because it's dealing with a single edge instead of all edges. Removed: - The `SwitchIntEdgeEffects` trait, and its two impls: `BackwardSwitchIntEdgeEffectsApplier` (which has six fields) and `ForwardSwitchIntEdgeEffectsApplier` structs (which has four fields). - The closure. The new structure is more concise and simpler.
2024-12-10Rename some `Analysis` and `ResultsVisitor` methods.Nicholas Nethercote-20/+20
The words "before" and "after" have an obvious temporal meaning, e.g. `seek_before_primary_effect`, `visit_statement_{before,after}_primary_effect`. But "before" is also used to name the effect that occurs before the primary effect of a statement/terminator; this is `Effect::Before`. This leads to the confusing possibility of talking about things happening "before/after the before event". This commit removes this awkward overloading of "before" by renaming `Effect::Before` as `Effect::Early`. It also renames some of the `Analysis` and `ResultsVisitor` methods to be more consistent. Here are the before and after names: - `Effect::{Before,Primary}` -> `Effect::{Early,Primary}` - `apply_before_statement_effect` -> `apply_early_statement_effect` - `apply_statement_effect` -> `apply_primary_statement_effect` - `visit_statement_before_primary_effect` -> `visit_after_early_statement_effect` - `visit_statement_after_primary_effect` -> `visit_after_primary_statement_effect` (And s/statement/terminator/ for all the terminator events.)
2024-12-10Call all `Domain` values `state`.Nicholas Nethercote-15/+15
Currently they are called (most common) `state`, or `trans`, or (rare) `on_entry`. I think `trans` is short for "transfer function", which perhaps made more sense when `GenKillAnalysis` existed. Using `state` everywhere now is more consistent.
2024-12-10Remove lifetimes from `BorrowckDomain`.Nicholas Nethercote-9/+14
They are only present because it's currently defined in terms of the domains of `Borrows` and `MaybeUninitializedPlaces` and `EverInitializedPlaces` via associated types. This commit introduces typedefs for those domains, avoiding the lifetimes.
2024-11-20reduce false positives of tail-expr-drop-order from consumed valuesDing Xiang Fei-0/+1
take 2 open up coroutines tweak the wordings the lint works up until 2021 We were missing one case, for ADTs, which was causing `Result` to yield incorrect results. only include field spans with significant types deduplicate and eliminate field spans switch to emit spans to impl Drops Co-authored-by: Niko Matsakis <nikomat@amazon.com> collect drops instead of taking liveness diff apply some suggestions and add explantory notes small fix on the cache let the query recurse through coroutine new suggestion format with extracted variable name fine-tune the drop span and messages bugfix on runtime borrows tweak message wording filter out ecosystem types earlier apply suggestions clippy check lint level at session level further restrict applicability of the lint translate bid into nop for stable mir detect cycle in type structure
2024-11-05Replace `BorrowckResults` with `Borrowck`.Nicholas Nethercote-41/+119
The results of most analyses end up in a `Results<'tcx, A>`, where `A` is the analysis. It's then possible to traverse the results via a `ResultsVisitor`, which relies on the `ResultsVisitable` trait. (That trait ends up using the same `apply_*` methods that were used when computing the analysis, albeit indirectly.) This pattern of "compute analysis results, then visit them" is common. But there is one exception. For borrow checking we compute three separate analyses (`Borrows`, `MaybeUninitializedPlaces`, and `EverInitializedPlaces`), combine them into a single `BorrowckResults`, and then do a single visit of that `BorrowckResults` with `MirBorrowckResults`. `BorrowckResults` is just different enough from `Results` that it requires the existence of `ResultsVisitable`, which abstracts over the traversal differences between `Results` and `BorrowckResults`. This commit changes things by introducing `Borrowck` and bundling the three borrowck analysis results into a standard `Results<Borrowck>` instead of the exceptional `BorrowckResults`. Once that's done, the results can be visited like any other analysis results. `BorrowckResults` is removed, as is `impl ResultsVisitable for BorrowckResults`. (It's instructive to see how similar the added `impl Analysis for Borrowck` is to the removed `impl ResultsVisitable for BorrowckResults`. They're both doing exactly the same things.) Overall this increases the number of lines of code and might not seem like a win. But it enables the removal of `ResultsVisitable` in the next commit, which results in many simplifications.
2024-11-04Tidy up comments and some formatting.Nicholas Nethercote-2/+2
Mostly by wrapping overly long comment lines, plus a few other things.
2024-10-14Add defaults for `Analysis::apply_{call_return_effect,terminator_effect}`.Nicholas Nethercote-11/+1
To avoid some low-value boilerplate code.
2024-10-14Merge `AnalysisDomain` into `Analysis`.Nicholas Nethercote-16/+14
With `GenKillAnalysis` gone, there is no need for them to be separate.
2024-10-14Remove `GenKillAnalysis`.Nicholas Nethercote-12/+6
It's now functionally identical to `Analysis`.
2024-10-14Minimize use of `GenKill`.Nicholas Nethercote-2/+6
Thanks to the previous couple of commits, many uses of the `GenKill` trait can be replaced with a concrete type.
2024-10-14Tweak `GenKillAnalysis` method arguments.Nicholas Nethercote-2/+2
`GenKillAnalysis` has very similar methods to `Analysis`, but the first two have a notable difference: the second argument is `&mut impl GenKill<Self::Idx>` instead of `&mut Self::Domain`. But thanks to the previous commit, this difference is no longer necessary.
2024-10-02make `Borrows` dataflow dumps about its loan domainRémy Rakic-11/+1
2024-09-22Reformat using the new identifier sorting from rustfmtMichael Goulet-1/+1
2024-09-14Rollup merge of #130294 - nnethercote:more-lifetimes, r=lcnrLeón Orell Valerian Liehr-10/+10
Lifetime cleanups The last commit is very opinionated, let's see how we go. r? `@oli-obk`
2024-09-13Rename `FlowState` as `Domain`.Nicholas Nethercote-12/+11
Because that's what it is; no point having a different name for it.
2024-09-13Rename and reorder lots of lifetimes.Nicholas Nethercote-10/+10
- Replace non-standard names like 's, 'p, 'rg, 'ck, 'parent, 'this, and 'me with vanilla 'a. These are cases where the original name isn't really any more informative than 'a. - Replace names like 'cx, 'mir, and 'body with vanilla 'a when the lifetime applies to multiple fields and so the original lifetime name isn't really accurate. - Put 'tcx last in lifetime lists, and 'a before 'b.
2024-09-09Remove unnecessary lifetimes in dataflow structs.Nicholas Nethercote-20/+19
There are four related dataflow structs: `MaybeInitializedPlaces`, `MaybeUninitializedPlaces`, and `EverInitializedPlaces`, `DefinitelyInitializedPlaces`. They all have a `&Body` and a `&MoveData<'tcx>` field. The first three use different lifetimes for the two fields, but the last one uses the same lifetime for both. This commit changes the first three to use the same lifetime, removing the need for one of the lifetimes. Other structs that also lose a lifetime as a result of this are `LivenessContext`, `LivenessResults`, `InitializationData`. It then does similar things in various other structs.
2024-08-30Remove `#[macro_use] extern crate tracing` from `rustc_borrowck`.Nicholas Nethercote-0/+1
2024-08-16Add `warn(unreachable_pub)` to `rustc_borrowck`.Nicholas Nethercote-2/+2
2024-07-29Reformat `use` declarations.Nicholas Nethercote-6/+5
The previous commit updated `rustfmt.toml` appropriately. This commit is the outcome of running `x fmt --all` with the new formatting options.
2024-07-14Stop using the gen keyword in the compilerMichael Goulet-1/+1
2024-06-26Split lifetimes on mir borrowck dataflowOli Scherer-20/+20
2024-06-05Remove confusing `use_polonius` flag and do less cloningAmanda Stjerna-3/+3
The `use_polonius` flag is both redundant and confusing since every function it's propagated to also checks if `all_facts` is `Some`, the true test of whether to generate Polonius facts for Polonius or for external consumers. This PR makes that path clearer by simply doing away with the argument and handling the logic in precisely two places: where facts are populated (check for `Some`), and where `all_facts` are initialised. It also delays some statements until after that check to avoid the miniscule performance penalty of executing them when Polonius is disabled. This also addresses @lqd's concern in #125652 by reducing the size of what is cloned out of Polonius facts to just the facts being added, as opposed to the entire vector of potential inputs, and added descriptive comments. *Reviewer note*: the comments in [add_extra_drop_facts](https://github.com/rust-lang/rust/blob/85f90a461262f7ca37a6e629933d455fa9c3ee48/compiler/rustc_borrowck/src/type_check/liveness/trace.rs#L219) should be inspected by a reviewer, in particular the one on L#259 in this PR, which should be trivial for someone with the right background knowledge. I also included some minor lints I found on the way there that I couldn't help myself from addressing.
2024-04-14Make `depth_first_search` into a standalone functionMaybe Waffle-2/+2
Does not necessarily change much, but we never overwrite it, so I see no reason for it to be in the `Successors` trait. (+we already have a similar `is_cyclic`)
2024-04-14Merge `{With,Graph}{Successors,Predecessors}` into `{Successors,Predecessors}`Maybe Waffle-1/+1
Now with GAT!
2024-02-06Invert diagnostic lints.Nicholas Nethercote-2/+0
That is, change `diagnostic_outside_of_impl` and `untranslatable_diagnostic` from `allow` to `deny`, because more than half of the compiler has be converted to use translated diagnostics. This commit removes more `deny` attributes than it adds `allow` attributes, which proves that this change is warranted.
2023-12-10remove redundant importssurechen-1/+1
detects redundant imports that can be eliminated. for #117772 : In order to facilitate review and modification, split the checking code and removing redundant imports code into two PR.
2023-12-08Tweak `GenKillAnalysis`.Nicholas Nethercote-2/+2
`GenKillAnalysis` has five methods that take a transfer function arg: - `statement_effect` - `before_statement_effect` - `terminator_effect` - `before_terminator_effect` - `call_return_effect` All the transfer function args have type `&mut impl GenKill<Self::Idx>`, except for `terminator_effect`, which takes the simpler `Self::Domain`. But only the first two need to be `impl GenKill`. The other three can all be `Self::Domain`, just like `Analysis`. So this commit changes the last two to take `Self::Domain`, making `GenKillAnalysis` and `Analysis` more similar. (Another idea would be to make all these methods `impl GenKill`. But that doesn't work: `MaybeInitializedPlaces::terminator_effect` requires the arg be `Self::Domain` so that `self_is_unwind_dead(place, state)` can be called on it.)
2023-12-08Remove `BorrowckAnalyses`.Nicholas Nethercote-28/+18
This results in two non-generic types being used: `BorrowckResults` and `BorrowckFlowState`. It's a net reduction in lines of code, and a little easier to read.
2023-12-08Remove `impl_visitable!`.Nicholas Nethercote-65/+62
It is used just once. With it removed, the relevant code is a little boilerplate-y but much easier to read, and is the same length. Overall I think it's an improvement.
2023-12-01improve NLL/polonius scope equality assertionRémy Rakic-1/+2
2023-11-25Auto merge of #118203 - nnethercote:rustc_mir_dataflow, r=cjgillotbors-17/+17
Minor `rustc_mir_dataflow` cleanups r? `@cjgillot`
2023-11-23Use `'mir` lifetime name more.Nicholas Nethercote-17/+17
Some types have a `body: &'mir Body<'tcx>` and some have `body: &'a Body<'tcx>`. The former is more readable, so this commit converts some fo the latter to the former.
2023-11-22Replace `no_ord_impl` with `orderable`.Nicholas Nethercote-0/+1
Similar to the previous commit, this replaces `newtype_index`'s opt-out `no_ord_impl` attribute with the opt-in `orderable` attribute.
2023-11-04traverse region graph instead of SCCs to compute polonius loan scopesRémy Rakic-3/+3
By using SCC for better performance, we also have to take into account SCCs whose representative is an existential region but also contains a placeholder. By only checking the representative, we may miss that the loan escapes the function. This can be fixed by picking a better representative, or removing placeholders from the main path. This is the simplest fix: forgo efficiency and traverse the region graph instead of the SCCs.
2023-10-20consider a loan escapes the function via applied member constraintsRémy Rakic-7/+14
2023-10-05compute NLL loan scopes with liveness in `-Zpolonius=next`Rémy Rakic-1/+183
2023-10-05add note why a function is pubRémy Rakic-0/+1
2023-10-04document Borrows dataflow analysisRémy Rakic-0/+7
2023-10-04rename ancient regioncxRémy Rakic-2/+2
2023-08-16Make TerminatorEdge plural.Camille GILLOT-2/+2
2023-08-16Allow apply_terminator_effect to customize edges.Camille GILLOT-6/+9
2023-08-16Move domain_size to GenKillAnalysis.Camille GILLOT-0/+4
2023-07-25inline format!() args from rustc_codegen_llvm to the end (4)Matthias Krüger-1/+1
r? @WaffleLapkin
2023-06-28Rollup merge of #112236 - cjgillot:interval-kill, r=davidtwcoDylan DPC-51/+43
Simplify computation of killed borrows Follow-up to https://github.com/rust-lang/rust/pull/111759 Processing the first block manually once makes the pre-order walk simpler.