2017-08-21 09:31:35 +00:00
|
|
|
//! This query borrow-checks the MIR to (further) ensure it is not broken.
|
2017-07-05 12:52:18 +00:00
|
|
|
|
Use `tidy` to sort crate attributes for all compiler crates.
We already do this for a number of crates, e.g. `rustc_middle`,
`rustc_span`, `rustc_metadata`, `rustc_span`, `rustc_errors`.
For the ones we don't, in many cases the attributes are a mess.
- There is no consistency about order of attribute kinds (e.g.
`allow`/`deny`/`feature`).
- Within attribute kind groups (e.g. the `feature` attributes),
sometimes the order is alphabetical, and sometimes there is no
particular order.
- Sometimes the attributes of a particular kind aren't even grouped
all together, e.g. there might be a `feature`, then an `allow`, then
another `feature`.
This commit extends the existing sorting to all compiler crates,
increasing consistency. If any new attribute line is added there is now
only one place it can go -- no need for arbitrary decisions.
Exceptions:
- `rustc_log`, `rustc_next_trait_solver` and `rustc_type_ir_macros`,
because they have no crate attributes.
- `rustc_codegen_gcc`, because it's quasi-external to rustc (e.g. it's
ignored in `rustfmt.toml`).
2024-06-12 03:49:36 +00:00
|
|
|
// tidy-alphabetical-start
|
2023-10-16 22:22:17 +00:00
|
|
|
#![allow(internal_features)]
|
2023-11-13 12:39:17 +00:00
|
|
|
#![doc(rust_logo)]
|
2024-01-24 22:27:25 +00:00
|
|
|
#![feature(assert_matches)]
|
2020-12-30 17:48:40 +00:00
|
|
|
#![feature(box_patterns)]
|
2024-09-24 21:25:16 +00:00
|
|
|
#![feature(file_buffered)]
|
2024-12-12 10:00:07 +00:00
|
|
|
#![feature(if_let_guard)]
|
2022-08-20 18:40:08 +00:00
|
|
|
#![feature(let_chains)]
|
2022-02-14 16:10:22 +00:00
|
|
|
#![feature(never_type)]
|
2022-06-22 12:24:35 +00:00
|
|
|
#![feature(rustc_attrs)]
|
Use `tidy` to sort crate attributes for all compiler crates.
We already do this for a number of crates, e.g. `rustc_middle`,
`rustc_span`, `rustc_metadata`, `rustc_span`, `rustc_errors`.
For the ones we don't, in many cases the attributes are a mess.
- There is no consistency about order of attribute kinds (e.g.
`allow`/`deny`/`feature`).
- Within attribute kind groups (e.g. the `feature` attributes),
sometimes the order is alphabetical, and sometimes there is no
particular order.
- Sometimes the attributes of a particular kind aren't even grouped
all together, e.g. there might be a `feature`, then an `allow`, then
another `feature`.
This commit extends the existing sorting to all compiler crates,
increasing consistency. If any new attribute line is added there is now
only one place it can go -- no need for arbitrary decisions.
Exceptions:
- `rustc_log`, `rustc_next_trait_solver` and `rustc_type_ir_macros`,
because they have no crate attributes.
- `rustc_codegen_gcc`, because it's quasi-external to rustc (e.g. it's
ignored in `rustfmt.toml`).
2024-06-12 03:49:36 +00:00
|
|
|
#![feature(rustdoc_internals)]
|
2020-12-30 17:48:40 +00:00
|
|
|
#![feature(stmt_expr_attributes)]
|
|
|
|
#![feature(try_blocks)]
|
2024-07-06 11:53:49 +00:00
|
|
|
#![warn(unreachable_pub)]
|
Use `tidy` to sort crate attributes for all compiler crates.
We already do this for a number of crates, e.g. `rustc_middle`,
`rustc_span`, `rustc_metadata`, `rustc_span`, `rustc_errors`.
For the ones we don't, in many cases the attributes are a mess.
- There is no consistency about order of attribute kinds (e.g.
`allow`/`deny`/`feature`).
- Within attribute kind groups (e.g. the `feature` attributes),
sometimes the order is alphabetical, and sometimes there is no
particular order.
- Sometimes the attributes of a particular kind aren't even grouped
all together, e.g. there might be a `feature`, then an `allow`, then
another `feature`.
This commit extends the existing sorting to all compiler crates,
increasing consistency. If any new attribute line is added there is now
only one place it can go -- no need for arbitrary decisions.
Exceptions:
- `rustc_log`, `rustc_next_trait_solver` and `rustc_type_ir_macros`,
because they have no crate attributes.
- `rustc_codegen_gcc`, because it's quasi-external to rustc (e.g. it's
ignored in `rustfmt.toml`).
2024-06-12 03:49:36 +00:00
|
|
|
// tidy-alphabetical-end
|
2020-12-30 17:48:40 +00:00
|
|
|
|
2024-12-04 18:58:59 +00:00
|
|
|
use std::borrow::Cow;
|
2019-12-29 02:36:42 +00:00
|
|
|
use std::cell::RefCell;
|
2023-11-25 20:45:44 +00:00
|
|
|
use std::marker::PhantomData;
|
2024-12-30 11:27:24 +00:00
|
|
|
use std::ops::{ControlFlow, Deref};
|
2024-07-28 22:13:50 +00:00
|
|
|
|
2024-11-03 03:24:15 +00:00
|
|
|
use rustc_abi::FieldIdx;
|
2023-03-04 12:40:38 +00:00
|
|
|
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
|
Merge indexed_set.rs into bitvec.rs, and rename it bit_set.rs.
Currently we have two files implementing bitsets (and 2D bit matrices).
This commit combines them into one, taking the best features from each.
This involves renaming a lot of things. The high level changes are as
follows.
- bitvec.rs --> bit_set.rs
- indexed_set.rs --> (removed)
- BitArray + IdxSet --> BitSet (merged, see below)
- BitVector --> GrowableBitSet
- {,Sparse,Hybrid}IdxSet --> {,Sparse,Hybrid}BitSet
- BitMatrix --> BitMatrix
- SparseBitMatrix --> SparseBitMatrix
The changes within the bitset types themselves are as follows.
```
OLD OLD NEW
BitArray<C> IdxSet<T> BitSet<T>
-------- ------ ------
grow - grow
new - (remove)
new_empty new_empty new_empty
new_filled new_filled new_filled
- to_hybrid to_hybrid
clear clear clear
set_up_to set_up_to set_up_to
clear_above - clear_above
count - count
contains(T) contains(&T) contains(T)
contains_all - superset
is_empty - is_empty
insert(T) add(&T) insert(T)
insert_all - insert_all()
remove(T) remove(&T) remove(T)
words words words
words_mut words_mut words_mut
- overwrite overwrite
merge union union
- subtract subtract
- intersect intersect
iter iter iter
```
In general, when choosing names I went with:
- names that are more obvious (e.g. `BitSet` over `IdxSet`).
- names that are more like the Rust libraries (e.g. `T` over `C`,
`insert` over `add`);
- names that are more set-like (e.g. `union` over `merge`, `superset`
over `contains_all`, `domain_size` over `num_bits`).
Also, using `T` for index arguments seems more sensible than `&T` --
even though the latter is standard in Rust collection types -- because
indices are always copyable. It also results in fewer `&` and `*`
sigils in practice.
2018-09-14 05:07:25 +00:00
|
|
|
use rustc_data_structures::graph::dominators::Dominators;
|
2025-01-06 03:25:45 +00:00
|
|
|
use rustc_errors::LintDiagnostic;
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir as hir;
|
2024-12-04 18:58:59 +00:00
|
|
|
use rustc_hir::CRATE_HIR_ID;
|
2020-05-11 08:29:05 +00:00
|
|
|
use rustc_hir::def_id::LocalDefId;
|
2025-01-07 15:19:05 +00:00
|
|
|
use rustc_index::bit_set::{DenseBitSet, MixedBitSet};
|
2023-04-19 10:57:17 +00:00
|
|
|
use rustc_index::{IndexSlice, IndexVec};
|
2024-05-06 15:20:13 +00:00
|
|
|
use rustc_infer::infer::{
|
|
|
|
InferCtxt, NllRegionVariableOrigin, RegionVariableOrigin, TyCtxtInferExt,
|
|
|
|
};
|
2023-09-30 15:14:07 +00:00
|
|
|
use rustc_middle::mir::tcx::PlaceTy;
|
|
|
|
use rustc_middle::mir::*;
|
2023-05-15 04:24:45 +00:00
|
|
|
use rustc_middle::query::Providers;
|
2024-11-27 17:37:39 +00:00
|
|
|
use rustc_middle::ty::fold::fold_regions;
|
2024-10-17 22:28:43 +00:00
|
|
|
use rustc_middle::ty::{self, ParamEnv, RegionVid, TyCtxt, TypingMode};
|
2024-04-29 03:56:41 +00:00
|
|
|
use rustc_middle::{bug, span_bug};
|
2021-01-05 18:53:07 +00:00
|
|
|
use rustc_mir_dataflow::impls::{
|
2020-12-30 17:48:40 +00:00
|
|
|
EverInitializedPlaces, MaybeInitializedPlaces, MaybeUninitializedPlaces,
|
2024-07-28 22:13:50 +00:00
|
|
|
};
|
2023-09-30 20:23:34 +00:00
|
|
|
use rustc_mir_dataflow::move_paths::{
|
2024-12-30 00:56:14 +00:00
|
|
|
InitIndex, InitLocation, LookupResult, MoveData, MovePathIndex,
|
2024-07-28 22:13:50 +00:00
|
|
|
};
|
2024-11-26 03:21:17 +00:00
|
|
|
use rustc_mir_dataflow::{Analysis, EntryStates, Results, ResultsVisitor, visit_results};
|
2024-12-04 18:58:59 +00:00
|
|
|
use rustc_session::lint::builtin::{TAIL_EXPR_DROP_ORDER, UNUSED_MUT};
|
2023-02-16 22:14:04 +00:00
|
|
|
use rustc_span::{Span, Symbol};
|
2020-01-05 01:37:57 +00:00
|
|
|
use smallvec::SmallVec;
|
2024-08-29 22:39:41 +00:00
|
|
|
use tracing::{debug, instrument};
|
2022-08-26 02:32:59 +00:00
|
|
|
|
2024-10-04 00:21:55 +00:00
|
|
|
use crate::borrow_set::{BorrowData, BorrowSet};
|
|
|
|
use crate::consumers::{BodyWithBorrowckFacts, ConsumerOptions};
|
Replace `BorrowckResults` with `Borrowck`.
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-10-31 00:02:22 +00:00
|
|
|
use crate::dataflow::{BorrowIndex, Borrowck, BorrowckDomain, Borrows};
|
2024-12-30 01:16:52 +00:00
|
|
|
use crate::diagnostics::{
|
|
|
|
AccessKind, BorrowckDiagnosticsBuffer, IllegalMoveOriginKind, MoveError, RegionName,
|
|
|
|
};
|
2024-10-04 00:21:55 +00:00
|
|
|
use crate::path_utils::*;
|
|
|
|
use crate::place_ext::PlaceExt;
|
|
|
|
use crate::places_conflict::{PlaceConflictBias, places_conflict};
|
2025-01-08 11:22:03 +00:00
|
|
|
use crate::polonius::legacy::{PoloniusLocationTable, PoloniusOutput};
|
2024-10-04 00:21:55 +00:00
|
|
|
use crate::prefixes::PrefixSet;
|
|
|
|
use crate::region_infer::RegionInferenceContext;
|
|
|
|
use crate::renumber::RegionCtxt;
|
2022-08-26 02:32:59 +00:00
|
|
|
use crate::session_diagnostics::VarNeedNotMut;
|
2018-05-19 06:47:48 +00:00
|
|
|
|
2024-10-04 00:04:34 +00:00
|
|
|
mod borrow_set;
|
2020-12-30 17:48:40 +00:00
|
|
|
mod borrowck_errors;
|
2019-12-06 16:02:48 +00:00
|
|
|
mod constraints;
|
2020-12-30 17:48:40 +00:00
|
|
|
mod dataflow;
|
2020-05-07 19:55:01 +00:00
|
|
|
mod def_use;
|
2019-11-27 17:39:25 +00:00
|
|
|
mod diagnostics;
|
2019-11-30 01:48:29 +00:00
|
|
|
mod member_constraints;
|
2019-12-22 22:42:04 +00:00
|
|
|
mod nll;
|
2018-06-22 03:10:52 +00:00
|
|
|
mod path_utils;
|
2019-12-06 16:02:48 +00:00
|
|
|
mod place_ext;
|
|
|
|
mod places_conflict;
|
2023-11-23 11:47:41 +00:00
|
|
|
mod polonius;
|
2017-12-06 18:27:38 +00:00
|
|
|
mod prefixes;
|
2019-11-30 01:48:29 +00:00
|
|
|
mod region_infer;
|
|
|
|
mod renumber;
|
2022-06-23 04:43:01 +00:00
|
|
|
mod session_diagnostics;
|
2019-12-06 16:02:48 +00:00
|
|
|
mod type_check;
|
|
|
|
mod universal_regions;
|
|
|
|
mod used_muts;
|
New `ActiveBorrows` dataflow for two-phase `&mut`; not yet borrowed-checked.
High-level picture: The old `Borrows` analysis is now called
`Reservations` (implemented as a newtype wrapper around `Borrows`);
this continues to compute whether a `Rvalue::Ref` can reach a
statement without an intervening `EndRegion`. In addition, we also
track what `Place` each such `Rvalue::Ref` was immediately assigned
to in a given borrow (yay for MIR-structural properties!).
The new `ActiveBorrows` analysis then tracks the initial use of any of
those assigned `Places` for a given borrow. I.e. a borrow becomes
"active" immediately after it starts being "used" in some way. (This
is conservative in the sense that we will treat a copy `x = y;` as a
use of `y`; in principle one might further delay activation in such
cases.)
The new `ActiveBorrows` analysis needs to take the `Reservations`
results as an initial input, because the reservation state influences
the gen/kill sets for `ActiveBorrows`. In particular, a use of `a`
activates a borrow `a = &b` if and only if there exists a path (in the
control flow graph) from the borrow to that use. So we need to know if
the borrow reaches a given use to know if it really gets a gen-bit or
not.
* Incorporating the output from one dataflow analysis into the input
of another required more changes to the infrastructure than I had
expected, and even after those changes, the resulting code is still
a bit subtle.
* In particular, Since we need to know the intrablock reservation
state, we need to dynamically update a bitvector for the
reservations as we are also trying to compute the gen/kills
bitvector for the active borrows.
* The way I ended up deciding to do this (after also toying with at
least two other designs) is to put both the reservation state and
the active borrow state into a single bitvector. That is why we now
have separate (but related) `BorrowIndex` and
`ReserveOrActivateIndex`: each borrow index maps to a pair of
neighboring reservation and activation indexes.
As noted above, these changes are solely adding the active borrows
dataflow analysis (and updating the existing code to cope with the
switch from `Borrows` to `Reservations`). The code to process the
bitvector in the borrow checker currently just skips over all of the
active borrow bits.
But atop this commit, one *can* observe the analysis results by
looking at the graphviz output, e.g. via
```rust
#[rustc_mir(borrowck_graphviz_preflow="pre_two_phase.dot",
borrowck_graphviz_postflow="post_two_phase.dot")]
```
Includes doc for `FindPlaceUses`, as well as `Reservations` and
`ActiveBorrows` structs, which are wrappers are the `Borrows` struct
that dictate which flow analysis should be performed.
2017-12-01 11:32:51 +00:00
|
|
|
|
2022-11-27 11:15:06 +00:00
|
|
|
/// A public API provided for the Rust compiler consumers.
|
2020-12-30 17:48:40 +00:00
|
|
|
pub mod consumers;
|
|
|
|
|
2023-11-21 22:53:07 +00:00
|
|
|
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
|
2022-10-13 09:13:02 +00:00
|
|
|
|
2022-05-27 03:22:28 +00:00
|
|
|
/// Associate some local constants with the `'tcx` lifetime
|
2023-11-25 20:45:44 +00:00
|
|
|
struct TyCtxtConsts<'tcx>(PhantomData<&'tcx ()>);
|
2024-10-04 00:24:34 +00:00
|
|
|
|
2022-05-27 03:22:28 +00:00
|
|
|
impl<'tcx> TyCtxtConsts<'tcx> {
|
|
|
|
const DEREF_PROJECTION: &'tcx [PlaceElem<'tcx>; 1] = &[ProjectionElem::Deref];
|
2022-05-22 19:57:46 +00:00
|
|
|
}
|
2020-03-04 14:21:01 +00:00
|
|
|
|
2020-07-05 20:00:14 +00:00
|
|
|
pub fn provide(providers: &mut Providers) {
|
2022-05-08 13:53:19 +00:00
|
|
|
*providers = Providers { mir_borrowck, ..*providers };
|
2017-08-21 08:24:12 +00:00
|
|
|
}
|
2017-07-05 12:52:18 +00:00
|
|
|
|
2022-05-08 13:53:19 +00:00
|
|
|
fn mir_borrowck(tcx: TyCtxt<'_>, def: LocalDefId) -> &BorrowCheckResult<'_> {
|
2020-08-14 16:01:14 +00:00
|
|
|
let (input_body, promoted) = tcx.mir_promoted(def);
|
2023-02-16 09:25:11 +00:00
|
|
|
debug!("run query mir_borrowck: {}", tcx.def_path_str(def));
|
2022-08-03 11:30:13 +00:00
|
|
|
|
2024-02-01 22:42:36 +00:00
|
|
|
let input_body: &Body<'_> = &input_body.borrow();
|
|
|
|
|
|
|
|
if input_body.should_skip() || input_body.tainted_by_errors.is_some() {
|
|
|
|
debug!("Skipping borrowck because of injected body or tainted body");
|
2022-08-03 11:30:13 +00:00
|
|
|
// Let's make up a borrowck result! Fun times!
|
|
|
|
let result = BorrowCheckResult {
|
2023-03-17 20:48:34 +00:00
|
|
|
concrete_opaque_types: FxIndexMap::default(),
|
2022-08-03 11:30:13 +00:00
|
|
|
closure_requirements: None,
|
|
|
|
used_mut_upvars: SmallVec::new(),
|
2024-02-01 22:42:36 +00:00
|
|
|
tainted_by_errors: input_body.tainted_by_errors,
|
2022-08-03 11:30:13 +00:00
|
|
|
};
|
|
|
|
return tcx.arena.alloc(result);
|
|
|
|
}
|
|
|
|
|
2024-12-29 23:14:22 +00:00
|
|
|
let borrowck_result = do_mir_borrowck(tcx, input_body, &*promoted.borrow(), None).0;
|
2017-10-30 09:50:39 +00:00
|
|
|
debug!("mir_borrowck done");
|
2017-11-22 22:38:51 +00:00
|
|
|
|
2024-12-29 23:14:22 +00:00
|
|
|
tcx.arena.alloc(borrowck_result)
|
2017-10-30 09:50:39 +00:00
|
|
|
}
|
|
|
|
|
2021-06-24 11:34:17 +00:00
|
|
|
/// Perform the actual borrow checking.
|
|
|
|
///
|
2023-04-29 14:57:50 +00:00
|
|
|
/// Use `consumer_options: None` for the default behavior of returning
|
|
|
|
/// [`BorrowCheckResult`] only. Otherwise, return [`BodyWithBorrowckFacts`] according
|
|
|
|
/// to the given [`ConsumerOptions`].
|
2024-05-06 15:20:13 +00:00
|
|
|
#[instrument(skip(tcx, input_body, input_promoted), fields(id=?input_body.source.def_id()), level = "debug")]
|
2022-09-09 18:01:06 +00:00
|
|
|
fn do_mir_borrowck<'tcx>(
|
2024-05-06 15:20:13 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-13 21:48:52 +00:00
|
|
|
input_body: &Body<'tcx>,
|
2023-03-31 07:32:44 +00:00
|
|
|
input_promoted: &IndexSlice<Promoted, Body<'tcx>>,
|
2023-04-29 14:57:50 +00:00
|
|
|
consumer_options: Option<ConsumerOptions>,
|
2021-06-24 11:34:17 +00:00
|
|
|
) -> (BorrowCheckResult<'tcx>, Option<Box<BodyWithBorrowckFacts<'tcx>>>) {
|
2022-05-08 13:53:19 +00:00
|
|
|
let def = input_body.source.def_id().expect_local();
|
2024-05-06 15:20:13 +00:00
|
|
|
let infcx = BorrowckInferCtxt::new(tcx, def);
|
2024-12-05 13:59:08 +00:00
|
|
|
if let Some(e) = input_body.tainted_by_errors {
|
|
|
|
infcx.set_tainted_by_errors(e);
|
|
|
|
}
|
2017-10-30 09:50:39 +00:00
|
|
|
|
2018-05-16 15:58:54 +00:00
|
|
|
let mut local_names = IndexVec::from_elem(None, &input_body.local_decls);
|
|
|
|
for var_debug_info in &input_body.var_debug_info {
|
2020-05-30 19:02:32 +00:00
|
|
|
if let VarDebugInfoContents::Place(place) = var_debug_info.value {
|
|
|
|
if let Some(local) = place.as_local() {
|
2022-02-27 14:10:20 +00:00
|
|
|
if let Some(prev_name) = local_names[local]
|
|
|
|
&& var_debug_info.name != prev_name
|
|
|
|
{
|
|
|
|
span_bug!(
|
|
|
|
var_debug_info.source_info.span,
|
|
|
|
"local {:?} has many names (`{}` vs `{}`)",
|
|
|
|
local,
|
|
|
|
prev_name,
|
|
|
|
var_debug_info.name
|
|
|
|
);
|
2018-05-16 15:58:54 +00:00
|
|
|
}
|
2020-05-30 19:02:32 +00:00
|
|
|
local_names[local] = Some(var_debug_info.name);
|
2018-05-16 15:58:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-09 09:28:00 +00:00
|
|
|
// Replace all regions with fresh inference variables. This
|
|
|
|
// requires first making our own copy of the MIR. This copy will
|
|
|
|
// be modified (in place) to contain non-lexical lifetimes. It
|
|
|
|
// will have a lifetime tied to the inference context.
|
2021-06-24 11:34:17 +00:00
|
|
|
let mut body_owned = input_body.clone();
|
2023-03-31 07:32:44 +00:00
|
|
|
let mut promoted = input_promoted.to_owned();
|
2024-11-01 02:51:00 +00:00
|
|
|
let free_regions = nll::replace_regions_in_mir(&infcx, &mut body_owned, &mut promoted);
|
2021-06-24 11:34:17 +00:00
|
|
|
let body = &body_owned; // no further changes
|
2019-10-04 04:55:28 +00:00
|
|
|
|
2024-05-06 16:04:57 +00:00
|
|
|
// FIXME(-Znext-solver): A bit dubious that we're only registering
|
|
|
|
// predefined opaques in the typeck root.
|
|
|
|
if infcx.next_trait_solver() && !infcx.tcx.is_typeck_child(body.source.def_id()) {
|
|
|
|
infcx.register_predefined_opaques_for_next_solver(def);
|
|
|
|
}
|
|
|
|
|
2025-01-08 11:22:03 +00:00
|
|
|
let location_table = PoloniusLocationTable::new(body);
|
2017-11-17 09:34:02 +00:00
|
|
|
|
2024-09-27 16:56:51 +00:00
|
|
|
let move_data = MoveData::gather_moves(body, tcx, |_| true);
|
2017-07-05 12:52:18 +00:00
|
|
|
|
2024-11-05 00:52:38 +00:00
|
|
|
let flow_inits = MaybeInitializedPlaces::new(tcx, body, &move_data)
|
2024-10-29 21:45:46 +00:00
|
|
|
.iterate_to_fixpoint(tcx, body, Some("borrowck"))
|
2023-11-21 19:07:32 +00:00
|
|
|
.into_results_cursor(body);
|
2017-11-17 09:34:02 +00:00
|
|
|
|
2022-05-08 13:53:19 +00:00
|
|
|
let locals_are_invalidated_at_exit = tcx.hir().body_owner_kind(def).is_fn_or_closure();
|
2024-10-04 03:47:32 +00:00
|
|
|
let borrow_set = BorrowSet::build(tcx, body, locals_are_invalidated_at_exit, &move_data);
|
2018-04-06 19:57:21 +00:00
|
|
|
|
2019-12-11 22:40:49 +00:00
|
|
|
// Compute non-lexical lifetimes.
|
2019-12-25 09:50:07 +00:00
|
|
|
let nll::NllOutput {
|
|
|
|
regioncx,
|
|
|
|
opaque_type_values,
|
2021-06-24 11:34:17 +00:00
|
|
|
polonius_input,
|
2019-12-25 09:50:07 +00:00
|
|
|
polonius_output,
|
|
|
|
opt_closure_req,
|
|
|
|
nll_errors,
|
2024-12-13 15:32:52 +00:00
|
|
|
localized_outlives_constraints,
|
2019-12-25 09:50:07 +00:00
|
|
|
} = nll::compute_regions(
|
2022-11-08 15:27:01 +00:00
|
|
|
&infcx,
|
2019-12-25 09:50:07 +00:00
|
|
|
free_regions,
|
|
|
|
body,
|
|
|
|
&promoted,
|
2023-11-23 10:42:19 +00:00
|
|
|
&location_table,
|
2024-11-05 00:52:38 +00:00
|
|
|
flow_inits,
|
2024-07-24 19:58:34 +00:00
|
|
|
&move_data,
|
2019-12-25 09:50:07 +00:00
|
|
|
&borrow_set,
|
2023-04-29 14:57:50 +00:00
|
|
|
consumer_options,
|
2019-12-25 09:50:07 +00:00
|
|
|
);
|
2019-12-11 22:40:49 +00:00
|
|
|
|
2024-12-29 23:14:22 +00:00
|
|
|
// Dump MIR results into a file, if that is enabled. This lets us
|
2019-12-11 22:40:49 +00:00
|
|
|
// write unit-tests, as well as helping with debugging.
|
2024-08-28 21:17:44 +00:00
|
|
|
nll::dump_nll_mir(&infcx, body, ®ioncx, &opt_closure_req, &borrow_set);
|
2019-12-11 22:40:49 +00:00
|
|
|
|
2020-01-05 18:27:51 +00:00
|
|
|
// We also have a `#[rustc_regions]` annotation that causes us to dump
|
2019-12-11 22:40:49 +00:00
|
|
|
// information.
|
2024-12-30 01:21:48 +00:00
|
|
|
let diags_buffer = &mut BorrowckDiagnosticsBuffer::default();
|
|
|
|
nll::dump_annotation(
|
|
|
|
&infcx,
|
|
|
|
body,
|
|
|
|
®ioncx,
|
|
|
|
&opt_closure_req,
|
|
|
|
&opaque_type_values,
|
|
|
|
diags_buffer,
|
|
|
|
);
|
2018-09-14 04:55:21 +00:00
|
|
|
|
2023-10-19 21:46:28 +00:00
|
|
|
let movable_coroutine =
|
|
|
|
// The first argument is the coroutine type passed by value
|
2022-03-29 21:50:01 +00:00
|
|
|
if let Some(local) = body.local_decls.raw.get(1)
|
2023-07-11 21:35:29 +00:00
|
|
|
// Get the interior types and args which typeck computed
|
2023-12-21 01:52:10 +00:00
|
|
|
&& let ty::Coroutine(def_id, _) = *local.ty.kind()
|
2023-12-26 22:43:11 +00:00
|
|
|
&& tcx.coroutine_movability(def_id) == hir::Movability::Movable
|
2022-03-29 21:50:01 +00:00
|
|
|
{
|
|
|
|
true
|
2023-12-21 01:52:10 +00:00
|
|
|
} else {
|
|
|
|
false
|
2022-03-29 21:50:01 +00:00
|
|
|
};
|
2018-01-11 18:50:40 +00:00
|
|
|
|
2024-12-18 09:30:40 +00:00
|
|
|
// While promoteds should mostly be correct by construction, we need to check them for
|
|
|
|
// invalid moves to detect moving out of arrays:`struct S; fn main() { &([S][0]); }`.
|
|
|
|
for promoted_body in &promoted {
|
2023-09-30 15:14:07 +00:00
|
|
|
use rustc_middle::mir::visit::Visitor;
|
2024-12-18 09:30:40 +00:00
|
|
|
// This assumes that we won't use some of the fields of the `promoted_mbcx`
|
|
|
|
// when detecting and reporting move errors. While it would be nice to move
|
|
|
|
// this check out of `MirBorrowckCtxt`, actually doing so is far from trivial.
|
|
|
|
let move_data = MoveData::gather_moves(promoted_body, tcx, |_| true);
|
2023-09-30 15:14:07 +00:00
|
|
|
let mut promoted_mbcx = MirBorrowckCtxt {
|
|
|
|
infcx: &infcx,
|
|
|
|
body: promoted_body,
|
|
|
|
move_data: &move_data,
|
2025-01-08 11:22:03 +00:00
|
|
|
// no need to create a real location table for the promoted, it is not used
|
|
|
|
location_table: &location_table,
|
2023-09-30 15:14:07 +00:00
|
|
|
movable_coroutine,
|
|
|
|
fn_self_span_reported: Default::default(),
|
|
|
|
locals_are_invalidated_at_exit,
|
|
|
|
access_place_error_reported: Default::default(),
|
|
|
|
reservation_error_reported: Default::default(),
|
|
|
|
uninitialized_error_reported: Default::default(),
|
2024-10-04 03:47:32 +00:00
|
|
|
regioncx: ®ioncx,
|
2023-09-30 15:14:07 +00:00
|
|
|
used_mut: Default::default(),
|
|
|
|
used_mut_upvars: SmallVec::new(),
|
2024-10-04 03:47:32 +00:00
|
|
|
borrow_set: &borrow_set,
|
2023-11-26 13:39:57 +00:00
|
|
|
upvars: &[],
|
2023-09-30 15:14:07 +00:00
|
|
|
local_names: IndexVec::from_elem(None, &promoted_body.local_decls),
|
|
|
|
region_names: RefCell::default(),
|
|
|
|
next_region_name: RefCell::new(1),
|
|
|
|
polonius_output: None,
|
|
|
|
move_errors: Vec::new(),
|
2024-12-30 01:21:48 +00:00
|
|
|
diags_buffer,
|
2020-04-22 20:40:18 +00:00
|
|
|
};
|
2024-09-05 23:18:17 +00:00
|
|
|
struct MoveVisitor<'a, 'b, 'infcx, 'tcx> {
|
|
|
|
ctxt: &'a mut MirBorrowckCtxt<'b, 'infcx, 'tcx>,
|
2023-09-30 15:14:07 +00:00
|
|
|
}
|
|
|
|
|
2024-09-05 23:18:17 +00:00
|
|
|
impl<'tcx> Visitor<'tcx> for MoveVisitor<'_, '_, '_, 'tcx> {
|
2023-09-30 15:14:07 +00:00
|
|
|
fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) {
|
|
|
|
if let Operand::Move(place) = operand {
|
|
|
|
self.ctxt.check_movable_place(location, *place);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-12-18 09:30:40 +00:00
|
|
|
MoveVisitor { ctxt: &mut promoted_mbcx }.visit_body(promoted_body);
|
|
|
|
promoted_mbcx.report_move_errors();
|
2020-04-22 20:40:18 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 18:27:38 +00:00
|
|
|
let mut mbcx = MirBorrowckCtxt {
|
2022-11-08 15:27:01 +00:00
|
|
|
infcx: &infcx,
|
2019-11-06 17:00:46 +00:00
|
|
|
body,
|
2024-07-24 19:58:34 +00:00
|
|
|
move_data: &move_data,
|
2023-11-23 10:42:19 +00:00
|
|
|
location_table: &location_table,
|
2023-10-19 21:46:28 +00:00
|
|
|
movable_coroutine,
|
2018-09-05 22:49:58 +00:00
|
|
|
locals_are_invalidated_at_exit,
|
2020-06-11 17:48:46 +00:00
|
|
|
fn_self_span_reported: Default::default(),
|
2018-10-16 14:57:53 +00:00
|
|
|
access_place_error_reported: Default::default(),
|
|
|
|
reservation_error_reported: Default::default(),
|
|
|
|
uninitialized_error_reported: Default::default(),
|
2024-10-04 03:47:32 +00:00
|
|
|
regioncx: ®ioncx,
|
2018-10-16 14:57:53 +00:00
|
|
|
used_mut: Default::default(),
|
2018-03-03 04:42:37 +00:00
|
|
|
used_mut_upvars: SmallVec::new(),
|
2024-10-04 03:47:32 +00:00
|
|
|
borrow_set: &borrow_set,
|
2023-11-26 13:39:57 +00:00
|
|
|
upvars: tcx.closure_captures(def),
|
2018-05-16 15:58:54 +00:00
|
|
|
local_names,
|
2019-12-29 02:36:42 +00:00
|
|
|
region_names: RefCell::default(),
|
|
|
|
next_region_name: RefCell::new(1),
|
2020-01-20 23:18:13 +00:00
|
|
|
polonius_output,
|
2023-09-30 15:14:07 +00:00
|
|
|
move_errors: Vec::new(),
|
2024-12-30 01:21:48 +00:00
|
|
|
diags_buffer,
|
2017-12-06 18:27:38 +00:00
|
|
|
};
|
|
|
|
|
2019-12-11 22:40:49 +00:00
|
|
|
// Compute and report region errors, if any.
|
|
|
|
mbcx.report_region_errors(nll_errors);
|
|
|
|
|
Replace `BorrowckResults` with `Borrowck`.
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-10-31 00:02:22 +00:00
|
|
|
let mut flow_results = get_flow_results(tcx, body, &move_data, &borrow_set, ®ioncx);
|
2024-10-31 01:33:12 +00:00
|
|
|
visit_results(
|
2021-09-30 17:38:50 +00:00
|
|
|
body,
|
|
|
|
traversal::reverse_postorder(body).map(|(bb, _)| bb),
|
Replace `BorrowckResults` with `Borrowck`.
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-10-31 00:02:22 +00:00
|
|
|
&mut flow_results,
|
2020-01-20 23:18:13 +00:00
|
|
|
&mut mbcx,
|
|
|
|
);
|
2017-11-22 22:38:51 +00:00
|
|
|
|
2023-09-30 15:14:07 +00:00
|
|
|
mbcx.report_move_errors();
|
|
|
|
|
2024-12-13 15:44:39 +00:00
|
|
|
// If requested, dump polonius MIR.
|
|
|
|
polonius::dump_polonius_mir(
|
|
|
|
&infcx,
|
|
|
|
body,
|
|
|
|
®ioncx,
|
|
|
|
&borrow_set,
|
2024-12-16 11:08:00 +00:00
|
|
|
localized_outlives_constraints,
|
2024-12-13 15:44:39 +00:00
|
|
|
&opt_closure_req,
|
|
|
|
);
|
|
|
|
|
2018-04-22 06:41:44 +00:00
|
|
|
// For each non-user used mutable variable, check if it's been assigned from
|
|
|
|
// a user-declared local. If so, then put that local into the used_mut set.
|
|
|
|
// Note that this set is expected to be small - only upvars from closures
|
|
|
|
// would have a chance of erroneously adding non-user-defined mutable vars
|
|
|
|
// to the set.
|
2023-03-04 12:40:38 +00:00
|
|
|
let temporary_used_locals: FxIndexSet<Local> = mbcx
|
2018-11-07 12:40:55 +00:00
|
|
|
.used_mut
|
|
|
|
.iter()
|
2019-11-06 17:00:46 +00:00
|
|
|
.filter(|&local| !mbcx.body.local_decls[*local].is_user_variable())
|
2018-06-22 03:10:52 +00:00
|
|
|
.cloned()
|
|
|
|
.collect();
|
2018-11-07 12:40:55 +00:00
|
|
|
// For the remaining unused locals that are marked as mutable, we avoid linting any that
|
|
|
|
// were never initialized. These locals may have been removed as unreachable code; or will be
|
|
|
|
// linted as unused variables.
|
2019-11-06 17:00:46 +00:00
|
|
|
let unused_mut_locals =
|
2018-11-07 12:40:55 +00:00
|
|
|
mbcx.body.mut_vars_iter().filter(|local| !mbcx.used_mut.contains(local)).collect();
|
|
|
|
mbcx.gather_used_muts(temporary_used_locals, unused_mut_locals);
|
2018-04-22 06:41:44 +00:00
|
|
|
|
2018-03-03 04:42:37 +00:00
|
|
|
debug!("mbcx.used_mut: {:?}", mbcx.used_mut);
|
2024-12-18 17:27:26 +00:00
|
|
|
mbcx.lint_unused_mut();
|
2022-02-08 06:37:32 +00:00
|
|
|
let tainted_by_errors = mbcx.emit_errors();
|
2018-07-18 21:10:08 +00:00
|
|
|
|
2018-07-03 15:38:09 +00:00
|
|
|
let result = BorrowCheckResult {
|
2019-12-25 09:50:07 +00:00
|
|
|
concrete_opaque_types: opaque_type_values,
|
2018-03-03 04:42:37 +00:00
|
|
|
closure_requirements: opt_closure_req,
|
|
|
|
used_mut_upvars: mbcx.used_mut_upvars,
|
2022-02-08 06:37:32 +00:00
|
|
|
tainted_by_errors,
|
2018-07-03 15:38:09 +00:00
|
|
|
};
|
|
|
|
|
2023-04-29 14:57:50 +00:00
|
|
|
let body_with_facts = if consumer_options.is_some() {
|
|
|
|
let output_facts = mbcx.polonius_output;
|
2021-08-05 03:36:38 +00:00
|
|
|
Some(Box::new(BodyWithBorrowckFacts {
|
2021-06-24 11:34:17 +00:00
|
|
|
body: body_owned,
|
2023-05-17 14:07:01 +00:00
|
|
|
promoted,
|
2023-04-29 14:57:50 +00:00
|
|
|
borrow_set,
|
|
|
|
region_inference_context: regioncx,
|
2023-11-23 10:42:19 +00:00
|
|
|
location_table: polonius_input.as_ref().map(|_| location_table),
|
2023-04-29 14:57:50 +00:00
|
|
|
input_facts: polonius_input,
|
2021-06-24 11:34:17 +00:00
|
|
|
output_facts,
|
2021-08-05 03:36:38 +00:00
|
|
|
}))
|
2021-06-24 11:34:17 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2018-07-03 15:38:09 +00:00
|
|
|
debug!("do_mir_borrowck: result = {:#?}", result);
|
|
|
|
|
2021-06-24 11:34:17 +00:00
|
|
|
(result, body_with_facts)
|
|
|
|
}
|
|
|
|
|
Replace `BorrowckResults` with `Borrowck`.
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-10-31 00:02:22 +00:00
|
|
|
fn get_flow_results<'a, 'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
body: &'a Body<'tcx>,
|
|
|
|
move_data: &'a MoveData<'tcx>,
|
|
|
|
borrow_set: &'a BorrowSet<'tcx>,
|
|
|
|
regioncx: &RegionInferenceContext<'tcx>,
|
|
|
|
) -> Results<'tcx, Borrowck<'a, 'tcx>> {
|
|
|
|
// We compute these three analyses individually, but them combine them into
|
|
|
|
// a single results so that `mbcx` can visit them all together.
|
|
|
|
let borrows = Borrows::new(tcx, body, regioncx, borrow_set).iterate_to_fixpoint(
|
|
|
|
tcx,
|
|
|
|
body,
|
|
|
|
Some("borrowck"),
|
|
|
|
);
|
|
|
|
let uninits = MaybeUninitializedPlaces::new(tcx, body, move_data).iterate_to_fixpoint(
|
|
|
|
tcx,
|
|
|
|
body,
|
|
|
|
Some("borrowck"),
|
|
|
|
);
|
|
|
|
let ever_inits = EverInitializedPlaces::new(body, move_data).iterate_to_fixpoint(
|
|
|
|
tcx,
|
|
|
|
body,
|
|
|
|
Some("borrowck"),
|
|
|
|
);
|
|
|
|
|
|
|
|
let analysis = Borrowck {
|
|
|
|
borrows: borrows.analysis,
|
|
|
|
uninits: uninits.analysis,
|
|
|
|
ever_inits: ever_inits.analysis,
|
|
|
|
};
|
|
|
|
|
2024-11-26 03:21:17 +00:00
|
|
|
assert_eq!(borrows.entry_states.len(), uninits.entry_states.len());
|
|
|
|
assert_eq!(borrows.entry_states.len(), ever_inits.entry_states.len());
|
|
|
|
let entry_states: EntryStates<'_, Borrowck<'_, '_>> =
|
|
|
|
itertools::izip!(borrows.entry_states, uninits.entry_states, ever_inits.entry_states)
|
Replace `BorrowckResults` with `Borrowck`.
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-10-31 00:02:22 +00:00
|
|
|
.map(|(borrows, uninits, ever_inits)| BorrowckDomain { borrows, uninits, ever_inits })
|
|
|
|
.collect();
|
|
|
|
|
2024-11-26 03:21:17 +00:00
|
|
|
Results { analysis, entry_states }
|
Replace `BorrowckResults` with `Borrowck`.
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-10-31 00:02:22 +00:00
|
|
|
}
|
|
|
|
|
2024-10-04 00:04:34 +00:00
|
|
|
pub(crate) struct BorrowckInferCtxt<'tcx> {
|
2024-05-06 15:20:13 +00:00
|
|
|
pub(crate) infcx: InferCtxt<'tcx>,
|
2023-03-04 12:40:38 +00:00
|
|
|
pub(crate) reg_var_to_origin: RefCell<FxIndexMap<ty::RegionVid, RegionCtxt>>,
|
2024-11-01 02:51:00 +00:00
|
|
|
pub(crate) param_env: ParamEnv<'tcx>,
|
2022-11-07 17:24:07 +00:00
|
|
|
}
|
|
|
|
|
2024-05-06 15:20:13 +00:00
|
|
|
impl<'tcx> BorrowckInferCtxt<'tcx> {
|
|
|
|
pub(crate) fn new(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Self {
|
2024-10-17 22:28:43 +00:00
|
|
|
let infcx = tcx.infer_ctxt().build(TypingMode::analysis_in_body(tcx, def_id));
|
2024-11-01 02:51:00 +00:00
|
|
|
let param_env = tcx.param_env(def_id);
|
|
|
|
BorrowckInferCtxt { infcx, reg_var_to_origin: RefCell::new(Default::default()), param_env }
|
2022-11-08 15:27:01 +00:00
|
|
|
}
|
|
|
|
|
2023-02-19 21:44:01 +00:00
|
|
|
pub(crate) fn next_region_var<F>(
|
2022-11-08 15:27:01 +00:00
|
|
|
&self,
|
|
|
|
origin: RegionVariableOrigin,
|
2023-02-19 21:44:01 +00:00
|
|
|
get_ctxt_fn: F,
|
|
|
|
) -> ty::Region<'tcx>
|
|
|
|
where
|
|
|
|
F: Fn() -> RegionCtxt,
|
|
|
|
{
|
2022-11-08 15:27:01 +00:00
|
|
|
let next_region = self.infcx.next_region_var(origin);
|
2023-04-12 05:36:03 +00:00
|
|
|
let vid = next_region.as_var();
|
2022-11-08 15:27:01 +00:00
|
|
|
|
2023-05-03 18:58:49 +00:00
|
|
|
if cfg!(debug_assertions) {
|
2023-02-19 21:44:01 +00:00
|
|
|
debug!("inserting vid {:?} with origin {:?} into var_to_origin", vid, origin);
|
|
|
|
let ctxt = get_ctxt_fn();
|
|
|
|
let mut var_to_origin = self.reg_var_to_origin.borrow_mut();
|
2023-05-03 18:58:49 +00:00
|
|
|
assert_eq!(var_to_origin.insert(vid, ctxt), None);
|
2023-02-19 21:44:01 +00:00
|
|
|
}
|
2022-11-08 15:27:01 +00:00
|
|
|
|
|
|
|
next_region
|
|
|
|
}
|
|
|
|
|
2023-02-19 21:44:01 +00:00
|
|
|
#[instrument(skip(self, get_ctxt_fn), level = "debug")]
|
|
|
|
pub(crate) fn next_nll_region_var<F>(
|
2022-11-08 15:27:01 +00:00
|
|
|
&self,
|
|
|
|
origin: NllRegionVariableOrigin,
|
2023-02-19 21:44:01 +00:00
|
|
|
get_ctxt_fn: F,
|
|
|
|
) -> ty::Region<'tcx>
|
|
|
|
where
|
|
|
|
F: Fn() -> RegionCtxt,
|
|
|
|
{
|
2023-04-15 17:02:26 +00:00
|
|
|
let next_region = self.infcx.next_nll_region_var(origin);
|
2023-04-12 05:36:03 +00:00
|
|
|
let vid = next_region.as_var();
|
2022-11-08 15:27:01 +00:00
|
|
|
|
2023-05-03 18:58:49 +00:00
|
|
|
if cfg!(debug_assertions) {
|
2023-02-19 21:44:01 +00:00
|
|
|
debug!("inserting vid {:?} with origin {:?} into var_to_origin", vid, origin);
|
|
|
|
let ctxt = get_ctxt_fn();
|
|
|
|
let mut var_to_origin = self.reg_var_to_origin.borrow_mut();
|
2023-05-03 18:58:49 +00:00
|
|
|
assert_eq!(var_to_origin.insert(vid, ctxt), None);
|
2023-02-19 21:44:01 +00:00
|
|
|
}
|
2022-11-08 15:27:01 +00:00
|
|
|
|
|
|
|
next_region
|
2022-11-07 17:24:07 +00:00
|
|
|
}
|
2024-05-06 16:04:57 +00:00
|
|
|
|
|
|
|
/// With the new solver we prepopulate the opaque type storage during
|
|
|
|
/// MIR borrowck with the hidden types from HIR typeck. This is necessary
|
|
|
|
/// to avoid ambiguities as earlier goals can rely on the hidden type
|
|
|
|
/// of an opaque which is only constrained by a later goal.
|
|
|
|
fn register_predefined_opaques_for_next_solver(&self, def_id: LocalDefId) {
|
|
|
|
let tcx = self.tcx;
|
|
|
|
// OK to use the identity arguments for each opaque type key, since
|
|
|
|
// we remap opaques from HIR typeck back to their definition params.
|
|
|
|
for data in tcx.typeck(def_id).concrete_opaque_types.iter().map(|(k, v)| (*k, *v)) {
|
|
|
|
// HIR typeck did not infer the regions of the opaque, so we instantiate
|
|
|
|
// them with fresh inference variables.
|
2024-11-27 17:37:39 +00:00
|
|
|
let (key, hidden_ty) = fold_regions(tcx, data, |_, _| {
|
2024-05-06 16:04:57 +00:00
|
|
|
self.next_nll_region_var_in_universe(
|
|
|
|
NllRegionVariableOrigin::Existential { from_forall: false },
|
|
|
|
ty::UniverseIndex::ROOT,
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
self.inject_new_hidden_type_unchecked(key, hidden_ty);
|
|
|
|
}
|
|
|
|
}
|
2022-11-07 17:24:07 +00:00
|
|
|
}
|
|
|
|
|
2024-05-06 15:20:13 +00:00
|
|
|
impl<'tcx> Deref for BorrowckInferCtxt<'tcx> {
|
2022-11-07 17:24:07 +00:00
|
|
|
type Target = InferCtxt<'tcx>;
|
|
|
|
|
2024-05-06 15:20:13 +00:00
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.infcx
|
2022-11-07 17:24:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-05 23:18:17 +00:00
|
|
|
struct MirBorrowckCtxt<'a, 'infcx, 'tcx> {
|
2024-06-27 09:07:57 +00:00
|
|
|
infcx: &'infcx BorrowckInferCtxt<'tcx>,
|
2024-09-05 23:18:17 +00:00
|
|
|
body: &'a Body<'tcx>,
|
2024-06-20 13:10:11 +00:00
|
|
|
move_data: &'a MoveData<'tcx>,
|
2018-06-26 11:12:51 +00:00
|
|
|
|
|
|
|
/// Map from MIR `Location` to `LocationIndex`; created
|
|
|
|
/// when MIR borrowck begins.
|
2025-01-08 11:22:03 +00:00
|
|
|
location_table: &'a PoloniusLocationTable,
|
2018-06-26 11:12:51 +00:00
|
|
|
|
2023-10-19 21:46:28 +00:00
|
|
|
movable_coroutine: bool,
|
2017-12-03 14:08:28 +00:00
|
|
|
/// This keeps track of whether local variables are free-ed when the function
|
2017-12-05 22:51:47 +00:00
|
|
|
/// exits even without a `StorageDead`, which appears to be the case for
|
|
|
|
/// constants.
|
|
|
|
///
|
|
|
|
/// I'm not sure this is the right approach - @eddyb could you try and
|
|
|
|
/// figure this out?
|
2017-12-03 14:08:28 +00:00
|
|
|
locals_are_invalidated_at_exit: bool,
|
2018-01-20 02:15:57 +00:00
|
|
|
/// This field keeps track of when borrow errors are reported in the access_place function
|
|
|
|
/// so that there is no duplicate reporting. This field cannot also be used for the conflicting
|
|
|
|
/// borrow errors that is handled by the `reservation_error_reported` field as the inclusion
|
|
|
|
/// of the `Span` type (while required to mute some errors) stops the muting of the reservation
|
|
|
|
/// errors.
|
2023-03-04 12:40:38 +00:00
|
|
|
access_place_error_reported: FxIndexSet<(Place<'tcx>, Span)>,
|
2017-12-07 16:45:13 +00:00
|
|
|
/// This field keeps track of when borrow conflict errors are reported
|
|
|
|
/// for reservations, so that we don't report seemingly duplicate
|
2019-02-08 13:53:55 +00:00
|
|
|
/// errors for corresponding activations.
|
|
|
|
//
|
|
|
|
// FIXME: ideally this would be a set of `BorrowIndex`, not `Place`s,
|
|
|
|
// but it is currently inconvenient to track down the `BorrowIndex`
|
|
|
|
// at the time we detect and report a reservation error.
|
2023-03-04 12:40:38 +00:00
|
|
|
reservation_error_reported: FxIndexSet<Place<'tcx>>,
|
2020-06-11 17:48:46 +00:00
|
|
|
/// This fields keeps track of the `Span`s that we have
|
|
|
|
/// used to report extra information for `FnSelfUse`, to avoid
|
|
|
|
/// unnecessarily verbose errors.
|
2023-03-04 12:40:38 +00:00
|
|
|
fn_self_span_reported: FxIndexSet<Span>,
|
2018-09-06 13:48:33 +00:00
|
|
|
/// This field keeps track of errors reported in the checking of uninitialized variables,
|
2018-07-06 20:25:40 +00:00
|
|
|
/// so that we don't report seemingly duplicate errors.
|
2024-04-28 06:09:43 +00:00
|
|
|
uninitialized_error_reported: FxIndexSet<Local>,
|
2018-02-28 09:09:08 +00:00
|
|
|
/// This field keeps track of all the local variables that are declared mut and are mutated.
|
|
|
|
/// Used for the warning issued by an unused mutable local variable.
|
2023-03-04 12:40:38 +00:00
|
|
|
used_mut: FxIndexSet<Local>,
|
2018-03-03 04:42:37 +00:00
|
|
|
/// If the function we're checking is a closure, then we'll need to report back the list of
|
|
|
|
/// mutable upvars that have been used. This field keeps track of them.
|
2023-03-28 19:32:57 +00:00
|
|
|
used_mut_upvars: SmallVec<[FieldIdx; 8]>,
|
2019-12-29 02:02:20 +00:00
|
|
|
/// Region inference context. This contains the results from region inference and lets us e.g.
|
2017-12-07 16:21:29 +00:00
|
|
|
/// find out which CFG points are contained in each borrow region.
|
2024-10-04 03:47:32 +00:00
|
|
|
regioncx: &'a RegionInferenceContext<'tcx>,
|
2018-04-07 12:01:21 +00:00
|
|
|
|
|
|
|
/// The set of borrows extracted from the MIR
|
2024-10-04 03:47:32 +00:00
|
|
|
borrow_set: &'a BorrowSet<'tcx>,
|
2018-04-07 12:01:21 +00:00
|
|
|
|
2018-05-16 12:38:32 +00:00
|
|
|
/// Information about upvars not necessarily preserved in types or MIR
|
2023-11-26 13:39:57 +00:00
|
|
|
upvars: &'tcx [&'tcx ty::CapturedPlace<'tcx>],
|
2018-05-16 15:58:54 +00:00
|
|
|
|
|
|
|
/// Names of local (user) variables (extracted from `var_debug_info`).
|
2020-04-19 11:00:18 +00:00
|
|
|
local_names: IndexVec<Local, Option<Symbol>>,
|
2019-12-29 02:36:42 +00:00
|
|
|
|
|
|
|
/// Record the region names generated for each region in the given
|
|
|
|
/// MIR def so that we can reuse them later in help/error messages.
|
2023-03-04 12:40:38 +00:00
|
|
|
region_names: RefCell<FxIndexMap<RegionVid, RegionName>>,
|
2019-12-29 02:36:42 +00:00
|
|
|
|
|
|
|
/// The counter for generating new region names.
|
|
|
|
next_region_name: RefCell<usize>,
|
2020-01-20 23:18:13 +00:00
|
|
|
|
|
|
|
/// Results of Polonius analysis.
|
2024-10-04 04:02:53 +00:00
|
|
|
polonius_output: Option<Box<PoloniusOutput>>,
|
2022-02-08 06:37:32 +00:00
|
|
|
|
2024-12-30 01:21:48 +00:00
|
|
|
diags_buffer: &'a mut BorrowckDiagnosticsBuffer<'infcx, 'tcx>,
|
2023-09-30 15:14:07 +00:00
|
|
|
move_errors: Vec<MoveError<'tcx>>,
|
2017-07-05 12:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that:
|
|
|
|
// 1. assignments are always made to mutable locations (FIXME: does that still really go here?)
|
|
|
|
// 2. loans made in overlapping scopes do not conflict
|
|
|
|
// 3. assignments do not affect things loaned out as immutable
|
|
|
|
// 4. moves do not affect things loaned out in any way
|
2024-10-31 01:33:12 +00:00
|
|
|
impl<'a, 'tcx> ResultsVisitor<'a, 'tcx, Borrowck<'a, 'tcx>> for MirBorrowckCtxt<'a, '_, 'tcx> {
|
2024-11-26 04:31:02 +00:00
|
|
|
fn visit_after_early_statement_effect(
|
2017-11-17 09:47:02 +00:00
|
|
|
&mut self,
|
2024-10-31 01:33:12 +00:00
|
|
|
_results: &mut Results<'tcx, Borrowck<'a, 'tcx>>,
|
2024-11-26 03:02:10 +00:00
|
|
|
state: &BorrowckDomain,
|
2024-09-05 23:18:17 +00:00
|
|
|
stmt: &'a Statement<'tcx>,
|
2020-01-20 23:18:13 +00:00
|
|
|
location: Location,
|
2017-11-17 09:47:02 +00:00
|
|
|
) {
|
2024-09-13 06:27:24 +00:00
|
|
|
debug!("MirBorrowckCtxt::process_statement({:?}, {:?}): {:?}", location, stmt, state);
|
2017-07-05 12:52:18 +00:00
|
|
|
let span = stmt.source_info.span;
|
2017-12-07 16:45:13 +00:00
|
|
|
|
2024-09-13 06:27:24 +00:00
|
|
|
self.check_activations(location, span, state);
|
2017-12-07 16:45:13 +00:00
|
|
|
|
2020-03-30 20:49:33 +00:00
|
|
|
match &stmt.kind {
|
2022-12-02 19:07:57 +00:00
|
|
|
StatementKind::Assign(box (lhs, rhs)) => {
|
2024-09-13 06:27:24 +00:00
|
|
|
self.consume_rvalue(location, (rhs, span), state);
|
2018-02-05 22:31:56 +00:00
|
|
|
|
2024-09-13 06:27:24 +00:00
|
|
|
self.mutate_place(location, (*lhs, span), Shallow(None), state);
|
2017-07-05 12:52:18 +00:00
|
|
|
}
|
2022-12-02 19:07:57 +00:00
|
|
|
StatementKind::FakeRead(box (_, place)) => {
|
2018-09-13 20:36:15 +00:00
|
|
|
// Read for match doesn't access any memory and is used to
|
|
|
|
// assert that a place is safe and live. So we don't have to
|
|
|
|
// do any checks here.
|
|
|
|
//
|
|
|
|
// FIXME: Remove check that the place is initialized. This is
|
|
|
|
// needed for now because matches don't have never patterns yet.
|
|
|
|
// So this is the only place we prevent
|
|
|
|
// let x: !;
|
|
|
|
// match x {};
|
|
|
|
// from compiling.
|
|
|
|
self.check_if_path_or_subpath_is_moved(
|
2019-05-01 21:03:17 +00:00
|
|
|
location,
|
2018-09-13 20:36:15 +00:00
|
|
|
InitializationRequiringAction::Use,
|
2019-07-21 20:38:30 +00:00
|
|
|
(place.as_ref(), span),
|
2024-09-13 06:27:24 +00:00
|
|
|
state,
|
2018-06-22 03:10:52 +00:00
|
|
|
);
|
2018-05-04 10:04:33 +00:00
|
|
|
}
|
2022-12-02 19:07:57 +00:00
|
|
|
StatementKind::Intrinsic(box kind) => match kind {
|
2024-10-04 00:24:34 +00:00
|
|
|
NonDivergingIntrinsic::Assume(op) => {
|
|
|
|
self.consume_operand(location, (op, span), state);
|
|
|
|
}
|
2022-07-12 10:05:00 +00:00
|
|
|
NonDivergingIntrinsic::CopyNonOverlapping(..) => span_bug!(
|
2021-02-26 16:42:51 +00:00
|
|
|
span,
|
|
|
|
"Unexpected CopyNonOverlapping, should only appear after lower_intrinsics",
|
|
|
|
)
|
2021-01-23 00:23:13 +00:00
|
|
|
}
|
2022-06-30 08:16:05 +00:00
|
|
|
// Only relevant for mir typeck
|
|
|
|
StatementKind::AscribeUserType(..)
|
2022-11-24 19:09:27 +00:00
|
|
|
// Only relevant for liveness and unsafeck
|
2022-09-06 16:41:01 +00:00
|
|
|
| StatementKind::PlaceMention(..)
|
2022-06-30 08:16:05 +00:00
|
|
|
// Doesn't have any language semantics
|
2020-08-15 11:42:13 +00:00
|
|
|
| StatementKind::Coverage(..)
|
2023-01-04 04:29:27 +00:00
|
|
|
// These do not actually affect borrowck
|
|
|
|
| StatementKind::ConstEvalCounter
|
2022-06-30 08:16:05 +00:00
|
|
|
| StatementKind::StorageLive(..) => {}
|
2024-12-04 18:58:59 +00:00
|
|
|
// This does not affect borrowck
|
|
|
|
StatementKind::BackwardIncompatibleDropHint { place, reason: BackwardIncompatibleDropReason::Edition2024 } => {
|
|
|
|
self.check_backward_incompatible_drop(location, (**place, span), state);
|
|
|
|
}
|
2017-09-04 05:01:46 +00:00
|
|
|
StatementKind::StorageDead(local) => {
|
2017-11-17 09:47:02 +00:00
|
|
|
self.access_place(
|
2019-05-01 21:03:17 +00:00
|
|
|
location,
|
2020-03-30 20:49:33 +00:00
|
|
|
(Place::from(*local), span),
|
2018-09-23 15:07:45 +00:00
|
|
|
(Shallow(None), Write(WriteKind::StorageDeadOrDrop)),
|
2017-11-16 16:44:24 +00:00
|
|
|
LocalMutationIsAllowed::Yes,
|
2024-09-13 06:27:24 +00:00
|
|
|
state,
|
2017-11-17 09:47:02 +00:00
|
|
|
);
|
2017-08-14 12:42:17 +00:00
|
|
|
}
|
2023-01-04 04:29:27 +00:00
|
|
|
StatementKind::Nop
|
2022-06-30 08:16:05 +00:00
|
|
|
| StatementKind::Retag { .. }
|
|
|
|
| StatementKind::Deinit(..)
|
|
|
|
| StatementKind::SetDiscriminant { .. } => {
|
2022-04-05 21:14:59 +00:00
|
|
|
bug!("Statement not allowed in this MIR phase")
|
|
|
|
}
|
2017-07-05 12:52:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-26 04:31:02 +00:00
|
|
|
fn visit_after_early_terminator_effect(
|
2017-11-17 09:47:02 +00:00
|
|
|
&mut self,
|
2024-10-31 01:33:12 +00:00
|
|
|
_results: &mut Results<'tcx, Borrowck<'a, 'tcx>>,
|
2024-11-26 03:02:10 +00:00
|
|
|
state: &BorrowckDomain,
|
2024-09-05 23:18:17 +00:00
|
|
|
term: &'a Terminator<'tcx>,
|
2020-01-20 23:18:13 +00:00
|
|
|
loc: Location,
|
2017-11-17 09:47:02 +00:00
|
|
|
) {
|
2024-09-13 06:27:24 +00:00
|
|
|
debug!("MirBorrowckCtxt::process_terminator({:?}, {:?}): {:?}", loc, term, state);
|
2017-07-05 12:52:18 +00:00
|
|
|
let span = term.source_info.span;
|
2017-12-07 16:45:13 +00:00
|
|
|
|
2024-09-13 06:27:24 +00:00
|
|
|
self.check_activations(loc, span, state);
|
2017-12-07 16:45:13 +00:00
|
|
|
|
2022-12-02 19:07:57 +00:00
|
|
|
match &term.kind {
|
2022-12-04 00:03:27 +00:00
|
|
|
TerminatorKind::SwitchInt { discr, targets: _ } => {
|
2024-09-13 06:27:24 +00:00
|
|
|
self.consume_operand(loc, (discr, span), state);
|
2017-07-05 12:52:18 +00:00
|
|
|
}
|
2023-05-25 17:30:23 +00:00
|
|
|
TerminatorKind::Drop { place, target: _, unwind: _, replace } => {
|
Special-case `Box` in `rustc_mir::borrow_check`.
This should address issue 45696.
Since we know dropping a box will not access any `&mut` or `&`
references, it is safe to model its destructor as only touching the
contents *owned* by the box.
Note: At some point we may want to generalize this machinery to other
reference and collection types that are "pure" in the same sense as
box. If we add a `&move` reference type, it would probably also fall
into this branch of code. But for the short term, we will be
conservative and restrict this change to `Box<T>` alone.
The code works by recursively descending a deref of the `Box`. We
prevent `visit_terminator_drop` infinite-loop (which can arise in a
very obscure scenario) via a linked-list of seen types.
Note: A similar style stack-only linked-list definition can be found
in `rustc_mir::borrow_check::places_conflict`. It might be good at
some point in the future to unify the two types and put the resulting
definition into `librustc_data_structures/`.
----
One final note: Review feedback led to significant simplification of
logic here.
During review, eddyb RalfJung and I uncovered the heart of why I
needed a so-called "step 2" aka the Shallow Write to the Deref of the
box. It was because the `visit_terminator_drop`, in its base case,
will not emit any write at all (shallow or deep) to a place unless
that place has a need_drop.
So I was encoding a Shallow Write by hand for a `Box<T>`, as a
separate step from recursively descending through `*a_box` (which was
at the time known as "step 1"; it is now the *only* step, apart from
the change to the base case for `visit_terminator_drop` that this
commit now has encoded).
eddyb aruged that *something* should be emitting some sort of write in
the base case here (even a shallow one), of the dropped place, since
by analogy we also emit a write when you *move* a place. That led
to the revision here in this commit.
* (Its possible that this desired write should be attached in some
manner to StorageDead instead of Drop. But in this PR, I tried to
leave the StorageDead logic alone and focus my attention solely on
how Drop(x) is modelled in MIR-borrowck.)
2018-07-26 20:29:50 +00:00
|
|
|
debug!(
|
|
|
|
"visit_terminator_drop \
|
2020-10-21 21:52:41 +00:00
|
|
|
loc: {:?} term: {:?} place: {:?} span: {:?}",
|
|
|
|
loc, term, place, span
|
Special-case `Box` in `rustc_mir::borrow_check`.
This should address issue 45696.
Since we know dropping a box will not access any `&mut` or `&`
references, it is safe to model its destructor as only touching the
contents *owned* by the box.
Note: At some point we may want to generalize this machinery to other
reference and collection types that are "pure" in the same sense as
box. If we add a `&move` reference type, it would probably also fall
into this branch of code. But for the short term, we will be
conservative and restrict this change to `Box<T>` alone.
The code works by recursively descending a deref of the `Box`. We
prevent `visit_terminator_drop` infinite-loop (which can arise in a
very obscure scenario) via a linked-list of seen types.
Note: A similar style stack-only linked-list definition can be found
in `rustc_mir::borrow_check::places_conflict`. It might be good at
some point in the future to unify the two types and put the resulting
definition into `librustc_data_structures/`.
----
One final note: Review feedback led to significant simplification of
logic here.
During review, eddyb RalfJung and I uncovered the heart of why I
needed a so-called "step 2" aka the Shallow Write to the Deref of the
box. It was because the `visit_terminator_drop`, in its base case,
will not emit any write at all (shallow or deep) to a place unless
that place has a need_drop.
So I was encoding a Shallow Write by hand for a `Box<T>`, as a
separate step from recursively descending through `*a_box` (which was
at the time known as "step 1"; it is now the *only* step, apart from
the change to the base case for `visit_terminator_drop` that this
commit now has encoded).
eddyb aruged that *something* should be emitting some sort of write in
the base case here (even a shallow one), of the dropped place, since
by analogy we also emit a write when you *move* a place. That led
to the revision here in this commit.
* (Its possible that this desired write should be attached in some
manner to StorageDead instead of Drop. But in this PR, I tried to
leave the StorageDead logic alone and focus my attention solely on
how Drop(x) is modelled in MIR-borrowck.)
2018-07-26 20:29:50 +00:00
|
|
|
);
|
|
|
|
|
2023-05-25 17:30:23 +00:00
|
|
|
let write_kind =
|
|
|
|
if *replace { WriteKind::Replace } else { WriteKind::StorageDeadOrDrop };
|
2018-09-23 15:07:45 +00:00
|
|
|
self.access_place(
|
2019-05-01 21:03:17 +00:00
|
|
|
loc,
|
2022-12-02 19:07:57 +00:00
|
|
|
(*place, span),
|
2023-05-25 17:30:23 +00:00
|
|
|
(AccessDepth::Drop, Write(write_kind)),
|
2018-09-23 15:07:45 +00:00
|
|
|
LocalMutationIsAllowed::Yes,
|
2024-09-13 06:27:24 +00:00
|
|
|
state,
|
2018-09-23 15:07:45 +00:00
|
|
|
);
|
2017-07-05 12:52:18 +00:00
|
|
|
}
|
2017-11-17 09:47:02 +00:00
|
|
|
TerminatorKind::Call {
|
2022-12-02 19:07:57 +00:00
|
|
|
func,
|
|
|
|
args,
|
2022-04-16 13:27:54 +00:00
|
|
|
destination,
|
|
|
|
target: _,
|
2022-10-08 22:47:59 +00:00
|
|
|
unwind: _,
|
2023-06-18 05:24:38 +00:00
|
|
|
call_source: _,
|
2020-06-09 19:34:23 +00:00
|
|
|
fn_span: _,
|
2017-11-17 09:47:02 +00:00
|
|
|
} => {
|
2024-09-13 06:27:24 +00:00
|
|
|
self.consume_operand(loc, (func, span), state);
|
2017-07-05 12:52:18 +00:00
|
|
|
for arg in args {
|
2024-09-13 06:27:24 +00:00
|
|
|
self.consume_operand(loc, (&arg.node, arg.span), state);
|
2017-07-05 12:52:18 +00:00
|
|
|
}
|
2024-09-13 06:27:24 +00:00
|
|
|
self.mutate_place(loc, (*destination, span), Deep, state);
|
2017-07-05 12:52:18 +00:00
|
|
|
}
|
2024-02-15 19:54:37 +00:00
|
|
|
TerminatorKind::TailCall { func, args, fn_span: _ } => {
|
2024-09-13 06:27:24 +00:00
|
|
|
self.consume_operand(loc, (func, span), state);
|
2024-02-15 19:54:37 +00:00
|
|
|
for arg in args {
|
2024-09-13 06:27:24 +00:00
|
|
|
self.consume_operand(loc, (&arg.node, arg.span), state);
|
2024-02-15 19:54:37 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-08 22:47:59 +00:00
|
|
|
TerminatorKind::Assert { cond, expected: _, msg, target: _, unwind: _ } => {
|
2024-09-13 06:27:24 +00:00
|
|
|
self.consume_operand(loc, (cond, span), state);
|
2023-05-01 22:30:54 +00:00
|
|
|
if let AssertKind::BoundsCheck { len, index } = &**msg {
|
2024-09-13 06:27:24 +00:00
|
|
|
self.consume_operand(loc, (len, span), state);
|
|
|
|
self.consume_operand(loc, (index, span), state);
|
2017-07-05 12:52:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-02 19:07:57 +00:00
|
|
|
TerminatorKind::Yield { value, resume: _, resume_arg, drop: _ } => {
|
2024-09-13 06:27:24 +00:00
|
|
|
self.consume_operand(loc, (value, span), state);
|
|
|
|
self.mutate_place(loc, (*resume_arg, span), Deep, state);
|
2020-01-20 23:18:13 +00:00
|
|
|
}
|
2018-01-11 18:50:40 +00:00
|
|
|
|
2020-05-26 19:07:59 +00:00
|
|
|
TerminatorKind::InlineAsm {
|
2024-09-05 17:45:40 +00:00
|
|
|
asm_macro: _,
|
2020-05-26 19:07:59 +00:00
|
|
|
template: _,
|
2022-12-02 19:07:57 +00:00
|
|
|
operands,
|
2020-05-26 19:07:59 +00:00
|
|
|
options: _,
|
|
|
|
line_spans: _,
|
2023-12-26 15:28:42 +00:00
|
|
|
targets: _,
|
2022-10-08 22:47:59 +00:00
|
|
|
unwind: _,
|
2020-05-26 19:07:59 +00:00
|
|
|
} => {
|
2020-02-14 18:17:50 +00:00
|
|
|
for op in operands {
|
2022-12-02 19:07:57 +00:00
|
|
|
match op {
|
|
|
|
InlineAsmOperand::In { reg: _, value } => {
|
2024-09-13 06:27:24 +00:00
|
|
|
self.consume_operand(loc, (value, span), state);
|
2020-02-14 18:17:50 +00:00
|
|
|
}
|
|
|
|
InlineAsmOperand::Out { reg: _, late: _, place, .. } => {
|
|
|
|
if let Some(place) = place {
|
2024-09-13 06:27:24 +00:00
|
|
|
self.mutate_place(loc, (*place, span), Shallow(None), state);
|
2020-02-14 18:17:50 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-02 19:07:57 +00:00
|
|
|
InlineAsmOperand::InOut { reg: _, late: _, in_value, out_place } => {
|
2024-09-13 06:27:24 +00:00
|
|
|
self.consume_operand(loc, (in_value, span), state);
|
2022-12-02 19:07:57 +00:00
|
|
|
if let &Some(out_place) = out_place {
|
2024-09-13 06:27:24 +00:00
|
|
|
self.mutate_place(loc, (out_place, span), Shallow(None), state);
|
2020-02-14 18:17:50 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-06 04:50:55 +00:00
|
|
|
InlineAsmOperand::Const { value: _ }
|
|
|
|
| InlineAsmOperand::SymFn { value: _ }
|
2023-12-26 16:07:35 +00:00
|
|
|
| InlineAsmOperand::SymStatic { def_id: _ }
|
|
|
|
| InlineAsmOperand::Label { target_index: _ } => {}
|
2020-02-14 18:17:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-20 23:18:13 +00:00
|
|
|
TerminatorKind::Goto { target: _ }
|
2023-08-21 07:57:10 +00:00
|
|
|
| TerminatorKind::UnwindTerminate(_)
|
2020-01-20 23:18:13 +00:00
|
|
|
| TerminatorKind::Unreachable
|
2023-08-19 11:10:25 +00:00
|
|
|
| TerminatorKind::UnwindResume
|
2020-01-20 23:18:13 +00:00
|
|
|
| TerminatorKind::Return
|
2023-10-19 16:06:43 +00:00
|
|
|
| TerminatorKind::CoroutineDrop
|
2020-06-02 07:15:24 +00:00
|
|
|
| TerminatorKind::FalseEdge { real_target: _, imaginary_target: _ }
|
2020-01-20 23:18:13 +00:00
|
|
|
| TerminatorKind::FalseUnwind { real_target: _, unwind: _ } => {
|
|
|
|
// no data used, thus irrelevant to borrowck
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-26 04:31:02 +00:00
|
|
|
fn visit_after_primary_terminator_effect(
|
2020-01-20 23:18:13 +00:00
|
|
|
&mut self,
|
2024-10-31 01:33:12 +00:00
|
|
|
_results: &mut Results<'tcx, Borrowck<'a, 'tcx>>,
|
2024-11-26 03:02:10 +00:00
|
|
|
state: &BorrowckDomain,
|
2024-09-05 23:18:17 +00:00
|
|
|
term: &'a Terminator<'tcx>,
|
2020-01-20 23:18:13 +00:00
|
|
|
loc: Location,
|
|
|
|
) {
|
|
|
|
let span = term.source_info.span;
|
2018-01-11 18:50:40 +00:00
|
|
|
|
2020-01-20 23:18:13 +00:00
|
|
|
match term.kind {
|
|
|
|
TerminatorKind::Yield { value: _, resume: _, resume_arg: _, drop: _ } => {
|
2023-10-19 21:46:28 +00:00
|
|
|
if self.movable_coroutine {
|
2018-01-11 18:50:40 +00:00
|
|
|
// Look for any active borrows to locals
|
2024-09-13 06:27:24 +00:00
|
|
|
for i in state.borrows.iter() {
|
2024-10-04 03:47:32 +00:00
|
|
|
let borrow = &self.borrow_set[i];
|
2020-01-20 23:18:13 +00:00
|
|
|
self.check_for_local_borrow(borrow, span);
|
|
|
|
}
|
2018-01-11 18:50:40 +00:00
|
|
|
}
|
2017-08-16 20:05:48 +00:00
|
|
|
}
|
|
|
|
|
2023-08-19 11:10:25 +00:00
|
|
|
TerminatorKind::UnwindResume
|
|
|
|
| TerminatorKind::Return
|
2024-02-15 19:54:37 +00:00
|
|
|
| TerminatorKind::TailCall { .. }
|
2023-10-19 16:06:43 +00:00
|
|
|
| TerminatorKind::CoroutineDrop => {
|
2024-07-11 20:38:34 +00:00
|
|
|
// Returning from the function implicitly kills storage for all locals and statics.
|
|
|
|
// Often, the storage will already have been killed by an explicit
|
2017-11-19 12:26:23 +00:00
|
|
|
// StorageDead, but we don't always emit those (notably on unwind paths),
|
|
|
|
// so this "extra check" serves as a kind of backup.
|
2024-09-13 06:27:24 +00:00
|
|
|
for i in state.borrows.iter() {
|
2024-10-04 03:47:32 +00:00
|
|
|
let borrow = &self.borrow_set[i];
|
2020-01-20 23:18:13 +00:00
|
|
|
self.check_for_invalidation_at_exit(loc, borrow, span);
|
|
|
|
}
|
2017-11-19 12:26:23 +00:00
|
|
|
}
|
2020-01-20 23:18:13 +00:00
|
|
|
|
2023-08-21 07:57:10 +00:00
|
|
|
TerminatorKind::UnwindTerminate(_)
|
2020-02-11 20:04:46 +00:00
|
|
|
| TerminatorKind::Assert { .. }
|
|
|
|
| TerminatorKind::Call { .. }
|
|
|
|
| TerminatorKind::Drop { .. }
|
2020-06-02 07:15:24 +00:00
|
|
|
| TerminatorKind::FalseEdge { real_target: _, imaginary_target: _ }
|
2020-02-11 20:04:46 +00:00
|
|
|
| TerminatorKind::FalseUnwind { real_target: _, unwind: _ }
|
|
|
|
| TerminatorKind::Goto { .. }
|
|
|
|
| TerminatorKind::SwitchInt { .. }
|
2020-02-14 18:17:50 +00:00
|
|
|
| TerminatorKind::Unreachable
|
|
|
|
| TerminatorKind::InlineAsm { .. } => {}
|
2017-07-05 12:52:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 15:07:45 +00:00
|
|
|
use self::AccessDepth::{Deep, Shallow};
|
2017-12-07 16:45:13 +00:00
|
|
|
use self::ReadOrWrite::{Activation, Read, Reservation, Write};
|
2017-08-21 10:48:33 +00:00
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
|
|
|
enum ArtificialField {
|
2023-11-08 12:42:30 +00:00
|
|
|
FakeBorrow,
|
2017-08-21 10:48:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
2018-09-23 15:07:45 +00:00
|
|
|
enum AccessDepth {
|
2017-08-21 10:48:33 +00:00
|
|
|
/// From the RFC: "A *shallow* access means that the immediate
|
2018-01-29 00:25:35 +00:00
|
|
|
/// fields reached at P are accessed, but references or pointers
|
2017-08-21 10:48:33 +00:00
|
|
|
/// found within are not dereferenced. Right now, the only access
|
|
|
|
/// that is shallow is an assignment like `x = ...;`, which would
|
|
|
|
/// be a *shallow write* of `x`."
|
|
|
|
Shallow(Option<ArtificialField>),
|
|
|
|
|
|
|
|
/// From the RFC: "A *deep* access means that all data reachable
|
2017-12-01 12:39:51 +00:00
|
|
|
/// through the given place may be invalidated or accesses by
|
2017-08-21 10:48:33 +00:00
|
|
|
/// this action."
|
|
|
|
Deep,
|
2018-09-23 15:07:45 +00:00
|
|
|
|
|
|
|
/// Access is Deep only when there is a Drop implementation that
|
|
|
|
/// can reach the data behind the reference.
|
|
|
|
Drop,
|
2017-08-21 10:48:33 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 16:44:24 +00:00
|
|
|
/// Kind of access to a value: read or write
|
|
|
|
/// (For informational purposes only)
|
2017-08-21 10:48:33 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
|
|
|
enum ReadOrWrite {
|
|
|
|
/// From the RFC: "A *read* means that the existing data may be
|
|
|
|
/// read, but will not be changed."
|
|
|
|
Read(ReadKind),
|
|
|
|
|
|
|
|
/// From the RFC: "A *write* means that the data may be mutated to
|
|
|
|
/// new values or otherwise invalidated (for example, it could be
|
|
|
|
/// de-initialized, as in a move operation).
|
|
|
|
Write(WriteKind),
|
2017-12-07 16:45:13 +00:00
|
|
|
|
|
|
|
/// For two-phase borrows, we distinguish a reservation (which is treated
|
|
|
|
/// like a Read) from an activation (which is treated like a write), and
|
|
|
|
/// each of those is furthermore distinguished from Reads/Writes above.
|
|
|
|
Reservation(WriteKind),
|
|
|
|
Activation(WriteKind, BorrowIndex),
|
2017-08-21 10:48:33 +00:00
|
|
|
}
|
|
|
|
|
2017-11-16 16:44:24 +00:00
|
|
|
/// Kind of read access to a value
|
|
|
|
/// (For informational purposes only)
|
2017-08-21 10:48:33 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
|
|
|
enum ReadKind {
|
|
|
|
Borrow(BorrowKind),
|
|
|
|
Copy,
|
|
|
|
}
|
|
|
|
|
2017-11-16 16:44:24 +00:00
|
|
|
/// Kind of write access to a value
|
|
|
|
/// (For informational purposes only)
|
2017-08-21 10:48:33 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
|
|
|
enum WriteKind {
|
2018-09-23 15:07:45 +00:00
|
|
|
StorageDeadOrDrop,
|
2023-05-25 17:30:23 +00:00
|
|
|
Replace,
|
2017-08-21 10:48:33 +00:00
|
|
|
MutableBorrow(BorrowKind),
|
|
|
|
Mutate,
|
|
|
|
Move,
|
|
|
|
}
|
|
|
|
|
2017-12-01 12:39:51 +00:00
|
|
|
/// When checking permissions for a place access, this flag is used to indicate that an immutable
|
|
|
|
/// local place can be mutated.
|
2019-02-08 13:53:55 +00:00
|
|
|
//
|
|
|
|
// FIXME: @nikomatsakis suggested that this flag could be removed with the following modifications:
|
|
|
|
// - Split `is_mutable()` into `is_assignable()` (can be directly assigned) and
|
|
|
|
// `is_declared_mutable()`.
|
|
|
|
// - Take flow state into consideration in `is_assignable()` for local variables.
|
2017-11-16 16:44:24 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
|
|
|
enum LocalMutationIsAllowed {
|
|
|
|
Yes,
|
2017-12-07 18:48:12 +00:00
|
|
|
/// We want use of immutable upvars to cause a "write to immutable upvar"
|
|
|
|
/// error, not an "reassignment" error.
|
|
|
|
ExceptUpvars,
|
2017-12-14 12:33:29 +00:00
|
|
|
No,
|
2017-11-16 16:44:24 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 13:48:33 +00:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
MIR: Fix value moved diagnose messages
MIR: adopt borrowck test
Fix trailing whitespace
span_bug! on unexpected action
Make RegionVid use newtype_index!
Closes #45843
Check rvalue aggregates during check_stmt in tycheck, add initial, (not passing) test
Fix failing test
Remove attributes and test comments accidentally left behind, add in span_mirbugs
Normalize LvalueTy for ops and format code to satisfy tidy check
only normalize operand types when in an ADT constructor
avoid early return
handle the active field index in unions
normalize types in ADT constructor
Fixes #45940
Fix borrowck compiler errors for upvars contain "spurious" dereferences
Fixes #46003
added associated function Box::leak
Box::leak - improve documentation
Box::leak - fixed bug in documentation
Box::leak - relaxed constraints wrt. lifetimes
Box::leak - updated documentation
Box::leak - made an oops, fixed now =)
Box::leak: update unstable issue number (46179).
Add test for #44953
Add missing Debug impls to std_unicode
Also adds #![deny(missing_debug_implementations)] so they don't get
missed again.
Amend RELEASES for 1.22.1
and fix the date for 1.22.0
Rename param in `[T]::swap_with_slice` from `src` to `other`.
The idea of ‘source’ and ‘destination’ aren’t very applicable for this
operation since both slices can both be considered sources and
destinations.
Clarify stdin behavior of `Command::output`.
Fixes #44929.
Add hints for the case of confusing enum with its variants
Add failing testcases
Add module population and case of enum in place of expression
Use for_each_child_stable in find_module
Use multiline text for crate conflict diagnostics
Make float::from_bits transmute (and update the documentation to reflect this).
The current implementation/documentation was made to avoid sNaN because of
potential safety issues implied by old/bad LLVM documentation. These issues
aren't real, so we can just make the implementation transmute (as permitted
by the existing documentation of this method).
Also the documentation didn't actually match the behaviour: it said we may
change sNaNs, but in fact we canonicalized *all* NaNs.
Also an example in the documentation was wrong: it said we *always* change
sNaNs, when the documentation was explicitly written to indicate it was
implementation-defined.
This makes to_bits and from_bits perfectly roundtrip cross-platform, except
for one caveat: although the 2008 edition of IEEE-754 specifies how to
interpet the signaling bit, earlier editions didn't. This lead to some platforms
picking the opposite interpretation, so all signaling NaNs on x86/ARM are quiet
on MIPS, and vice-versa.
NaN-boxing is a fairly important optimization, while we don't even guarantee
that float operations properly preserve signalingness. As such, this seems like
the more natural strategy to take (as opposed to trying to mangle the signaling
bit on a per-platform basis).
This implementation is also, of course, faster.
Simplify an Iterator::fold to Iterator::any
This method of once-diagnostics doesn't allow nesting
UI tests extract the regular output from the 'rendered' field in json
Merge cfail and ui tests into ui tests
Add a MIR pass to lower 128-bit operators to lang item calls
Runs only with `-Z lower_128bit_ops` since it's not hooked into targets yet.
Include tuple projections in MIR tests
Add type checking for the lang item
As part of doing so, add more lang items instead of passing u128 to the i128 ones where it doesn't matter in twos-complement.
Handle shifts properly
* The overflow-checking shift items need to take a full 128-bit type, since they need to be able to detect idiocy like `1i128 << (1u128 << 127)`
* The unchecked ones just take u32, like the `*_sh?` methods in core
* Because shift-by-anything is allowed, cast into a new local for every shift
incr.comp.: Make sure we don't lose unused green results from the query cache.
rustbuild: Update LLVM and enable ThinLTO
This commit updates LLVM to fix #45511 (https://reviews.llvm.org/D39981) and
also reenables ThinLTO for libtest now that we shouldn't hit #45768. This also
opportunistically enables ThinLTO for libstd which was previously blocked
(#45661) on test failures related to debuginfo with a presumed cause of #45511.
Closes #45511
std: Flag Windows TLS dtor symbol as #[used]
Turns out ThinLTO was internalizing this symbol and eliminating it. Worse yet if
you compiled with LTO turns out no TLS destructors would run on Windows! The
`#[used]` annotation should be a more bulletproof implementation (in the face of
LTO) of preserving this symbol all the way through in LLVM and ensuring it makes
it all the way to the linker which will take care of it.
Add enum InitializationRequiringAction
Fix tidy tests
2017-11-23 11:36:48 +00:00
|
|
|
enum InitializationRequiringAction {
|
|
|
|
Borrow,
|
2018-09-10 21:34:38 +00:00
|
|
|
MatchOn,
|
MIR: Fix value moved diagnose messages
MIR: adopt borrowck test
Fix trailing whitespace
span_bug! on unexpected action
Make RegionVid use newtype_index!
Closes #45843
Check rvalue aggregates during check_stmt in tycheck, add initial, (not passing) test
Fix failing test
Remove attributes and test comments accidentally left behind, add in span_mirbugs
Normalize LvalueTy for ops and format code to satisfy tidy check
only normalize operand types when in an ADT constructor
avoid early return
handle the active field index in unions
normalize types in ADT constructor
Fixes #45940
Fix borrowck compiler errors for upvars contain "spurious" dereferences
Fixes #46003
added associated function Box::leak
Box::leak - improve documentation
Box::leak - fixed bug in documentation
Box::leak - relaxed constraints wrt. lifetimes
Box::leak - updated documentation
Box::leak - made an oops, fixed now =)
Box::leak: update unstable issue number (46179).
Add test for #44953
Add missing Debug impls to std_unicode
Also adds #![deny(missing_debug_implementations)] so they don't get
missed again.
Amend RELEASES for 1.22.1
and fix the date for 1.22.0
Rename param in `[T]::swap_with_slice` from `src` to `other`.
The idea of ‘source’ and ‘destination’ aren’t very applicable for this
operation since both slices can both be considered sources and
destinations.
Clarify stdin behavior of `Command::output`.
Fixes #44929.
Add hints for the case of confusing enum with its variants
Add failing testcases
Add module population and case of enum in place of expression
Use for_each_child_stable in find_module
Use multiline text for crate conflict diagnostics
Make float::from_bits transmute (and update the documentation to reflect this).
The current implementation/documentation was made to avoid sNaN because of
potential safety issues implied by old/bad LLVM documentation. These issues
aren't real, so we can just make the implementation transmute (as permitted
by the existing documentation of this method).
Also the documentation didn't actually match the behaviour: it said we may
change sNaNs, but in fact we canonicalized *all* NaNs.
Also an example in the documentation was wrong: it said we *always* change
sNaNs, when the documentation was explicitly written to indicate it was
implementation-defined.
This makes to_bits and from_bits perfectly roundtrip cross-platform, except
for one caveat: although the 2008 edition of IEEE-754 specifies how to
interpet the signaling bit, earlier editions didn't. This lead to some platforms
picking the opposite interpretation, so all signaling NaNs on x86/ARM are quiet
on MIPS, and vice-versa.
NaN-boxing is a fairly important optimization, while we don't even guarantee
that float operations properly preserve signalingness. As such, this seems like
the more natural strategy to take (as opposed to trying to mangle the signaling
bit on a per-platform basis).
This implementation is also, of course, faster.
Simplify an Iterator::fold to Iterator::any
This method of once-diagnostics doesn't allow nesting
UI tests extract the regular output from the 'rendered' field in json
Merge cfail and ui tests into ui tests
Add a MIR pass to lower 128-bit operators to lang item calls
Runs only with `-Z lower_128bit_ops` since it's not hooked into targets yet.
Include tuple projections in MIR tests
Add type checking for the lang item
As part of doing so, add more lang items instead of passing u128 to the i128 ones where it doesn't matter in twos-complement.
Handle shifts properly
* The overflow-checking shift items need to take a full 128-bit type, since they need to be able to detect idiocy like `1i128 << (1u128 << 127)`
* The unchecked ones just take u32, like the `*_sh?` methods in core
* Because shift-by-anything is allowed, cast into a new local for every shift
incr.comp.: Make sure we don't lose unused green results from the query cache.
rustbuild: Update LLVM and enable ThinLTO
This commit updates LLVM to fix #45511 (https://reviews.llvm.org/D39981) and
also reenables ThinLTO for libtest now that we shouldn't hit #45768. This also
opportunistically enables ThinLTO for libstd which was previously blocked
(#45661) on test failures related to debuginfo with a presumed cause of #45511.
Closes #45511
std: Flag Windows TLS dtor symbol as #[used]
Turns out ThinLTO was internalizing this symbol and eliminating it. Worse yet if
you compiled with LTO turns out no TLS destructors would run on Windows! The
`#[used]` annotation should be a more bulletproof implementation (in the face of
LTO) of preserving this symbol all the way through in LLVM and ensuring it makes
it all the way to the linker which will take care of it.
Add enum InitializationRequiringAction
Fix tidy tests
2017-11-23 11:36:48 +00:00
|
|
|
Use,
|
|
|
|
Assignment,
|
2018-10-09 21:14:31 +00:00
|
|
|
PartialAssignment,
|
MIR: Fix value moved diagnose messages
MIR: adopt borrowck test
Fix trailing whitespace
span_bug! on unexpected action
Make RegionVid use newtype_index!
Closes #45843
Check rvalue aggregates during check_stmt in tycheck, add initial, (not passing) test
Fix failing test
Remove attributes and test comments accidentally left behind, add in span_mirbugs
Normalize LvalueTy for ops and format code to satisfy tidy check
only normalize operand types when in an ADT constructor
avoid early return
handle the active field index in unions
normalize types in ADT constructor
Fixes #45940
Fix borrowck compiler errors for upvars contain "spurious" dereferences
Fixes #46003
added associated function Box::leak
Box::leak - improve documentation
Box::leak - fixed bug in documentation
Box::leak - relaxed constraints wrt. lifetimes
Box::leak - updated documentation
Box::leak - made an oops, fixed now =)
Box::leak: update unstable issue number (46179).
Add test for #44953
Add missing Debug impls to std_unicode
Also adds #![deny(missing_debug_implementations)] so they don't get
missed again.
Amend RELEASES for 1.22.1
and fix the date for 1.22.0
Rename param in `[T]::swap_with_slice` from `src` to `other`.
The idea of ‘source’ and ‘destination’ aren’t very applicable for this
operation since both slices can both be considered sources and
destinations.
Clarify stdin behavior of `Command::output`.
Fixes #44929.
Add hints for the case of confusing enum with its variants
Add failing testcases
Add module population and case of enum in place of expression
Use for_each_child_stable in find_module
Use multiline text for crate conflict diagnostics
Make float::from_bits transmute (and update the documentation to reflect this).
The current implementation/documentation was made to avoid sNaN because of
potential safety issues implied by old/bad LLVM documentation. These issues
aren't real, so we can just make the implementation transmute (as permitted
by the existing documentation of this method).
Also the documentation didn't actually match the behaviour: it said we may
change sNaNs, but in fact we canonicalized *all* NaNs.
Also an example in the documentation was wrong: it said we *always* change
sNaNs, when the documentation was explicitly written to indicate it was
implementation-defined.
This makes to_bits and from_bits perfectly roundtrip cross-platform, except
for one caveat: although the 2008 edition of IEEE-754 specifies how to
interpet the signaling bit, earlier editions didn't. This lead to some platforms
picking the opposite interpretation, so all signaling NaNs on x86/ARM are quiet
on MIPS, and vice-versa.
NaN-boxing is a fairly important optimization, while we don't even guarantee
that float operations properly preserve signalingness. As such, this seems like
the more natural strategy to take (as opposed to trying to mangle the signaling
bit on a per-platform basis).
This implementation is also, of course, faster.
Simplify an Iterator::fold to Iterator::any
This method of once-diagnostics doesn't allow nesting
UI tests extract the regular output from the 'rendered' field in json
Merge cfail and ui tests into ui tests
Add a MIR pass to lower 128-bit operators to lang item calls
Runs only with `-Z lower_128bit_ops` since it's not hooked into targets yet.
Include tuple projections in MIR tests
Add type checking for the lang item
As part of doing so, add more lang items instead of passing u128 to the i128 ones where it doesn't matter in twos-complement.
Handle shifts properly
* The overflow-checking shift items need to take a full 128-bit type, since they need to be able to detect idiocy like `1i128 << (1u128 << 127)`
* The unchecked ones just take u32, like the `*_sh?` methods in core
* Because shift-by-anything is allowed, cast into a new local for every shift
incr.comp.: Make sure we don't lose unused green results from the query cache.
rustbuild: Update LLVM and enable ThinLTO
This commit updates LLVM to fix #45511 (https://reviews.llvm.org/D39981) and
also reenables ThinLTO for libtest now that we shouldn't hit #45768. This also
opportunistically enables ThinLTO for libstd which was previously blocked
(#45661) on test failures related to debuginfo with a presumed cause of #45511.
Closes #45511
std: Flag Windows TLS dtor symbol as #[used]
Turns out ThinLTO was internalizing this symbol and eliminating it. Worse yet if
you compiled with LTO turns out no TLS destructors would run on Windows! The
`#[used]` annotation should be a more bulletproof implementation (in the face of
LTO) of preserving this symbol all the way through in LLVM and ensuring it makes
it all the way to the linker which will take care of it.
Add enum InitializationRequiringAction
Fix tidy tests
2017-11-23 11:36:48 +00:00
|
|
|
}
|
|
|
|
|
2023-04-28 17:35:40 +00:00
|
|
|
#[derive(Debug)]
|
2020-03-03 20:22:54 +00:00
|
|
|
struct RootPlace<'tcx> {
|
2020-01-14 05:10:05 +00:00
|
|
|
place_local: Local,
|
2020-03-03 20:22:54 +00:00
|
|
|
place_projection: &'tcx [PlaceElem<'tcx>],
|
2018-04-22 06:41:44 +00:00
|
|
|
is_local_mutation_allowed: LocalMutationIsAllowed,
|
|
|
|
}
|
|
|
|
|
MIR: Fix value moved diagnose messages
MIR: adopt borrowck test
Fix trailing whitespace
span_bug! on unexpected action
Make RegionVid use newtype_index!
Closes #45843
Check rvalue aggregates during check_stmt in tycheck, add initial, (not passing) test
Fix failing test
Remove attributes and test comments accidentally left behind, add in span_mirbugs
Normalize LvalueTy for ops and format code to satisfy tidy check
only normalize operand types when in an ADT constructor
avoid early return
handle the active field index in unions
normalize types in ADT constructor
Fixes #45940
Fix borrowck compiler errors for upvars contain "spurious" dereferences
Fixes #46003
added associated function Box::leak
Box::leak - improve documentation
Box::leak - fixed bug in documentation
Box::leak - relaxed constraints wrt. lifetimes
Box::leak - updated documentation
Box::leak - made an oops, fixed now =)
Box::leak: update unstable issue number (46179).
Add test for #44953
Add missing Debug impls to std_unicode
Also adds #![deny(missing_debug_implementations)] so they don't get
missed again.
Amend RELEASES for 1.22.1
and fix the date for 1.22.0
Rename param in `[T]::swap_with_slice` from `src` to `other`.
The idea of ‘source’ and ‘destination’ aren’t very applicable for this
operation since both slices can both be considered sources and
destinations.
Clarify stdin behavior of `Command::output`.
Fixes #44929.
Add hints for the case of confusing enum with its variants
Add failing testcases
Add module population and case of enum in place of expression
Use for_each_child_stable in find_module
Use multiline text for crate conflict diagnostics
Make float::from_bits transmute (and update the documentation to reflect this).
The current implementation/documentation was made to avoid sNaN because of
potential safety issues implied by old/bad LLVM documentation. These issues
aren't real, so we can just make the implementation transmute (as permitted
by the existing documentation of this method).
Also the documentation didn't actually match the behaviour: it said we may
change sNaNs, but in fact we canonicalized *all* NaNs.
Also an example in the documentation was wrong: it said we *always* change
sNaNs, when the documentation was explicitly written to indicate it was
implementation-defined.
This makes to_bits and from_bits perfectly roundtrip cross-platform, except
for one caveat: although the 2008 edition of IEEE-754 specifies how to
interpet the signaling bit, earlier editions didn't. This lead to some platforms
picking the opposite interpretation, so all signaling NaNs on x86/ARM are quiet
on MIPS, and vice-versa.
NaN-boxing is a fairly important optimization, while we don't even guarantee
that float operations properly preserve signalingness. As such, this seems like
the more natural strategy to take (as opposed to trying to mangle the signaling
bit on a per-platform basis).
This implementation is also, of course, faster.
Simplify an Iterator::fold to Iterator::any
This method of once-diagnostics doesn't allow nesting
UI tests extract the regular output from the 'rendered' field in json
Merge cfail and ui tests into ui tests
Add a MIR pass to lower 128-bit operators to lang item calls
Runs only with `-Z lower_128bit_ops` since it's not hooked into targets yet.
Include tuple projections in MIR tests
Add type checking for the lang item
As part of doing so, add more lang items instead of passing u128 to the i128 ones where it doesn't matter in twos-complement.
Handle shifts properly
* The overflow-checking shift items need to take a full 128-bit type, since they need to be able to detect idiocy like `1i128 << (1u128 << 127)`
* The unchecked ones just take u32, like the `*_sh?` methods in core
* Because shift-by-anything is allowed, cast into a new local for every shift
incr.comp.: Make sure we don't lose unused green results from the query cache.
rustbuild: Update LLVM and enable ThinLTO
This commit updates LLVM to fix #45511 (https://reviews.llvm.org/D39981) and
also reenables ThinLTO for libtest now that we shouldn't hit #45768. This also
opportunistically enables ThinLTO for libstd which was previously blocked
(#45661) on test failures related to debuginfo with a presumed cause of #45511.
Closes #45511
std: Flag Windows TLS dtor symbol as #[used]
Turns out ThinLTO was internalizing this symbol and eliminating it. Worse yet if
you compiled with LTO turns out no TLS destructors would run on Windows! The
`#[used]` annotation should be a more bulletproof implementation (in the face of
LTO) of preserving this symbol all the way through in LLVM and ensuring it makes
it all the way to the linker which will take care of it.
Add enum InitializationRequiringAction
Fix tidy tests
2017-11-23 11:36:48 +00:00
|
|
|
impl InitializationRequiringAction {
|
|
|
|
fn as_noun(self) -> &'static str {
|
|
|
|
match self {
|
2017-11-17 09:47:02 +00:00
|
|
|
InitializationRequiringAction::Borrow => "borrow",
|
2018-09-10 21:34:38 +00:00
|
|
|
InitializationRequiringAction::MatchOn => "use", // no good noun
|
2017-11-17 09:47:02 +00:00
|
|
|
InitializationRequiringAction::Use => "use",
|
|
|
|
InitializationRequiringAction::Assignment => "assign",
|
2018-10-09 21:14:31 +00:00
|
|
|
InitializationRequiringAction::PartialAssignment => "assign to part",
|
MIR: Fix value moved diagnose messages
MIR: adopt borrowck test
Fix trailing whitespace
span_bug! on unexpected action
Make RegionVid use newtype_index!
Closes #45843
Check rvalue aggregates during check_stmt in tycheck, add initial, (not passing) test
Fix failing test
Remove attributes and test comments accidentally left behind, add in span_mirbugs
Normalize LvalueTy for ops and format code to satisfy tidy check
only normalize operand types when in an ADT constructor
avoid early return
handle the active field index in unions
normalize types in ADT constructor
Fixes #45940
Fix borrowck compiler errors for upvars contain "spurious" dereferences
Fixes #46003
added associated function Box::leak
Box::leak - improve documentation
Box::leak - fixed bug in documentation
Box::leak - relaxed constraints wrt. lifetimes
Box::leak - updated documentation
Box::leak - made an oops, fixed now =)
Box::leak: update unstable issue number (46179).
Add test for #44953
Add missing Debug impls to std_unicode
Also adds #![deny(missing_debug_implementations)] so they don't get
missed again.
Amend RELEASES for 1.22.1
and fix the date for 1.22.0
Rename param in `[T]::swap_with_slice` from `src` to `other`.
The idea of ‘source’ and ‘destination’ aren’t very applicable for this
operation since both slices can both be considered sources and
destinations.
Clarify stdin behavior of `Command::output`.
Fixes #44929.
Add hints for the case of confusing enum with its variants
Add failing testcases
Add module population and case of enum in place of expression
Use for_each_child_stable in find_module
Use multiline text for crate conflict diagnostics
Make float::from_bits transmute (and update the documentation to reflect this).
The current implementation/documentation was made to avoid sNaN because of
potential safety issues implied by old/bad LLVM documentation. These issues
aren't real, so we can just make the implementation transmute (as permitted
by the existing documentation of this method).
Also the documentation didn't actually match the behaviour: it said we may
change sNaNs, but in fact we canonicalized *all* NaNs.
Also an example in the documentation was wrong: it said we *always* change
sNaNs, when the documentation was explicitly written to indicate it was
implementation-defined.
This makes to_bits and from_bits perfectly roundtrip cross-platform, except
for one caveat: although the 2008 edition of IEEE-754 specifies how to
interpet the signaling bit, earlier editions didn't. This lead to some platforms
picking the opposite interpretation, so all signaling NaNs on x86/ARM are quiet
on MIPS, and vice-versa.
NaN-boxing is a fairly important optimization, while we don't even guarantee
that float operations properly preserve signalingness. As such, this seems like
the more natural strategy to take (as opposed to trying to mangle the signaling
bit on a per-platform basis).
This implementation is also, of course, faster.
Simplify an Iterator::fold to Iterator::any
This method of once-diagnostics doesn't allow nesting
UI tests extract the regular output from the 'rendered' field in json
Merge cfail and ui tests into ui tests
Add a MIR pass to lower 128-bit operators to lang item calls
Runs only with `-Z lower_128bit_ops` since it's not hooked into targets yet.
Include tuple projections in MIR tests
Add type checking for the lang item
As part of doing so, add more lang items instead of passing u128 to the i128 ones where it doesn't matter in twos-complement.
Handle shifts properly
* The overflow-checking shift items need to take a full 128-bit type, since they need to be able to detect idiocy like `1i128 << (1u128 << 127)`
* The unchecked ones just take u32, like the `*_sh?` methods in core
* Because shift-by-anything is allowed, cast into a new local for every shift
incr.comp.: Make sure we don't lose unused green results from the query cache.
rustbuild: Update LLVM and enable ThinLTO
This commit updates LLVM to fix #45511 (https://reviews.llvm.org/D39981) and
also reenables ThinLTO for libtest now that we shouldn't hit #45768. This also
opportunistically enables ThinLTO for libstd which was previously blocked
(#45661) on test failures related to debuginfo with a presumed cause of #45511.
Closes #45511
std: Flag Windows TLS dtor symbol as #[used]
Turns out ThinLTO was internalizing this symbol and eliminating it. Worse yet if
you compiled with LTO turns out no TLS destructors would run on Windows! The
`#[used]` annotation should be a more bulletproof implementation (in the face of
LTO) of preserving this symbol all the way through in LLVM and ensuring it makes
it all the way to the linker which will take care of it.
Add enum InitializationRequiringAction
Fix tidy tests
2017-11-23 11:36:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_verb_in_past_tense(self) -> &'static str {
|
|
|
|
match self {
|
2017-11-17 09:47:02 +00:00
|
|
|
InitializationRequiringAction::Borrow => "borrowed",
|
2018-09-10 21:34:38 +00:00
|
|
|
InitializationRequiringAction::MatchOn => "matched on",
|
2017-11-17 09:47:02 +00:00
|
|
|
InitializationRequiringAction::Use => "used",
|
|
|
|
InitializationRequiringAction::Assignment => "assigned",
|
2018-10-09 21:14:31 +00:00
|
|
|
InitializationRequiringAction::PartialAssignment => "partially assigned",
|
MIR: Fix value moved diagnose messages
MIR: adopt borrowck test
Fix trailing whitespace
span_bug! on unexpected action
Make RegionVid use newtype_index!
Closes #45843
Check rvalue aggregates during check_stmt in tycheck, add initial, (not passing) test
Fix failing test
Remove attributes and test comments accidentally left behind, add in span_mirbugs
Normalize LvalueTy for ops and format code to satisfy tidy check
only normalize operand types when in an ADT constructor
avoid early return
handle the active field index in unions
normalize types in ADT constructor
Fixes #45940
Fix borrowck compiler errors for upvars contain "spurious" dereferences
Fixes #46003
added associated function Box::leak
Box::leak - improve documentation
Box::leak - fixed bug in documentation
Box::leak - relaxed constraints wrt. lifetimes
Box::leak - updated documentation
Box::leak - made an oops, fixed now =)
Box::leak: update unstable issue number (46179).
Add test for #44953
Add missing Debug impls to std_unicode
Also adds #![deny(missing_debug_implementations)] so they don't get
missed again.
Amend RELEASES for 1.22.1
and fix the date for 1.22.0
Rename param in `[T]::swap_with_slice` from `src` to `other`.
The idea of ‘source’ and ‘destination’ aren’t very applicable for this
operation since both slices can both be considered sources and
destinations.
Clarify stdin behavior of `Command::output`.
Fixes #44929.
Add hints for the case of confusing enum with its variants
Add failing testcases
Add module population and case of enum in place of expression
Use for_each_child_stable in find_module
Use multiline text for crate conflict diagnostics
Make float::from_bits transmute (and update the documentation to reflect this).
The current implementation/documentation was made to avoid sNaN because of
potential safety issues implied by old/bad LLVM documentation. These issues
aren't real, so we can just make the implementation transmute (as permitted
by the existing documentation of this method).
Also the documentation didn't actually match the behaviour: it said we may
change sNaNs, but in fact we canonicalized *all* NaNs.
Also an example in the documentation was wrong: it said we *always* change
sNaNs, when the documentation was explicitly written to indicate it was
implementation-defined.
This makes to_bits and from_bits perfectly roundtrip cross-platform, except
for one caveat: although the 2008 edition of IEEE-754 specifies how to
interpet the signaling bit, earlier editions didn't. This lead to some platforms
picking the opposite interpretation, so all signaling NaNs on x86/ARM are quiet
on MIPS, and vice-versa.
NaN-boxing is a fairly important optimization, while we don't even guarantee
that float operations properly preserve signalingness. As such, this seems like
the more natural strategy to take (as opposed to trying to mangle the signaling
bit on a per-platform basis).
This implementation is also, of course, faster.
Simplify an Iterator::fold to Iterator::any
This method of once-diagnostics doesn't allow nesting
UI tests extract the regular output from the 'rendered' field in json
Merge cfail and ui tests into ui tests
Add a MIR pass to lower 128-bit operators to lang item calls
Runs only with `-Z lower_128bit_ops` since it's not hooked into targets yet.
Include tuple projections in MIR tests
Add type checking for the lang item
As part of doing so, add more lang items instead of passing u128 to the i128 ones where it doesn't matter in twos-complement.
Handle shifts properly
* The overflow-checking shift items need to take a full 128-bit type, since they need to be able to detect idiocy like `1i128 << (1u128 << 127)`
* The unchecked ones just take u32, like the `*_sh?` methods in core
* Because shift-by-anything is allowed, cast into a new local for every shift
incr.comp.: Make sure we don't lose unused green results from the query cache.
rustbuild: Update LLVM and enable ThinLTO
This commit updates LLVM to fix #45511 (https://reviews.llvm.org/D39981) and
also reenables ThinLTO for libtest now that we shouldn't hit #45768. This also
opportunistically enables ThinLTO for libstd which was previously blocked
(#45661) on test failures related to debuginfo with a presumed cause of #45511.
Closes #45511
std: Flag Windows TLS dtor symbol as #[used]
Turns out ThinLTO was internalizing this symbol and eliminating it. Worse yet if
you compiled with LTO turns out no TLS destructors would run on Windows! The
`#[used]` annotation should be a more bulletproof implementation (in the face of
LTO) of preserving this symbol all the way through in LLVM and ensuring it makes
it all the way to the linker which will take care of it.
Add enum InitializationRequiringAction
Fix tidy tests
2017-11-23 11:36:48 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-21 22:54:17 +00:00
|
|
|
|
|
|
|
fn as_general_verb_in_past_tense(self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
InitializationRequiringAction::Borrow
|
|
|
|
| InitializationRequiringAction::MatchOn
|
|
|
|
| InitializationRequiringAction::Use => "used",
|
|
|
|
InitializationRequiringAction::Assignment => "assigned",
|
|
|
|
InitializationRequiringAction::PartialAssignment => "partially assigned",
|
|
|
|
}
|
|
|
|
}
|
MIR: Fix value moved diagnose messages
MIR: adopt borrowck test
Fix trailing whitespace
span_bug! on unexpected action
Make RegionVid use newtype_index!
Closes #45843
Check rvalue aggregates during check_stmt in tycheck, add initial, (not passing) test
Fix failing test
Remove attributes and test comments accidentally left behind, add in span_mirbugs
Normalize LvalueTy for ops and format code to satisfy tidy check
only normalize operand types when in an ADT constructor
avoid early return
handle the active field index in unions
normalize types in ADT constructor
Fixes #45940
Fix borrowck compiler errors for upvars contain "spurious" dereferences
Fixes #46003
added associated function Box::leak
Box::leak - improve documentation
Box::leak - fixed bug in documentation
Box::leak - relaxed constraints wrt. lifetimes
Box::leak - updated documentation
Box::leak - made an oops, fixed now =)
Box::leak: update unstable issue number (46179).
Add test for #44953
Add missing Debug impls to std_unicode
Also adds #![deny(missing_debug_implementations)] so they don't get
missed again.
Amend RELEASES for 1.22.1
and fix the date for 1.22.0
Rename param in `[T]::swap_with_slice` from `src` to `other`.
The idea of ‘source’ and ‘destination’ aren’t very applicable for this
operation since both slices can both be considered sources and
destinations.
Clarify stdin behavior of `Command::output`.
Fixes #44929.
Add hints for the case of confusing enum with its variants
Add failing testcases
Add module population and case of enum in place of expression
Use for_each_child_stable in find_module
Use multiline text for crate conflict diagnostics
Make float::from_bits transmute (and update the documentation to reflect this).
The current implementation/documentation was made to avoid sNaN because of
potential safety issues implied by old/bad LLVM documentation. These issues
aren't real, so we can just make the implementation transmute (as permitted
by the existing documentation of this method).
Also the documentation didn't actually match the behaviour: it said we may
change sNaNs, but in fact we canonicalized *all* NaNs.
Also an example in the documentation was wrong: it said we *always* change
sNaNs, when the documentation was explicitly written to indicate it was
implementation-defined.
This makes to_bits and from_bits perfectly roundtrip cross-platform, except
for one caveat: although the 2008 edition of IEEE-754 specifies how to
interpet the signaling bit, earlier editions didn't. This lead to some platforms
picking the opposite interpretation, so all signaling NaNs on x86/ARM are quiet
on MIPS, and vice-versa.
NaN-boxing is a fairly important optimization, while we don't even guarantee
that float operations properly preserve signalingness. As such, this seems like
the more natural strategy to take (as opposed to trying to mangle the signaling
bit on a per-platform basis).
This implementation is also, of course, faster.
Simplify an Iterator::fold to Iterator::any
This method of once-diagnostics doesn't allow nesting
UI tests extract the regular output from the 'rendered' field in json
Merge cfail and ui tests into ui tests
Add a MIR pass to lower 128-bit operators to lang item calls
Runs only with `-Z lower_128bit_ops` since it's not hooked into targets yet.
Include tuple projections in MIR tests
Add type checking for the lang item
As part of doing so, add more lang items instead of passing u128 to the i128 ones where it doesn't matter in twos-complement.
Handle shifts properly
* The overflow-checking shift items need to take a full 128-bit type, since they need to be able to detect idiocy like `1i128 << (1u128 << 127)`
* The unchecked ones just take u32, like the `*_sh?` methods in core
* Because shift-by-anything is allowed, cast into a new local for every shift
incr.comp.: Make sure we don't lose unused green results from the query cache.
rustbuild: Update LLVM and enable ThinLTO
This commit updates LLVM to fix #45511 (https://reviews.llvm.org/D39981) and
also reenables ThinLTO for libtest now that we shouldn't hit #45768. This also
opportunistically enables ThinLTO for libstd which was previously blocked
(#45661) on test failures related to debuginfo with a presumed cause of #45511.
Closes #45511
std: Flag Windows TLS dtor symbol as #[used]
Turns out ThinLTO was internalizing this symbol and eliminating it. Worse yet if
you compiled with LTO turns out no TLS destructors would run on Windows! The
`#[used]` annotation should be a more bulletproof implementation (in the face of
LTO) of preserving this symbol all the way through in LLVM and ensuring it makes
it all the way to the linker which will take care of it.
Add enum InitializationRequiringAction
Fix tidy tests
2017-11-23 11:36:48 +00:00
|
|
|
}
|
|
|
|
|
2024-09-05 23:18:17 +00:00
|
|
|
impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
|
|
|
|
fn body(&self) -> &'a Body<'tcx> {
|
2020-04-12 17:28:41 +00:00
|
|
|
self.body
|
2020-01-20 23:18:13 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 12:39:51 +00:00
|
|
|
/// Checks an access to the given place to see if it is allowed. Examines the set of borrows
|
2017-11-17 00:09:18 +00:00
|
|
|
/// that are in scope, as well as which paths have been initialized, to ensure that (a) the
|
2017-12-01 12:39:51 +00:00
|
|
|
/// place is initialized and (b) it is not borrowed in some way that would prevent this
|
2017-11-17 00:09:18 +00:00
|
|
|
/// access.
|
|
|
|
///
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Returns `true` if an error is reported.
|
2017-11-17 09:47:02 +00:00
|
|
|
fn access_place(
|
|
|
|
&mut self,
|
2019-05-01 21:03:17 +00:00
|
|
|
location: Location,
|
2020-03-30 20:49:33 +00:00
|
|
|
place_span: (Place<'tcx>, Span),
|
2018-09-23 15:07:45 +00:00
|
|
|
kind: (AccessDepth, ReadOrWrite),
|
2017-11-17 09:47:02 +00:00
|
|
|
is_local_mutation_allowed: LocalMutationIsAllowed,
|
2024-11-26 03:02:10 +00:00
|
|
|
state: &BorrowckDomain,
|
2018-08-10 21:34:56 +00:00
|
|
|
) {
|
2017-08-21 10:48:33 +00:00
|
|
|
let (sd, rw) = kind;
|
2017-10-21 19:15:04 +00:00
|
|
|
|
2017-12-07 16:45:13 +00:00
|
|
|
if let Activation(_, borrow_index) = rw {
|
|
|
|
if self.reservation_error_reported.contains(&place_span.0) {
|
2018-03-06 05:29:03 +00:00
|
|
|
debug!(
|
|
|
|
"skipping access_place for activation of invalid reservation \
|
|
|
|
place: {:?} borrow_index: {:?}",
|
|
|
|
place_span.0, borrow_index
|
|
|
|
);
|
2018-08-10 21:34:56 +00:00
|
|
|
return;
|
2017-12-07 16:45:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-21 08:06:55 +00:00
|
|
|
// Check is_empty() first because it's the common case, and doing that
|
|
|
|
// way we avoid the clone() call.
|
|
|
|
if !self.access_place_error_reported.is_empty()
|
2020-03-30 20:49:33 +00:00
|
|
|
&& self.access_place_error_reported.contains(&(place_span.0, place_span.1))
|
2018-03-06 05:29:03 +00:00
|
|
|
{
|
|
|
|
debug!(
|
|
|
|
"access_place: suppressing error place_span=`{:?}` kind=`{:?}`",
|
|
|
|
place_span, kind
|
|
|
|
);
|
2018-08-10 21:34:56 +00:00
|
|
|
return;
|
2018-01-19 22:11:59 +00:00
|
|
|
}
|
|
|
|
|
2018-07-15 14:11:29 +00:00
|
|
|
let mutability_error = self.check_access_permissions(
|
|
|
|
place_span,
|
|
|
|
rw,
|
|
|
|
is_local_mutation_allowed,
|
2024-09-13 06:27:24 +00:00
|
|
|
state,
|
2019-05-01 21:03:17 +00:00
|
|
|
location,
|
2018-07-15 14:11:29 +00:00
|
|
|
);
|
2024-09-13 06:27:24 +00:00
|
|
|
let conflict_error = self.check_access_for_conflict(location, place_span, sd, rw, state);
|
2017-12-07 17:12:01 +00:00
|
|
|
|
2018-01-20 02:15:57 +00:00
|
|
|
if conflict_error || mutability_error {
|
2018-03-06 05:29:03 +00:00
|
|
|
debug!("access_place: logging error place_span=`{:?}` kind=`{:?}`", place_span, kind);
|
2020-03-30 20:49:33 +00:00
|
|
|
self.access_place_error_reported.insert((place_span.0, place_span.1));
|
2018-01-19 22:11:59 +00:00
|
|
|
}
|
2017-12-07 17:12:01 +00:00
|
|
|
}
|
|
|
|
|
2024-12-19 17:21:51 +00:00
|
|
|
fn borrows_in_scope<'s>(
|
2024-12-04 18:58:59 +00:00
|
|
|
&self,
|
|
|
|
location: Location,
|
|
|
|
state: &'s BorrowckDomain,
|
2025-01-07 15:19:05 +00:00
|
|
|
) -> Cow<'s, DenseBitSet<BorrowIndex>> {
|
2024-12-04 18:58:59 +00:00
|
|
|
if let Some(polonius) = &self.polonius_output {
|
2024-12-19 17:21:51 +00:00
|
|
|
// Use polonius output if it has been enabled.
|
2024-12-04 18:58:59 +00:00
|
|
|
let location = self.location_table.start_index(location);
|
2025-01-07 15:19:05 +00:00
|
|
|
let mut polonius_output = DenseBitSet::new_empty(self.borrow_set.len());
|
2024-12-04 18:58:59 +00:00
|
|
|
for &idx in polonius.errors_at(location) {
|
|
|
|
polonius_output.insert(idx);
|
|
|
|
}
|
|
|
|
Cow::Owned(polonius_output)
|
|
|
|
} else {
|
|
|
|
Cow::Borrowed(&state.borrows)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-13 06:27:24 +00:00
|
|
|
#[instrument(level = "debug", skip(self, state))]
|
2017-12-07 17:12:01 +00:00
|
|
|
fn check_access_for_conflict(
|
|
|
|
&mut self,
|
2019-05-01 21:03:17 +00:00
|
|
|
location: Location,
|
2020-03-30 20:49:33 +00:00
|
|
|
place_span: (Place<'tcx>, Span),
|
2018-09-23 15:07:45 +00:00
|
|
|
sd: AccessDepth,
|
2017-12-07 17:12:01 +00:00
|
|
|
rw: ReadOrWrite,
|
2024-11-26 03:02:10 +00:00
|
|
|
state: &BorrowckDomain,
|
2017-12-07 17:12:01 +00:00
|
|
|
) -> bool {
|
|
|
|
let mut error_reported = false;
|
2020-01-20 23:18:13 +00:00
|
|
|
|
2024-12-19 17:21:51 +00:00
|
|
|
let borrows_in_scope = self.borrows_in_scope(location, state);
|
2020-01-20 23:18:13 +00:00
|
|
|
|
2018-05-19 06:47:48 +00:00
|
|
|
each_borrow_involving_path(
|
|
|
|
self,
|
2023-11-23 10:42:19 +00:00
|
|
|
self.infcx.tcx,
|
|
|
|
self.body,
|
2017-11-17 09:47:02 +00:00
|
|
|
(sd, place_span.0),
|
2024-10-04 03:47:32 +00:00
|
|
|
self.borrow_set,
|
2023-05-19 08:26:31 +00:00
|
|
|
|borrow_index| borrows_in_scope.contains(borrow_index),
|
2018-06-22 03:10:52 +00:00
|
|
|
|this, borrow_index, borrow| match (rw, borrow.kind) {
|
2017-12-14 23:34:16 +00:00
|
|
|
// Obviously an activation is compatible with its own
|
|
|
|
// reservation (or even prior activating uses of same
|
|
|
|
// borrow); so don't check if they interfere.
|
|
|
|
//
|
|
|
|
// NOTE: *reservations* do conflict with themselves;
|
2022-03-30 05:39:38 +00:00
|
|
|
// thus aren't injecting unsoundness w/ this check.)
|
2018-04-06 21:03:14 +00:00
|
|
|
(Activation(_, activating), _) if activating == borrow_index => {
|
2017-12-14 12:33:29 +00:00
|
|
|
debug!(
|
|
|
|
"check_access_for_conflict place_span: {:?} sd: {:?} rw: {:?} \
|
2018-04-06 21:03:14 +00:00
|
|
|
skipping {:?} b/c activation of same borrow_index",
|
2017-12-14 12:33:29 +00:00
|
|
|
place_span,
|
|
|
|
sd,
|
|
|
|
rw,
|
2018-04-06 21:03:14 +00:00
|
|
|
(borrow_index, borrow),
|
2017-12-14 12:33:29 +00:00
|
|
|
);
|
2024-12-30 11:27:24 +00:00
|
|
|
ControlFlow::Continue(())
|
2017-12-14 23:34:16 +00:00
|
|
|
}
|
2017-12-01 15:02:15 +00:00
|
|
|
|
2024-04-06 22:30:28 +00:00
|
|
|
(Read(_), BorrowKind::Shared | BorrowKind::Fake(_))
|
|
|
|
| (
|
|
|
|
Read(ReadKind::Borrow(BorrowKind::Fake(FakeBorrowKind::Shallow))),
|
|
|
|
BorrowKind::Mut { .. },
|
2024-12-30 11:27:24 +00:00
|
|
|
) => ControlFlow::Continue(()),
|
2018-09-10 21:33:45 +00:00
|
|
|
|
2024-04-06 22:30:28 +00:00
|
|
|
(Reservation(_), BorrowKind::Fake(_) | BorrowKind::Shared) => {
|
2022-04-21 02:06:44 +00:00
|
|
|
// This used to be a future compatibility warning (to be
|
|
|
|
// disallowed on NLL). See rust-lang/rust#56254
|
2024-12-30 11:27:24 +00:00
|
|
|
ControlFlow::Continue(())
|
2022-04-21 02:06:44 +00:00
|
|
|
}
|
|
|
|
|
2024-04-06 22:30:28 +00:00
|
|
|
(Write(WriteKind::Move), BorrowKind::Fake(FakeBorrowKind::Shallow)) => {
|
2018-09-10 21:33:45 +00:00
|
|
|
// Handled by initialization checks.
|
2024-12-30 11:27:24 +00:00
|
|
|
ControlFlow::Continue(())
|
2017-12-14 12:33:29 +00:00
|
|
|
}
|
2017-12-07 16:45:13 +00:00
|
|
|
|
2023-05-30 22:25:27 +00:00
|
|
|
(Read(kind), BorrowKind::Mut { .. }) => {
|
2017-12-01 15:02:15 +00:00
|
|
|
// Reading from mere reservations of mutable-borrows is OK.
|
2023-05-17 10:28:33 +00:00
|
|
|
if !is_active(this.dominators(), borrow, location) {
|
2024-12-30 11:12:53 +00:00
|
|
|
assert!(borrow.kind.allows_two_phase_borrow());
|
2024-12-30 11:27:24 +00:00
|
|
|
return ControlFlow::Continue(());
|
2017-12-01 15:02:15 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 13:13:11 +00:00
|
|
|
error_reported = true;
|
2017-11-17 09:47:02 +00:00
|
|
|
match kind {
|
2018-09-10 21:34:38 +00:00
|
|
|
ReadKind::Copy => {
|
2022-02-06 20:15:39 +00:00
|
|
|
let err = this
|
|
|
|
.report_use_while_mutably_borrowed(location, place_span, borrow);
|
|
|
|
this.buffer_error(err);
|
2017-11-17 09:47:02 +00:00
|
|
|
}
|
|
|
|
ReadKind::Borrow(bk) => {
|
2022-02-06 20:15:39 +00:00
|
|
|
let err =
|
|
|
|
this.report_conflicting_borrow(location, place_span, bk, borrow);
|
|
|
|
this.buffer_error(err);
|
2017-08-21 10:48:33 +00:00
|
|
|
}
|
|
|
|
}
|
2024-12-30 11:27:24 +00:00
|
|
|
ControlFlow::Break(())
|
2017-11-17 09:47:02 +00:00
|
|
|
}
|
2017-12-07 16:45:13 +00:00
|
|
|
|
2020-04-17 00:38:52 +00:00
|
|
|
(Reservation(kind) | Activation(kind, _) | Write(kind), _) => {
|
2017-12-07 16:45:13 +00:00
|
|
|
match rw {
|
2019-03-01 20:09:54 +00:00
|
|
|
Reservation(..) => {
|
2017-12-14 12:33:29 +00:00
|
|
|
debug!(
|
|
|
|
"recording invalid reservation of \
|
|
|
|
place: {:?}",
|
|
|
|
place_span.0
|
|
|
|
);
|
2020-04-15 22:00:22 +00:00
|
|
|
this.reservation_error_reported.insert(place_span.0);
|
2018-03-06 05:29:03 +00:00
|
|
|
}
|
2017-12-07 16:45:13 +00:00
|
|
|
Activation(_, activating) => {
|
2017-12-14 12:33:29 +00:00
|
|
|
debug!(
|
|
|
|
"observing check_place for activation of \
|
|
|
|
borrow_index: {:?}",
|
|
|
|
activating
|
|
|
|
);
|
2018-03-06 05:29:03 +00:00
|
|
|
}
|
|
|
|
Read(..) | Write(..) => {}
|
2017-12-07 16:45:13 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 13:13:11 +00:00
|
|
|
error_reported = true;
|
2017-11-17 09:47:02 +00:00
|
|
|
match kind {
|
|
|
|
WriteKind::MutableBorrow(bk) => {
|
2022-02-06 20:15:39 +00:00
|
|
|
let err =
|
|
|
|
this.report_conflicting_borrow(location, place_span, bk, borrow);
|
|
|
|
this.buffer_error(err);
|
2017-11-17 09:47:02 +00:00
|
|
|
}
|
2023-02-16 22:14:04 +00:00
|
|
|
WriteKind::StorageDeadOrDrop => this
|
2023-05-25 17:30:23 +00:00
|
|
|
.report_borrowed_value_does_not_live_long_enough(
|
|
|
|
location,
|
|
|
|
borrow,
|
|
|
|
place_span,
|
|
|
|
Some(WriteKind::StorageDeadOrDrop),
|
|
|
|
),
|
2017-11-17 09:47:02 +00:00
|
|
|
WriteKind::Mutate => {
|
2019-05-01 21:03:17 +00:00
|
|
|
this.report_illegal_mutation_of_borrowed(location, place_span, borrow)
|
2017-11-17 09:47:02 +00:00
|
|
|
}
|
|
|
|
WriteKind::Move => {
|
2019-05-01 21:03:17 +00:00
|
|
|
this.report_move_out_while_borrowed(location, place_span, borrow)
|
2017-08-21 10:48:33 +00:00
|
|
|
}
|
2023-05-25 17:30:23 +00:00
|
|
|
WriteKind::Replace => {
|
|
|
|
this.report_illegal_mutation_of_borrowed(location, place_span, borrow)
|
|
|
|
}
|
2017-08-21 10:48:33 +00:00
|
|
|
}
|
2024-12-30 11:27:24 +00:00
|
|
|
ControlFlow::Break(())
|
2017-08-21 10:48:33 +00:00
|
|
|
}
|
2017-11-17 09:47:02 +00:00
|
|
|
},
|
|
|
|
);
|
2017-11-20 23:50:04 +00:00
|
|
|
|
2017-12-07 17:12:01 +00:00
|
|
|
error_reported
|
2017-08-21 10:48:33 +00:00
|
|
|
}
|
|
|
|
|
2024-12-04 18:58:59 +00:00
|
|
|
/// Through #123739, backward incompatible drops (BIDs) are introduced.
|
|
|
|
/// We would like to emit lints whether borrow checking fails at these future drop locations.
|
|
|
|
#[instrument(level = "debug", skip(self, state))]
|
|
|
|
fn check_backward_incompatible_drop(
|
|
|
|
&mut self,
|
|
|
|
location: Location,
|
2025-01-05 19:11:00 +00:00
|
|
|
(place, place_span): (Place<'tcx>, Span),
|
2024-12-04 18:58:59 +00:00
|
|
|
state: &BorrowckDomain,
|
|
|
|
) {
|
2025-01-05 19:11:00 +00:00
|
|
|
let tcx = self.infcx.tcx;
|
|
|
|
// If this type does not need `Drop`, then treat it like a `StorageDead`.
|
|
|
|
// This is needed because we track the borrows of refs to thread locals,
|
|
|
|
// and we'll ICE because we don't track borrows behind shared references.
|
|
|
|
let sd = if place.ty(self.body, tcx).ty.needs_drop(tcx, self.body.typing_env(tcx)) {
|
|
|
|
AccessDepth::Drop
|
|
|
|
} else {
|
|
|
|
AccessDepth::Shallow(None)
|
|
|
|
};
|
2024-12-04 18:58:59 +00:00
|
|
|
|
2024-12-19 17:21:51 +00:00
|
|
|
let borrows_in_scope = self.borrows_in_scope(location, state);
|
2024-12-04 18:58:59 +00:00
|
|
|
|
|
|
|
// This is a very simplified version of `Self::check_access_for_conflict`.
|
|
|
|
// We are here checking on BIDs and specifically still-live borrows of data involving the BIDs.
|
|
|
|
each_borrow_involving_path(
|
|
|
|
self,
|
|
|
|
self.infcx.tcx,
|
|
|
|
self.body,
|
2025-01-05 19:11:00 +00:00
|
|
|
(sd, place),
|
2024-12-04 18:58:59 +00:00
|
|
|
self.borrow_set,
|
|
|
|
|borrow_index| borrows_in_scope.contains(borrow_index),
|
|
|
|
|this, _borrow_index, borrow| {
|
|
|
|
if matches!(borrow.kind, BorrowKind::Fake(_)) {
|
2025-01-06 03:25:45 +00:00
|
|
|
return ControlFlow::Continue(());
|
2024-12-04 18:58:59 +00:00
|
|
|
}
|
|
|
|
let borrowed = this.retrieve_borrow_spans(borrow).var_or_use_path_span();
|
2025-01-06 03:25:45 +00:00
|
|
|
let explain = this.explain_why_borrow_contains_point(
|
|
|
|
location,
|
|
|
|
borrow,
|
|
|
|
Some((WriteKind::StorageDeadOrDrop, place)),
|
|
|
|
);
|
|
|
|
this.infcx.tcx.node_span_lint(
|
2024-12-04 18:58:59 +00:00
|
|
|
TAIL_EXPR_DROP_ORDER,
|
|
|
|
CRATE_HIR_ID,
|
2025-01-06 03:25:45 +00:00
|
|
|
borrowed,
|
|
|
|
|diag| {
|
|
|
|
session_diagnostics::TailExprDropOrder { borrowed }.decorate_lint(diag);
|
|
|
|
explain.add_explanation_to_diagnostic(&this, diag, "", None, None);
|
|
|
|
},
|
2024-12-04 18:58:59 +00:00
|
|
|
);
|
|
|
|
// We may stop at the first case
|
2025-01-06 03:25:45 +00:00
|
|
|
ControlFlow::Break(())
|
2024-12-04 18:58:59 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-11-17 09:47:02 +00:00
|
|
|
fn mutate_place(
|
|
|
|
&mut self,
|
2019-05-01 21:03:17 +00:00
|
|
|
location: Location,
|
2020-03-30 20:49:33 +00:00
|
|
|
place_span: (Place<'tcx>, Span),
|
2018-09-23 15:07:45 +00:00
|
|
|
kind: AccessDepth,
|
2024-11-26 03:02:10 +00:00
|
|
|
state: &BorrowckDomain,
|
2017-11-17 09:47:02 +00:00
|
|
|
) {
|
2022-01-12 00:00:00 +00:00
|
|
|
// Write of P[i] or *P requires P init'd.
|
2024-09-13 06:27:24 +00:00
|
|
|
self.check_if_assigned_path_is_moved(location, place_span, state);
|
2017-07-05 12:52:18 +00:00
|
|
|
|
2018-08-10 21:34:56 +00:00
|
|
|
self.access_place(
|
2019-05-01 21:03:17 +00:00
|
|
|
location,
|
2017-11-17 09:47:02 +00:00
|
|
|
place_span,
|
|
|
|
(kind, Write(WriteKind::Mutate)),
|
2018-08-10 21:34:56 +00:00
|
|
|
LocalMutationIsAllowed::No,
|
2024-09-13 06:27:24 +00:00
|
|
|
state,
|
2017-11-17 09:47:02 +00:00
|
|
|
);
|
2017-07-05 12:52:18 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 09:47:02 +00:00
|
|
|
fn consume_rvalue(
|
|
|
|
&mut self,
|
2019-05-01 21:03:17 +00:00
|
|
|
location: Location,
|
2024-09-05 23:18:17 +00:00
|
|
|
(rvalue, span): (&'a Rvalue<'tcx>, Span),
|
2024-11-26 03:02:10 +00:00
|
|
|
state: &BorrowckDomain,
|
2017-11-17 09:47:02 +00:00
|
|
|
) {
|
2022-12-02 19:07:57 +00:00
|
|
|
match rvalue {
|
|
|
|
&Rvalue::Ref(_ /*rgn*/, bk, place) => {
|
2017-08-21 10:48:33 +00:00
|
|
|
let access_kind = match bk {
|
2024-04-06 22:30:28 +00:00
|
|
|
BorrowKind::Fake(FakeBorrowKind::Shallow) => {
|
2023-11-08 12:42:30 +00:00
|
|
|
(Shallow(Some(ArtificialField::FakeBorrow)), Read(ReadKind::Borrow(bk)))
|
2018-09-10 21:33:45 +00:00
|
|
|
}
|
2024-04-06 22:30:28 +00:00
|
|
|
BorrowKind::Shared | BorrowKind::Fake(FakeBorrowKind::Deep) => {
|
|
|
|
(Deep, Read(ReadKind::Borrow(bk)))
|
|
|
|
}
|
2023-05-30 22:25:27 +00:00
|
|
|
BorrowKind::Mut { .. } => {
|
2017-12-07 16:45:13 +00:00
|
|
|
let wk = WriteKind::MutableBorrow(bk);
|
2024-12-30 11:12:53 +00:00
|
|
|
if bk.allows_two_phase_borrow() {
|
2017-12-07 16:45:13 +00:00
|
|
|
(Deep, Reservation(wk))
|
|
|
|
} else {
|
|
|
|
(Deep, Write(wk))
|
|
|
|
}
|
2017-11-17 09:47:02 +00:00
|
|
|
}
|
2017-08-21 10:48:33 +00:00
|
|
|
};
|
2017-12-07 16:45:13 +00:00
|
|
|
|
2017-11-17 09:47:02 +00:00
|
|
|
self.access_place(
|
2019-05-01 21:03:17 +00:00
|
|
|
location,
|
2017-11-17 09:47:02 +00:00
|
|
|
(place, span),
|
|
|
|
access_kind,
|
|
|
|
LocalMutationIsAllowed::No,
|
2024-09-13 06:27:24 +00:00
|
|
|
state,
|
2017-11-17 09:47:02 +00:00
|
|
|
);
|
2017-12-07 16:45:13 +00:00
|
|
|
|
2024-04-06 22:30:28 +00:00
|
|
|
let action = if bk == BorrowKind::Fake(FakeBorrowKind::Shallow) {
|
2018-09-10 21:34:38 +00:00
|
|
|
InitializationRequiringAction::MatchOn
|
|
|
|
} else {
|
|
|
|
InitializationRequiringAction::Borrow
|
|
|
|
};
|
|
|
|
|
2018-03-26 20:00:14 +00:00
|
|
|
self.check_if_path_or_subpath_is_moved(
|
2019-05-01 21:03:17 +00:00
|
|
|
location,
|
2018-09-10 21:34:38 +00:00
|
|
|
action,
|
2019-07-21 20:38:30 +00:00
|
|
|
(place.as_ref(), span),
|
2024-09-13 06:27:24 +00:00
|
|
|
state,
|
2017-11-17 09:47:02 +00:00
|
|
|
);
|
2017-07-05 12:52:18 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 08:57:57 +00:00
|
|
|
&Rvalue::RawPtr(mutability, place) => {
|
2018-12-23 19:00:58 +00:00
|
|
|
let access_kind = match mutability {
|
|
|
|
Mutability::Mut => (
|
|
|
|
Deep,
|
|
|
|
Write(WriteKind::MutableBorrow(BorrowKind::Mut {
|
2023-05-30 22:25:27 +00:00
|
|
|
kind: MutBorrowKind::Default,
|
2018-12-23 19:00:58 +00:00
|
|
|
})),
|
2019-12-22 22:42:04 +00:00
|
|
|
),
|
2018-12-23 19:00:58 +00:00
|
|
|
Mutability::Not => (Deep, Read(ReadKind::Borrow(BorrowKind::Shared))),
|
|
|
|
};
|
|
|
|
|
|
|
|
self.access_place(
|
|
|
|
location,
|
|
|
|
(place, span),
|
|
|
|
access_kind,
|
|
|
|
LocalMutationIsAllowed::No,
|
2024-09-13 06:27:24 +00:00
|
|
|
state,
|
2018-12-23 19:00:58 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
self.check_if_path_or_subpath_is_moved(
|
|
|
|
location,
|
|
|
|
InitializationRequiringAction::Borrow,
|
|
|
|
(place.as_ref(), span),
|
2024-09-13 06:27:24 +00:00
|
|
|
state,
|
2018-12-23 19:00:58 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-02 19:44:25 +00:00
|
|
|
Rvalue::ThreadLocalRef(_) => {}
|
|
|
|
|
2022-12-02 19:07:57 +00:00
|
|
|
Rvalue::Use(operand)
|
|
|
|
| Rvalue::Repeat(operand, _)
|
|
|
|
| Rvalue::UnaryOp(_ /*un_op*/, operand)
|
|
|
|
| Rvalue::Cast(_ /*cast_kind*/, operand, _ /*ty*/)
|
|
|
|
| Rvalue::ShallowInitBox(operand, _ /*ty*/) => {
|
2024-09-13 06:27:24 +00:00
|
|
|
self.consume_operand(location, (operand, span), state)
|
2017-07-05 12:52:18 +00:00
|
|
|
}
|
2022-12-02 19:07:57 +00:00
|
|
|
|
|
|
|
&Rvalue::CopyForDeref(place) => {
|
2022-06-13 13:37:41 +00:00
|
|
|
self.access_place(
|
|
|
|
location,
|
|
|
|
(place, span),
|
|
|
|
(Deep, Read(ReadKind::Copy)),
|
|
|
|
LocalMutationIsAllowed::No,
|
2024-09-13 06:27:24 +00:00
|
|
|
state,
|
2022-06-13 13:37:41 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Finally, check if path was already moved.
|
|
|
|
self.check_if_path_or_subpath_is_moved(
|
|
|
|
location,
|
|
|
|
InitializationRequiringAction::Use,
|
|
|
|
(place.as_ref(), span),
|
2024-09-13 06:27:24 +00:00
|
|
|
state,
|
2022-06-13 13:37:41 +00:00
|
|
|
);
|
|
|
|
}
|
2017-07-05 12:52:18 +00:00
|
|
|
|
2024-12-14 01:43:16 +00:00
|
|
|
&Rvalue::Discriminant(place) => {
|
2017-11-17 09:47:02 +00:00
|
|
|
self.access_place(
|
2019-05-01 21:03:17 +00:00
|
|
|
location,
|
2017-11-17 09:47:02 +00:00
|
|
|
(place, span),
|
2024-12-14 01:43:16 +00:00
|
|
|
(Shallow(None), Read(ReadKind::Copy)),
|
2017-11-17 09:47:02 +00:00
|
|
|
LocalMutationIsAllowed::No,
|
2024-09-13 06:27:24 +00:00
|
|
|
state,
|
2017-11-17 09:47:02 +00:00
|
|
|
);
|
2018-03-26 20:00:14 +00:00
|
|
|
self.check_if_path_or_subpath_is_moved(
|
2019-05-01 21:03:17 +00:00
|
|
|
location,
|
2017-11-17 09:47:02 +00:00
|
|
|
InitializationRequiringAction::Use,
|
2019-07-21 20:38:30 +00:00
|
|
|
(place.as_ref(), span),
|
2024-09-13 06:27:24 +00:00
|
|
|
state,
|
2017-11-17 09:47:02 +00:00
|
|
|
);
|
2017-07-05 12:52:18 +00:00
|
|
|
}
|
|
|
|
|
2024-05-16 09:07:31 +00:00
|
|
|
Rvalue::BinaryOp(_bin_op, box (operand1, operand2)) => {
|
2024-09-13 06:27:24 +00:00
|
|
|
self.consume_operand(location, (operand1, span), state);
|
|
|
|
self.consume_operand(location, (operand2, span), state);
|
2017-07-05 12:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Rvalue::NullaryOp(_op, _ty) => {
|
|
|
|
// nullary ops take no dynamic input; no borrowck effect.
|
|
|
|
}
|
|
|
|
|
2022-12-02 19:07:57 +00:00
|
|
|
Rvalue::Aggregate(aggregate_kind, operands) => {
|
2018-03-03 04:42:37 +00:00
|
|
|
// We need to report back the list of mutable upvars that were
|
|
|
|
// moved into the closure and subsequently used by the closure,
|
|
|
|
// in order to populate our used_mut set.
|
2018-07-03 19:12:09 +00:00
|
|
|
match **aggregate_kind {
|
2024-01-24 18:01:56 +00:00
|
|
|
AggregateKind::Closure(def_id, _)
|
|
|
|
| AggregateKind::CoroutineClosure(def_id, _)
|
|
|
|
| AggregateKind::Coroutine(def_id, _) => {
|
2023-01-23 22:15:33 +00:00
|
|
|
let def_id = def_id.expect_local();
|
2018-07-03 19:12:09 +00:00
|
|
|
let BorrowCheckResult { used_mut_upvars, .. } =
|
2022-07-12 14:10:22 +00:00
|
|
|
self.infcx.tcx.mir_borrowck(def_id);
|
2018-07-03 19:12:09 +00:00
|
|
|
debug!("{:?} used_mut_upvars={:?}", def_id, used_mut_upvars);
|
|
|
|
for field in used_mut_upvars {
|
2023-04-02 03:11:38 +00:00
|
|
|
self.propagate_closure_used_mut_upvar(&operands[*field]);
|
2018-03-03 04:42:37 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-03 19:12:09 +00:00
|
|
|
AggregateKind::Adt(..)
|
|
|
|
| AggregateKind::Array(..)
|
2024-04-11 21:44:02 +00:00
|
|
|
| AggregateKind::Tuple { .. }
|
|
|
|
| AggregateKind::RawPtr(..) => (),
|
2018-03-03 04:42:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for operand in operands {
|
2024-09-13 06:27:24 +00:00
|
|
|
self.consume_operand(location, (operand, span), state);
|
2018-03-03 04:42:37 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-05 12:52:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-04 20:25:08 +00:00
|
|
|
fn propagate_closure_used_mut_upvar(&mut self, operand: &Operand<'tcx>) {
|
2020-03-30 20:55:22 +00:00
|
|
|
let propagate_closure_used_mut_place = |this: &mut Self, place: Place<'tcx>| {
|
2021-01-29 21:01:27 +00:00
|
|
|
// We have three possibilities here:
|
2020-12-16 04:00:19 +00:00
|
|
|
// a. We are modifying something through a mut-ref
|
|
|
|
// b. We are modifying something that is local to our parent
|
2021-01-29 21:01:27 +00:00
|
|
|
// c. Current body is a nested closure, and we are modifying path starting from
|
2020-12-16 04:00:19 +00:00
|
|
|
// a Place captured by our parent closure.
|
|
|
|
|
|
|
|
// Handle (c), the path being modified is exactly the path captured by our parent
|
|
|
|
if let Some(field) = this.is_upvar_field_projection(place.as_ref()) {
|
|
|
|
this.used_mut_upvars.push(field);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (place_ref, proj) in place.iter_projections().rev() {
|
|
|
|
// Handle (a)
|
|
|
|
if proj == ProjectionElem::Deref {
|
|
|
|
match place_ref.ty(this.body(), this.infcx.tcx).ty.kind() {
|
|
|
|
// We aren't modifying a variable directly
|
|
|
|
ty::Ref(_, _, hir::Mutability::Mut) => return,
|
|
|
|
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle (c)
|
|
|
|
if let Some(field) = this.is_upvar_field_projection(place_ref) {
|
2019-04-30 16:58:24 +00:00
|
|
|
this.used_mut_upvars.push(field);
|
2020-12-16 04:00:19 +00:00
|
|
|
return;
|
2019-04-04 20:25:08 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-16 04:00:19 +00:00
|
|
|
|
|
|
|
// Handle(b)
|
|
|
|
this.used_mut.insert(place.local);
|
2019-04-04 20:25:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// This relies on the current way that by-value
|
|
|
|
// captures of a closure are copied/moved directly
|
|
|
|
// when generating MIR.
|
|
|
|
match *operand {
|
2020-03-30 20:55:22 +00:00
|
|
|
Operand::Move(place) | Operand::Copy(place) => {
|
2019-10-20 20:09:36 +00:00
|
|
|
match place.as_local() {
|
2019-11-06 17:00:46 +00:00
|
|
|
Some(local) if !self.body.local_decls[local].is_user_variable() => {
|
|
|
|
if self.body.local_decls[local].ty.is_mutable_ptr() {
|
2019-10-20 20:09:36 +00:00
|
|
|
// The variable will be marked as mutable by the borrow.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// This is an edge case where we have a `move` closure
|
|
|
|
// inside a non-move closure, and the inner closure
|
|
|
|
// contains a mutation:
|
|
|
|
//
|
|
|
|
// let mut i = 0;
|
|
|
|
// || { move || { i += 1; }; };
|
|
|
|
//
|
|
|
|
// In this case our usual strategy of assuming that the
|
|
|
|
// variable will be captured by mutable reference is
|
|
|
|
// wrong, since `i` can be copied into the inner
|
|
|
|
// closure from a shared reference.
|
|
|
|
//
|
|
|
|
// As such we have to search for the local that this
|
|
|
|
// capture comes from and mark it as being used as mut.
|
|
|
|
|
2023-10-12 17:19:19 +00:00
|
|
|
let Some(temp_mpi) = self.move_data.rev_lookup.find_local(local) else {
|
|
|
|
bug!("temporary should be tracked");
|
|
|
|
};
|
2019-10-20 20:09:36 +00:00
|
|
|
let init = if let [init_index] = *self.move_data.init_path_map[temp_mpi] {
|
|
|
|
&self.move_data.inits[init_index]
|
|
|
|
} else {
|
|
|
|
bug!("temporary should be initialized exactly once")
|
|
|
|
};
|
|
|
|
|
2022-02-02 11:45:20 +00:00
|
|
|
let InitLocation::Statement(loc) = init.location else {
|
|
|
|
bug!("temporary initialized in arguments")
|
2019-10-20 20:09:36 +00:00
|
|
|
};
|
|
|
|
|
2019-11-06 17:00:46 +00:00
|
|
|
let body = self.body;
|
|
|
|
let bbd = &body[loc.block];
|
2019-10-20 20:09:36 +00:00
|
|
|
let stmt = &bbd.statements[loc.statement_index];
|
|
|
|
debug!("temporary assigned in: stmt={:?}", stmt);
|
|
|
|
|
2020-03-30 20:55:22 +00:00
|
|
|
if let StatementKind::Assign(box (_, Rvalue::Ref(_, _, source))) = stmt.kind
|
2019-10-20 20:09:36 +00:00
|
|
|
{
|
|
|
|
propagate_closure_used_mut_place(self, source);
|
|
|
|
} else {
|
|
|
|
bug!(
|
|
|
|
"closures should only capture user variables \
|
|
|
|
or references to user variables"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => propagate_closure_used_mut_place(self, place),
|
2019-04-04 20:25:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Operand::Constant(..) => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 09:47:02 +00:00
|
|
|
fn consume_operand(
|
|
|
|
&mut self,
|
2019-05-01 21:03:17 +00:00
|
|
|
location: Location,
|
2024-09-05 23:18:17 +00:00
|
|
|
(operand, span): (&'a Operand<'tcx>, Span),
|
2024-11-26 03:02:10 +00:00
|
|
|
state: &BorrowckDomain,
|
2017-11-17 09:47:02 +00:00
|
|
|
) {
|
2017-07-05 12:52:18 +00:00
|
|
|
match *operand {
|
2020-03-30 20:49:33 +00:00
|
|
|
Operand::Copy(place) => {
|
2017-12-01 12:39:51 +00:00
|
|
|
// copy of place: check if this is "copy of frozen path"
|
2017-11-17 15:19:57 +00:00
|
|
|
// (FIXME: see check_loans.rs)
|
2017-11-17 09:47:02 +00:00
|
|
|
self.access_place(
|
2019-05-01 21:03:17 +00:00
|
|
|
location,
|
2017-11-17 09:47:02 +00:00
|
|
|
(place, span),
|
|
|
|
(Deep, Read(ReadKind::Copy)),
|
|
|
|
LocalMutationIsAllowed::No,
|
2024-09-13 06:27:24 +00:00
|
|
|
state,
|
2017-11-17 09:47:02 +00:00
|
|
|
);
|
2017-07-05 12:52:18 +00:00
|
|
|
|
2017-11-17 15:19:57 +00:00
|
|
|
// Finally, check if path was already moved.
|
2018-03-26 20:00:14 +00:00
|
|
|
self.check_if_path_or_subpath_is_moved(
|
2019-05-01 21:03:17 +00:00
|
|
|
location,
|
2017-11-17 09:47:02 +00:00
|
|
|
InitializationRequiringAction::Use,
|
2019-07-21 20:38:30 +00:00
|
|
|
(place.as_ref(), span),
|
2024-09-13 06:27:24 +00:00
|
|
|
state,
|
2017-11-17 09:47:02 +00:00
|
|
|
);
|
2017-07-05 12:52:18 +00:00
|
|
|
}
|
2020-03-30 20:49:33 +00:00
|
|
|
Operand::Move(place) => {
|
2023-09-30 15:14:07 +00:00
|
|
|
// Check if moving from this place makes sense.
|
|
|
|
self.check_movable_place(location, place);
|
|
|
|
|
2017-12-01 12:39:51 +00:00
|
|
|
// move of place: check if this is move of already borrowed path
|
2017-11-17 09:47:02 +00:00
|
|
|
self.access_place(
|
2019-05-01 21:03:17 +00:00
|
|
|
location,
|
2017-11-17 09:47:02 +00:00
|
|
|
(place, span),
|
|
|
|
(Deep, Write(WriteKind::Move)),
|
2017-12-07 18:48:12 +00:00
|
|
|
LocalMutationIsAllowed::Yes,
|
2024-09-13 06:27:24 +00:00
|
|
|
state,
|
2017-11-17 09:47:02 +00:00
|
|
|
);
|
2017-11-17 15:19:57 +00:00
|
|
|
|
|
|
|
// Finally, check if path was already moved.
|
2018-03-26 20:00:14 +00:00
|
|
|
self.check_if_path_or_subpath_is_moved(
|
2019-05-01 21:03:17 +00:00
|
|
|
location,
|
2017-11-17 09:47:02 +00:00
|
|
|
InitializationRequiringAction::Use,
|
2019-07-21 20:38:30 +00:00
|
|
|
(place.as_ref(), span),
|
2024-09-13 06:27:24 +00:00
|
|
|
state,
|
2017-11-17 09:47:02 +00:00
|
|
|
);
|
2017-07-05 12:52:18 +00:00
|
|
|
}
|
2017-11-17 15:19:57 +00:00
|
|
|
Operand::Constant(_) => {}
|
2017-07-05 12:52:18 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-27 23:43:59 +00:00
|
|
|
|
2018-09-17 23:47:53 +00:00
|
|
|
/// Checks whether a borrow of this place is invalidated when the function
|
2017-11-27 23:43:59 +00:00
|
|
|
/// exits
|
2022-08-29 06:00:08 +00:00
|
|
|
#[instrument(level = "debug", skip(self))]
|
2017-12-14 12:33:29 +00:00
|
|
|
fn check_for_invalidation_at_exit(
|
|
|
|
&mut self,
|
2019-05-01 21:03:17 +00:00
|
|
|
location: Location,
|
2017-12-14 12:33:29 +00:00
|
|
|
borrow: &BorrowData<'tcx>,
|
|
|
|
span: Span,
|
|
|
|
) {
|
2020-03-30 20:49:33 +00:00
|
|
|
let place = borrow.borrowed_place;
|
2020-01-14 05:10:05 +00:00
|
|
|
let mut root_place = PlaceRef { local: place.local, projection: &[] };
|
2017-11-27 23:43:59 +00:00
|
|
|
|
|
|
|
// FIXME(nll-rfc#40): do more precise destructor tracking here. For now
|
|
|
|
// we just know that all locals are dropped at function exit (otherwise
|
|
|
|
// we'll have a memory leak) and assume that all statics have a destructor.
|
2017-12-03 14:08:28 +00:00
|
|
|
//
|
2017-12-05 22:51:47 +00:00
|
|
|
// FIXME: allow thread-locals to borrow other thread locals?
|
2019-04-30 16:58:24 +00:00
|
|
|
|
2019-12-11 19:50:03 +00:00
|
|
|
let (might_be_alive, will_be_dropped) =
|
2020-01-14 05:10:05 +00:00
|
|
|
if self.body.local_decls[root_place.local].is_ref_to_thread_local() {
|
2019-12-11 19:50:03 +00:00
|
|
|
// Thread-locals might be dropped after the function exits
|
|
|
|
// We have to dereference the outer reference because
|
|
|
|
// borrows don't conflict behind shared references.
|
2022-05-27 03:22:28 +00:00
|
|
|
root_place.projection = TyCtxtConsts::DEREF_PROJECTION;
|
2019-12-11 19:50:03 +00:00
|
|
|
(true, true)
|
|
|
|
} else {
|
|
|
|
(false, self.locals_are_invalidated_at_exit)
|
|
|
|
};
|
2017-11-27 23:43:59 +00:00
|
|
|
|
|
|
|
if !will_be_dropped {
|
2017-11-17 09:47:02 +00:00
|
|
|
debug!("place_is_invalidated_at_exit({:?}) - won't be dropped", place);
|
2017-12-10 15:11:02 +00:00
|
|
|
return;
|
2017-11-27 23:43:59 +00:00
|
|
|
}
|
|
|
|
|
2017-12-14 12:33:29 +00:00
|
|
|
let sd = if might_be_alive { Deep } else { Shallow(None) };
|
2017-11-27 23:43:59 +00:00
|
|
|
|
2018-09-10 21:33:45 +00:00
|
|
|
if places_conflict::borrow_conflicts_with_place(
|
|
|
|
self.infcx.tcx,
|
2023-11-21 19:07:32 +00:00
|
|
|
self.body,
|
2018-09-10 21:33:45 +00:00
|
|
|
place,
|
|
|
|
borrow.kind,
|
2019-07-11 17:25:37 +00:00
|
|
|
root_place,
|
2018-12-17 12:11:33 +00:00
|
|
|
sd,
|
|
|
|
places_conflict::PlaceConflictBias::Overlap,
|
2018-09-10 21:33:45 +00:00
|
|
|
) {
|
2017-12-10 15:11:02 +00:00
|
|
|
debug!("check_for_invalidation_at_exit({:?}): INVALID", place);
|
|
|
|
// FIXME: should be talking about the region lifetime instead
|
|
|
|
// of just a span here.
|
2018-09-11 14:38:35 +00:00
|
|
|
let span = self.infcx.tcx.sess.source_map().end_point(span);
|
2017-12-10 15:11:02 +00:00
|
|
|
self.report_borrowed_value_does_not_live_long_enough(
|
2019-05-01 21:03:17 +00:00
|
|
|
location,
|
2017-12-10 15:11:02 +00:00
|
|
|
borrow,
|
2018-06-13 17:51:53 +00:00
|
|
|
(place, span),
|
|
|
|
None,
|
2017-12-10 15:11:02 +00:00
|
|
|
)
|
2017-12-03 15:55:41 +00:00
|
|
|
}
|
2017-11-27 23:43:59 +00:00
|
|
|
}
|
2017-12-07 16:45:13 +00:00
|
|
|
|
2018-01-11 18:50:40 +00:00
|
|
|
/// Reports an error if this is a borrow of local data.
|
2023-10-19 21:46:28 +00:00
|
|
|
/// This is called for all Yield expressions on movable coroutines
|
2018-03-06 05:29:03 +00:00
|
|
|
fn check_for_local_borrow(&mut self, borrow: &BorrowData<'tcx>, yield_span: Span) {
|
2018-01-11 18:50:40 +00:00
|
|
|
debug!("check_for_local_borrow({:?})", borrow);
|
|
|
|
|
2020-03-30 21:51:48 +00:00
|
|
|
if borrow_of_local_data(borrow.borrowed_place) {
|
2023-10-19 21:46:28 +00:00
|
|
|
let err = self.cannot_borrow_across_coroutine_yield(
|
2018-08-01 19:38:02 +00:00
|
|
|
self.retrieve_borrow_spans(borrow).var_or_use(),
|
2018-03-06 05:29:03 +00:00
|
|
|
yield_span,
|
2018-07-18 21:10:08 +00:00
|
|
|
);
|
|
|
|
|
2022-02-06 20:15:39 +00:00
|
|
|
self.buffer_error(err);
|
2018-01-11 18:50:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-26 03:02:10 +00:00
|
|
|
fn check_activations(&mut self, location: Location, span: Span, state: &BorrowckDomain) {
|
2017-12-07 16:45:13 +00:00
|
|
|
// Two-phase borrow support: For each activation that is newly
|
|
|
|
// generated at this statement, check if it interferes with
|
|
|
|
// another borrow.
|
2024-10-04 03:47:32 +00:00
|
|
|
for &borrow_index in self.borrow_set.activations_at_location(location) {
|
|
|
|
let borrow = &self.borrow_set[borrow_index];
|
2018-04-07 09:43:36 +00:00
|
|
|
|
|
|
|
// only mutable borrows should be 2-phase
|
|
|
|
assert!(match borrow.kind {
|
2024-04-06 22:30:28 +00:00
|
|
|
BorrowKind::Shared | BorrowKind::Fake(_) => false,
|
2023-05-30 22:25:27 +00:00
|
|
|
BorrowKind::Mut { .. } => true,
|
2018-04-07 09:43:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
self.access_place(
|
2019-05-01 21:03:17 +00:00
|
|
|
location,
|
2020-03-30 20:49:33 +00:00
|
|
|
(borrow.borrowed_place, span),
|
2018-04-07 09:43:36 +00:00
|
|
|
(Deep, Activation(WriteKind::MutableBorrow(borrow.kind), borrow_index)),
|
|
|
|
LocalMutationIsAllowed::No,
|
2024-09-13 06:27:24 +00:00
|
|
|
state,
|
2018-04-07 09:43:36 +00:00
|
|
|
);
|
|
|
|
// We do not need to call `check_if_path_or_subpath_is_moved`
|
|
|
|
// again, as we already called it when we made the
|
|
|
|
// initial reservation.
|
|
|
|
}
|
2017-12-07 16:45:13 +00:00
|
|
|
}
|
2017-07-05 12:52:18 +00:00
|
|
|
|
2023-09-30 15:14:07 +00:00
|
|
|
fn check_movable_place(&mut self, location: Location, place: Place<'tcx>) {
|
|
|
|
use IllegalMoveOriginKind::*;
|
|
|
|
|
|
|
|
let body = self.body;
|
|
|
|
let tcx = self.infcx.tcx;
|
|
|
|
let mut place_ty = PlaceTy::from_ty(body.local_decls[place.local].ty);
|
|
|
|
for (place_ref, elem) in place.iter_projections() {
|
|
|
|
match elem {
|
|
|
|
ProjectionElem::Deref => match place_ty.ty.kind() {
|
|
|
|
ty::Ref(..) | ty::RawPtr(..) => {
|
|
|
|
self.move_errors.push(MoveError::new(place, location, BorrowedContent {
|
|
|
|
target_place: place_ref.project_deeper(&[elem], tcx),
|
|
|
|
}));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ty::Adt(adt, _) => {
|
|
|
|
if !adt.is_box() {
|
|
|
|
bug!("Adt should be a box type when Place is deref");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ty::Bool
|
|
|
|
| ty::Char
|
|
|
|
| ty::Int(_)
|
|
|
|
| ty::Uint(_)
|
|
|
|
| ty::Float(_)
|
|
|
|
| ty::Foreign(_)
|
|
|
|
| ty::Str
|
|
|
|
| ty::Array(_, _)
|
2023-02-02 13:57:36 +00:00
|
|
|
| ty::Pat(_, _)
|
2023-09-30 15:14:07 +00:00
|
|
|
| ty::Slice(_)
|
|
|
|
| ty::FnDef(_, _)
|
2024-08-08 07:18:20 +00:00
|
|
|
| ty::FnPtr(..)
|
2023-09-30 15:14:07 +00:00
|
|
|
| ty::Dynamic(_, _, _)
|
|
|
|
| ty::Closure(_, _)
|
2024-01-24 18:01:56 +00:00
|
|
|
| ty::CoroutineClosure(_, _)
|
2023-12-21 01:52:10 +00:00
|
|
|
| ty::Coroutine(_, _)
|
2023-09-30 15:14:07 +00:00
|
|
|
| ty::CoroutineWitness(..)
|
|
|
|
| ty::Never
|
|
|
|
| ty::Tuple(_)
|
2024-12-21 17:05:40 +00:00
|
|
|
| ty::UnsafeBinder(_)
|
2023-09-30 15:14:07 +00:00
|
|
|
| ty::Alias(_, _)
|
|
|
|
| ty::Param(_)
|
|
|
|
| ty::Bound(_, _)
|
|
|
|
| ty::Infer(_)
|
|
|
|
| ty::Error(_)
|
|
|
|
| ty::Placeholder(_) => {
|
|
|
|
bug!("When Place is Deref it's type shouldn't be {place_ty:#?}")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ProjectionElem::Field(_, _) => match place_ty.ty.kind() {
|
|
|
|
ty::Adt(adt, _) => {
|
|
|
|
if adt.has_dtor(tcx) {
|
|
|
|
self.move_errors.push(MoveError::new(
|
|
|
|
place,
|
|
|
|
location,
|
|
|
|
InteriorOfTypeWithDestructor { container_ty: place_ty.ty },
|
|
|
|
));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2024-01-24 18:01:56 +00:00
|
|
|
ty::Closure(..)
|
|
|
|
| ty::CoroutineClosure(..)
|
|
|
|
| ty::Coroutine(_, _)
|
|
|
|
| ty::Tuple(_) => (),
|
2023-09-30 15:14:07 +00:00
|
|
|
ty::Bool
|
|
|
|
| ty::Char
|
|
|
|
| ty::Int(_)
|
|
|
|
| ty::Uint(_)
|
|
|
|
| ty::Float(_)
|
|
|
|
| ty::Foreign(_)
|
|
|
|
| ty::Str
|
|
|
|
| ty::Array(_, _)
|
2023-02-02 13:57:36 +00:00
|
|
|
| ty::Pat(_, _)
|
2023-09-30 15:14:07 +00:00
|
|
|
| ty::Slice(_)
|
2024-03-21 21:11:06 +00:00
|
|
|
| ty::RawPtr(_, _)
|
2023-09-30 15:14:07 +00:00
|
|
|
| ty::Ref(_, _, _)
|
|
|
|
| ty::FnDef(_, _)
|
2024-08-08 07:18:20 +00:00
|
|
|
| ty::FnPtr(..)
|
2023-09-30 15:14:07 +00:00
|
|
|
| ty::Dynamic(_, _, _)
|
|
|
|
| ty::CoroutineWitness(..)
|
|
|
|
| ty::Never
|
2024-12-21 17:05:40 +00:00
|
|
|
| ty::UnsafeBinder(_)
|
2023-09-30 15:14:07 +00:00
|
|
|
| ty::Alias(_, _)
|
|
|
|
| ty::Param(_)
|
|
|
|
| ty::Bound(_, _)
|
|
|
|
| ty::Infer(_)
|
|
|
|
| ty::Error(_)
|
|
|
|
| ty::Placeholder(_) => bug!(
|
|
|
|
"When Place contains ProjectionElem::Field it's type shouldn't be {place_ty:#?}"
|
|
|
|
),
|
|
|
|
},
|
|
|
|
ProjectionElem::ConstantIndex { .. } | ProjectionElem::Subslice { .. } => {
|
|
|
|
match place_ty.ty.kind() {
|
|
|
|
ty::Slice(_) => {
|
|
|
|
self.move_errors.push(MoveError::new(
|
|
|
|
place,
|
|
|
|
location,
|
2023-10-21 12:10:30 +00:00
|
|
|
InteriorOfSliceOrArray { ty: place_ty.ty, is_index: false },
|
2023-09-30 15:14:07 +00:00
|
|
|
));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ty::Array(_, _) => (),
|
|
|
|
_ => bug!("Unexpected type {:#?}", place_ty.ty),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ProjectionElem::Index(_) => match place_ty.ty.kind() {
|
2023-10-21 12:10:30 +00:00
|
|
|
ty::Array(..) | ty::Slice(..) => {
|
2023-09-30 15:14:07 +00:00
|
|
|
self.move_errors.push(MoveError::new(
|
|
|
|
place,
|
|
|
|
location,
|
|
|
|
InteriorOfSliceOrArray { ty: place_ty.ty, is_index: true },
|
|
|
|
));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_ => bug!("Unexpected type {place_ty:#?}"),
|
|
|
|
},
|
|
|
|
// `OpaqueCast`: only transmutes the type, so no moves there.
|
|
|
|
// `Downcast` : only changes information about a `Place` without moving.
|
|
|
|
// `Subtype` : only transmutes the type, so no moves.
|
|
|
|
// So it's safe to skip these.
|
|
|
|
ProjectionElem::OpaqueCast(_)
|
|
|
|
| ProjectionElem::Subtype(_)
|
|
|
|
| ProjectionElem::Downcast(_, _) => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
place_ty = place_ty.projection_ty(tcx, elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-01 08:01:51 +00:00
|
|
|
fn check_if_full_path_is_moved(
|
2017-11-17 09:47:02 +00:00
|
|
|
&mut self,
|
2019-05-01 21:03:17 +00:00
|
|
|
location: Location,
|
2017-11-17 09:47:02 +00:00
|
|
|
desired_action: InitializationRequiringAction,
|
2020-03-04 21:25:03 +00:00
|
|
|
place_span: (PlaceRef<'tcx>, Span),
|
2024-11-26 03:02:10 +00:00
|
|
|
state: &BorrowckDomain,
|
2017-11-17 09:47:02 +00:00
|
|
|
) {
|
2024-09-13 06:27:24 +00:00
|
|
|
let maybe_uninits = &state.uninits;
|
MIR-borrowck: Big fix to `fn check_if_path_is_moved`.
Fix #44833 (a very specific instance of a very broad bug).
In `check_if_path_is_moved(L)`, check nearest prefix of L with
MovePath, and suffixes of L with MovePaths.
Over the course of review, ariel pointed out a number of issues that
led to this version of the commit:
1. Looking solely at supporting prefixes does not suffice: it
overlooks checking if the path was ever actually initialized in the
first place. So you need to be willing to consider non-supporting
prefixes. Once you are looking at all prefixes, you *could* just
look at the local that forms the base of the projection, but to
handle partial initialization (which still needs to be formally
specified), this code instead looks at the nearest prefix of L that
has an associated MovePath (which, in the limit, will end up being
a local).
2. You also need to consider the suffixes of the given Lvalue, due to
how dataflow is representing partial moves of individual fields out
of struct values.
3. (There was originally a third search, but ariel pointed out that
the first and third could be folded into one.)
Also includes some drive-by refactorings to simplify some method
signatures and prefer `for _ in _` over `loop { }` (at least when it
comes semi-naturally).
2017-10-04 15:46:46 +00:00
|
|
|
|
|
|
|
// Bad scenarios:
|
|
|
|
//
|
|
|
|
// 1. Move of `a.b.c`, use of `a.b.c`
|
|
|
|
// 2. Move of `a.b.c`, use of `a.b.c.d` (without first reinitializing `a.b.c.d`)
|
2018-03-26 20:00:14 +00:00
|
|
|
// 3. Uninitialized `(a.b.c: &_)`, use of `*a.b.c`; note that with
|
MIR-borrowck: Big fix to `fn check_if_path_is_moved`.
Fix #44833 (a very specific instance of a very broad bug).
In `check_if_path_is_moved(L)`, check nearest prefix of L with
MovePath, and suffixes of L with MovePaths.
Over the course of review, ariel pointed out a number of issues that
led to this version of the commit:
1. Looking solely at supporting prefixes does not suffice: it
overlooks checking if the path was ever actually initialized in the
first place. So you need to be willing to consider non-supporting
prefixes. Once you are looking at all prefixes, you *could* just
look at the local that forms the base of the projection, but to
handle partial initialization (which still needs to be formally
specified), this code instead looks at the nearest prefix of L that
has an associated MovePath (which, in the limit, will end up being
a local).
2. You also need to consider the suffixes of the given Lvalue, due to
how dataflow is representing partial moves of individual fields out
of struct values.
3. (There was originally a third search, but ariel pointed out that
the first and third could be folded into one.)
Also includes some drive-by refactorings to simplify some method
signatures and prefer `for _ in _` over `loop { }` (at least when it
comes semi-naturally).
2017-10-04 15:46:46 +00:00
|
|
|
// partial initialization support, one might have `a.x`
|
|
|
|
// initialized but not `a.b`.
|
|
|
|
//
|
|
|
|
// OK scenarios:
|
|
|
|
//
|
2018-03-26 20:00:14 +00:00
|
|
|
// 4. Move of `a.b.c`, use of `a.b.d`
|
|
|
|
// 5. Uninitialized `a.x`, initialized `a.b`, use of `a.b`
|
|
|
|
// 6. Copied `(a.b: &_)`, use of `*(a.b).c`; note that `a.b`
|
MIR-borrowck: Big fix to `fn check_if_path_is_moved`.
Fix #44833 (a very specific instance of a very broad bug).
In `check_if_path_is_moved(L)`, check nearest prefix of L with
MovePath, and suffixes of L with MovePaths.
Over the course of review, ariel pointed out a number of issues that
led to this version of the commit:
1. Looking solely at supporting prefixes does not suffice: it
overlooks checking if the path was ever actually initialized in the
first place. So you need to be willing to consider non-supporting
prefixes. Once you are looking at all prefixes, you *could* just
look at the local that forms the base of the projection, but to
handle partial initialization (which still needs to be formally
specified), this code instead looks at the nearest prefix of L that
has an associated MovePath (which, in the limit, will end up being
a local).
2. You also need to consider the suffixes of the given Lvalue, due to
how dataflow is representing partial moves of individual fields out
of struct values.
3. (There was originally a third search, but ariel pointed out that
the first and third could be folded into one.)
Also includes some drive-by refactorings to simplify some method
signatures and prefer `for _ in _` over `loop { }` (at least when it
comes semi-naturally).
2017-10-04 15:46:46 +00:00
|
|
|
// must have been initialized for the use to be sound.
|
2018-03-26 20:00:14 +00:00
|
|
|
// 7. Move of `a.b.c` then reinit of `a.b.c.d`, use of `a.b.c.d`
|
MIR-borrowck: Big fix to `fn check_if_path_is_moved`.
Fix #44833 (a very specific instance of a very broad bug).
In `check_if_path_is_moved(L)`, check nearest prefix of L with
MovePath, and suffixes of L with MovePaths.
Over the course of review, ariel pointed out a number of issues that
led to this version of the commit:
1. Looking solely at supporting prefixes does not suffice: it
overlooks checking if the path was ever actually initialized in the
first place. So you need to be willing to consider non-supporting
prefixes. Once you are looking at all prefixes, you *could* just
look at the local that forms the base of the projection, but to
handle partial initialization (which still needs to be formally
specified), this code instead looks at the nearest prefix of L that
has an associated MovePath (which, in the limit, will end up being
a local).
2. You also need to consider the suffixes of the given Lvalue, due to
how dataflow is representing partial moves of individual fields out
of struct values.
3. (There was originally a third search, but ariel pointed out that
the first and third could be folded into one.)
Also includes some drive-by refactorings to simplify some method
signatures and prefer `for _ in _` over `loop { }` (at least when it
comes semi-naturally).
2017-10-04 15:46:46 +00:00
|
|
|
|
|
|
|
// The dataflow tracks shallow prefixes distinctly (that is,
|
|
|
|
// field-accesses on P distinctly from P itself), in order to
|
|
|
|
// track substructure initialization separately from the whole
|
|
|
|
// structure.
|
|
|
|
//
|
|
|
|
// E.g., when looking at (*a.b.c).d, if the closest prefix for
|
|
|
|
// which we have a MovePath is `a.b`, then that means that the
|
|
|
|
// initialization state of `a.b` is all we need to inspect to
|
|
|
|
// know if `a.b.c` is valid (and from that we infer that the
|
|
|
|
// dereference and `.d` access is also valid, since we assume
|
2021-08-22 12:46:15 +00:00
|
|
|
// `a.b.c` is assigned a reference to an initialized and
|
MIR-borrowck: Big fix to `fn check_if_path_is_moved`.
Fix #44833 (a very specific instance of a very broad bug).
In `check_if_path_is_moved(L)`, check nearest prefix of L with
MovePath, and suffixes of L with MovePaths.
Over the course of review, ariel pointed out a number of issues that
led to this version of the commit:
1. Looking solely at supporting prefixes does not suffice: it
overlooks checking if the path was ever actually initialized in the
first place. So you need to be willing to consider non-supporting
prefixes. Once you are looking at all prefixes, you *could* just
look at the local that forms the base of the projection, but to
handle partial initialization (which still needs to be formally
specified), this code instead looks at the nearest prefix of L that
has an associated MovePath (which, in the limit, will end up being
a local).
2. You also need to consider the suffixes of the given Lvalue, due to
how dataflow is representing partial moves of individual fields out
of struct values.
3. (There was originally a third search, but ariel pointed out that
the first and third could be folded into one.)
Also includes some drive-by refactorings to simplify some method
signatures and prefer `for _ in _` over `loop { }` (at least when it
comes semi-naturally).
2017-10-04 15:46:46 +00:00
|
|
|
// well-formed record structure.)
|
|
|
|
|
|
|
|
// Therefore, if we seek out the *closest* prefix for which we
|
|
|
|
// have a MovePath, that should capture the initialization
|
2017-12-01 12:39:51 +00:00
|
|
|
// state for the place scenario.
|
MIR-borrowck: Big fix to `fn check_if_path_is_moved`.
Fix #44833 (a very specific instance of a very broad bug).
In `check_if_path_is_moved(L)`, check nearest prefix of L with
MovePath, and suffixes of L with MovePaths.
Over the course of review, ariel pointed out a number of issues that
led to this version of the commit:
1. Looking solely at supporting prefixes does not suffice: it
overlooks checking if the path was ever actually initialized in the
first place. So you need to be willing to consider non-supporting
prefixes. Once you are looking at all prefixes, you *could* just
look at the local that forms the base of the projection, but to
handle partial initialization (which still needs to be formally
specified), this code instead looks at the nearest prefix of L that
has an associated MovePath (which, in the limit, will end up being
a local).
2. You also need to consider the suffixes of the given Lvalue, due to
how dataflow is representing partial moves of individual fields out
of struct values.
3. (There was originally a third search, but ariel pointed out that
the first and third could be folded into one.)
Also includes some drive-by refactorings to simplify some method
signatures and prefer `for _ in _` over `loop { }` (at least when it
comes semi-naturally).
2017-10-04 15:46:46 +00:00
|
|
|
//
|
2018-03-26 20:00:14 +00:00
|
|
|
// This code covers scenarios 1, 2, and 3.
|
MIR-borrowck: Big fix to `fn check_if_path_is_moved`.
Fix #44833 (a very specific instance of a very broad bug).
In `check_if_path_is_moved(L)`, check nearest prefix of L with
MovePath, and suffixes of L with MovePaths.
Over the course of review, ariel pointed out a number of issues that
led to this version of the commit:
1. Looking solely at supporting prefixes does not suffice: it
overlooks checking if the path was ever actually initialized in the
first place. So you need to be willing to consider non-supporting
prefixes. Once you are looking at all prefixes, you *could* just
look at the local that forms the base of the projection, but to
handle partial initialization (which still needs to be formally
specified), this code instead looks at the nearest prefix of L that
has an associated MovePath (which, in the limit, will end up being
a local).
2. You also need to consider the suffixes of the given Lvalue, due to
how dataflow is representing partial moves of individual fields out
of struct values.
3. (There was originally a third search, but ariel pointed out that
the first and third could be folded into one.)
Also includes some drive-by refactorings to simplify some method
signatures and prefer `for _ in _` over `loop { }` (at least when it
comes semi-naturally).
2017-10-04 15:46:46 +00:00
|
|
|
|
2018-09-03 20:50:03 +00:00
|
|
|
debug!("check_if_full_path_is_moved place: {:?}", place_span.0);
|
2019-12-11 13:39:24 +00:00
|
|
|
let (prefix, mpi) = self.move_path_closest_to(place_span.0);
|
|
|
|
if maybe_uninits.contains(mpi) {
|
|
|
|
self.report_use_of_moved_or_uninitialized(
|
|
|
|
location,
|
|
|
|
desired_action,
|
|
|
|
(prefix, place_span.0, place_span.1),
|
|
|
|
mpi,
|
|
|
|
);
|
|
|
|
} // Only query longest prefix with a MovePath, not further
|
|
|
|
// ancestors; dataflow recurs on children when parents
|
|
|
|
// move (to support partial (re)inits).
|
|
|
|
//
|
|
|
|
// (I.e., querying parents breaks scenario 7; but may want
|
|
|
|
// to do such a query based on partial-init feature-gate.)
|
2018-03-26 20:00:14 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 22:03:25 +00:00
|
|
|
/// Subslices correspond to multiple move paths, so we iterate through the
|
|
|
|
/// elements of the base array. For each element we check
|
|
|
|
///
|
|
|
|
/// * Does this element overlap with our slice.
|
|
|
|
/// * Is any part of it uninitialized.
|
|
|
|
fn check_if_subslice_element_is_moved(
|
|
|
|
&mut self,
|
|
|
|
location: Location,
|
|
|
|
desired_action: InitializationRequiringAction,
|
2020-03-04 21:25:03 +00:00
|
|
|
place_span: (PlaceRef<'tcx>, Span),
|
2024-12-05 03:20:05 +00:00
|
|
|
maybe_uninits: &MixedBitSet<MovePathIndex>,
|
2020-08-23 12:54:58 +00:00
|
|
|
from: u64,
|
|
|
|
to: u64,
|
2019-11-22 22:03:25 +00:00
|
|
|
) {
|
|
|
|
if let Some(mpi) = self.move_path_for_place(place_span.0) {
|
2020-01-20 23:18:13 +00:00
|
|
|
let move_paths = &self.move_data.move_paths;
|
2020-01-24 20:38:51 +00:00
|
|
|
|
|
|
|
let root_path = &move_paths[mpi];
|
|
|
|
for (child_mpi, child_move_path) in root_path.children(move_paths) {
|
2020-01-20 23:18:13 +00:00
|
|
|
let last_proj = child_move_path.place.projection.last().unwrap();
|
2019-11-22 22:03:25 +00:00
|
|
|
if let ProjectionElem::ConstantIndex { offset, from_end, .. } = last_proj {
|
|
|
|
debug_assert!(!from_end, "Array constant indexing shouldn't be `from_end`.");
|
|
|
|
|
|
|
|
if (from..to).contains(offset) {
|
2020-01-20 23:18:13 +00:00
|
|
|
let uninit_child =
|
|
|
|
self.move_data.find_in_move_path_or_its_descendants(child_mpi, |mpi| {
|
|
|
|
maybe_uninits.contains(mpi)
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Some(uninit_child) = uninit_child {
|
2019-11-22 22:03:25 +00:00
|
|
|
self.report_use_of_moved_or_uninitialized(
|
|
|
|
location,
|
|
|
|
desired_action,
|
|
|
|
(place_span.0, place_span.0, place_span.1),
|
|
|
|
uninit_child,
|
|
|
|
);
|
|
|
|
return; // don't bother finding other problems.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-26 20:00:14 +00:00
|
|
|
fn check_if_path_or_subpath_is_moved(
|
|
|
|
&mut self,
|
2019-05-01 21:03:17 +00:00
|
|
|
location: Location,
|
2018-03-26 20:00:14 +00:00
|
|
|
desired_action: InitializationRequiringAction,
|
2020-03-04 21:25:03 +00:00
|
|
|
place_span: (PlaceRef<'tcx>, Span),
|
2024-11-26 03:02:10 +00:00
|
|
|
state: &BorrowckDomain,
|
2018-03-26 20:00:14 +00:00
|
|
|
) {
|
2024-09-13 06:27:24 +00:00
|
|
|
let maybe_uninits = &state.uninits;
|
2018-03-26 20:00:14 +00:00
|
|
|
|
|
|
|
// Bad scenarios:
|
|
|
|
//
|
|
|
|
// 1. Move of `a.b.c`, use of `a` or `a.b`
|
|
|
|
// partial initialization support, one might have `a.x`
|
|
|
|
// initialized but not `a.b`.
|
2018-04-01 08:01:51 +00:00
|
|
|
// 2. All bad scenarios from `check_if_full_path_is_moved`
|
2018-03-26 20:00:14 +00:00
|
|
|
//
|
|
|
|
// OK scenarios:
|
|
|
|
//
|
|
|
|
// 3. Move of `a.b.c`, use of `a.b.d`
|
|
|
|
// 4. Uninitialized `a.x`, initialized `a.b`, use of `a.b`
|
|
|
|
// 5. Copied `(a.b: &_)`, use of `*(a.b).c`; note that `a.b`
|
|
|
|
// must have been initialized for the use to be sound.
|
|
|
|
// 6. Move of `a.b.c` then reinit of `a.b.c.d`, use of `a.b.c.d`
|
|
|
|
|
2024-09-13 06:27:24 +00:00
|
|
|
self.check_if_full_path_is_moved(location, desired_action, place_span, state);
|
MIR-borrowck: Big fix to `fn check_if_path_is_moved`.
Fix #44833 (a very specific instance of a very broad bug).
In `check_if_path_is_moved(L)`, check nearest prefix of L with
MovePath, and suffixes of L with MovePaths.
Over the course of review, ariel pointed out a number of issues that
led to this version of the commit:
1. Looking solely at supporting prefixes does not suffice: it
overlooks checking if the path was ever actually initialized in the
first place. So you need to be willing to consider non-supporting
prefixes. Once you are looking at all prefixes, you *could* just
look at the local that forms the base of the projection, but to
handle partial initialization (which still needs to be formally
specified), this code instead looks at the nearest prefix of L that
has an associated MovePath (which, in the limit, will end up being
a local).
2. You also need to consider the suffixes of the given Lvalue, due to
how dataflow is representing partial moves of individual fields out
of struct values.
3. (There was originally a third search, but ariel pointed out that
the first and third could be folded into one.)
Also includes some drive-by refactorings to simplify some method
signatures and prefer `for _ in _` over `loop { }` (at least when it
comes semi-naturally).
2017-10-04 15:46:46 +00:00
|
|
|
|
2021-01-02 18:31:37 +00:00
|
|
|
if let Some((place_base, ProjectionElem::Subslice { from, to, from_end: false })) =
|
|
|
|
place_span.0.last_projection()
|
2019-11-22 22:03:25 +00:00
|
|
|
{
|
2021-01-16 10:34:22 +00:00
|
|
|
let place_ty = place_base.ty(self.body(), self.infcx.tcx);
|
2020-08-02 22:49:11 +00:00
|
|
|
if let ty::Array(..) = place_ty.ty.kind() {
|
2019-11-22 22:03:25 +00:00
|
|
|
self.check_if_subslice_element_is_moved(
|
|
|
|
location,
|
|
|
|
desired_action,
|
2021-01-02 18:31:37 +00:00
|
|
|
(place_base, place_span.1),
|
2019-11-22 22:03:25 +00:00
|
|
|
maybe_uninits,
|
2021-01-02 18:31:37 +00:00
|
|
|
from,
|
|
|
|
to,
|
2019-11-22 22:03:25 +00:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-01 12:39:51 +00:00
|
|
|
// A move of any shallow suffix of `place` also interferes
|
|
|
|
// with an attempt to use `place`. This is scenario 3 above.
|
MIR-borrowck: Big fix to `fn check_if_path_is_moved`.
Fix #44833 (a very specific instance of a very broad bug).
In `check_if_path_is_moved(L)`, check nearest prefix of L with
MovePath, and suffixes of L with MovePaths.
Over the course of review, ariel pointed out a number of issues that
led to this version of the commit:
1. Looking solely at supporting prefixes does not suffice: it
overlooks checking if the path was ever actually initialized in the
first place. So you need to be willing to consider non-supporting
prefixes. Once you are looking at all prefixes, you *could* just
look at the local that forms the base of the projection, but to
handle partial initialization (which still needs to be formally
specified), this code instead looks at the nearest prefix of L that
has an associated MovePath (which, in the limit, will end up being
a local).
2. You also need to consider the suffixes of the given Lvalue, due to
how dataflow is representing partial moves of individual fields out
of struct values.
3. (There was originally a third search, but ariel pointed out that
the first and third could be folded into one.)
Also includes some drive-by refactorings to simplify some method
signatures and prefer `for _ in _` over `loop { }` (at least when it
comes semi-naturally).
2017-10-04 15:46:46 +00:00
|
|
|
//
|
|
|
|
// (Distinct from handling of scenarios 1+2+4 above because
|
2017-12-01 12:39:51 +00:00
|
|
|
// `place` does not interfere with suffixes of its prefixes,
|
2018-11-27 02:59:49 +00:00
|
|
|
// e.g., `a.b.c` does not interfere with `a.b.d`)
|
2018-03-26 20:00:14 +00:00
|
|
|
//
|
|
|
|
// This code covers scenario 1.
|
MIR-borrowck: Big fix to `fn check_if_path_is_moved`.
Fix #44833 (a very specific instance of a very broad bug).
In `check_if_path_is_moved(L)`, check nearest prefix of L with
MovePath, and suffixes of L with MovePaths.
Over the course of review, ariel pointed out a number of issues that
led to this version of the commit:
1. Looking solely at supporting prefixes does not suffice: it
overlooks checking if the path was ever actually initialized in the
first place. So you need to be willing to consider non-supporting
prefixes. Once you are looking at all prefixes, you *could* just
look at the local that forms the base of the projection, but to
handle partial initialization (which still needs to be formally
specified), this code instead looks at the nearest prefix of L that
has an associated MovePath (which, in the limit, will end up being
a local).
2. You also need to consider the suffixes of the given Lvalue, due to
how dataflow is representing partial moves of individual fields out
of struct values.
3. (There was originally a third search, but ariel pointed out that
the first and third could be folded into one.)
Also includes some drive-by refactorings to simplify some method
signatures and prefer `for _ in _` over `loop { }` (at least when it
comes semi-naturally).
2017-10-04 15:46:46 +00:00
|
|
|
|
2018-09-03 20:50:03 +00:00
|
|
|
debug!("check_if_path_or_subpath_is_moved place: {:?}", place_span.0);
|
|
|
|
if let Some(mpi) = self.move_path_for_place(place_span.0) {
|
2020-01-20 23:18:13 +00:00
|
|
|
let uninit_mpi = self
|
|
|
|
.move_data
|
|
|
|
.find_in_move_path_or_its_descendants(mpi, |mpi| maybe_uninits.contains(mpi));
|
|
|
|
|
|
|
|
if let Some(uninit_mpi) = uninit_mpi {
|
2017-11-17 09:47:02 +00:00
|
|
|
self.report_use_of_moved_or_uninitialized(
|
2019-05-01 21:03:17 +00:00
|
|
|
location,
|
2017-11-17 09:47:02 +00:00
|
|
|
desired_action,
|
2018-10-09 21:14:31 +00:00
|
|
|
(place_span.0, place_span.0, place_span.1),
|
2020-01-20 23:18:13 +00:00
|
|
|
uninit_mpi,
|
2017-11-17 09:47:02 +00:00
|
|
|
);
|
MIR-borrowck: Big fix to `fn check_if_path_is_moved`.
Fix #44833 (a very specific instance of a very broad bug).
In `check_if_path_is_moved(L)`, check nearest prefix of L with
MovePath, and suffixes of L with MovePaths.
Over the course of review, ariel pointed out a number of issues that
led to this version of the commit:
1. Looking solely at supporting prefixes does not suffice: it
overlooks checking if the path was ever actually initialized in the
first place. So you need to be willing to consider non-supporting
prefixes. Once you are looking at all prefixes, you *could* just
look at the local that forms the base of the projection, but to
handle partial initialization (which still needs to be formally
specified), this code instead looks at the nearest prefix of L that
has an associated MovePath (which, in the limit, will end up being
a local).
2. You also need to consider the suffixes of the given Lvalue, due to
how dataflow is representing partial moves of individual fields out
of struct values.
3. (There was originally a third search, but ariel pointed out that
the first and third could be folded into one.)
Also includes some drive-by refactorings to simplify some method
signatures and prefer `for _ in _` over `loop { }` (at least when it
comes semi-naturally).
2017-10-04 15:46:46 +00:00
|
|
|
return; // don't bother finding other problems.
|
2017-07-05 12:52:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-01 12:39:51 +00:00
|
|
|
/// Currently MoveData does not store entries for all places in
|
MIR-borrowck: Big fix to `fn check_if_path_is_moved`.
Fix #44833 (a very specific instance of a very broad bug).
In `check_if_path_is_moved(L)`, check nearest prefix of L with
MovePath, and suffixes of L with MovePaths.
Over the course of review, ariel pointed out a number of issues that
led to this version of the commit:
1. Looking solely at supporting prefixes does not suffice: it
overlooks checking if the path was ever actually initialized in the
first place. So you need to be willing to consider non-supporting
prefixes. Once you are looking at all prefixes, you *could* just
look at the local that forms the base of the projection, but to
handle partial initialization (which still needs to be formally
specified), this code instead looks at the nearest prefix of L that
has an associated MovePath (which, in the limit, will end up being
a local).
2. You also need to consider the suffixes of the given Lvalue, due to
how dataflow is representing partial moves of individual fields out
of struct values.
3. (There was originally a third search, but ariel pointed out that
the first and third could be folded into one.)
Also includes some drive-by refactorings to simplify some method
signatures and prefer `for _ in _` over `loop { }` (at least when it
comes semi-naturally).
2017-10-04 15:46:46 +00:00
|
|
|
/// the input MIR. For example it will currently filter out
|
2017-12-01 12:39:51 +00:00
|
|
|
/// places that are Copy; thus we do not track places of shared
|
|
|
|
/// reference type. This routine will walk up a place along its
|
|
|
|
/// prefixes, searching for a foundational place that *is*
|
MIR-borrowck: Big fix to `fn check_if_path_is_moved`.
Fix #44833 (a very specific instance of a very broad bug).
In `check_if_path_is_moved(L)`, check nearest prefix of L with
MovePath, and suffixes of L with MovePaths.
Over the course of review, ariel pointed out a number of issues that
led to this version of the commit:
1. Looking solely at supporting prefixes does not suffice: it
overlooks checking if the path was ever actually initialized in the
first place. So you need to be willing to consider non-supporting
prefixes. Once you are looking at all prefixes, you *could* just
look at the local that forms the base of the projection, but to
handle partial initialization (which still needs to be formally
specified), this code instead looks at the nearest prefix of L that
has an associated MovePath (which, in the limit, will end up being
a local).
2. You also need to consider the suffixes of the given Lvalue, due to
how dataflow is representing partial moves of individual fields out
of struct values.
3. (There was originally a third search, but ariel pointed out that
the first and third could be folded into one.)
Also includes some drive-by refactorings to simplify some method
signatures and prefer `for _ in _` over `loop { }` (at least when it
comes semi-naturally).
2017-10-04 15:46:46 +00:00
|
|
|
/// tracked in the MoveData.
|
|
|
|
///
|
|
|
|
/// An Err result includes a tag indicated why the search failed.
|
2018-02-16 14:56:50 +00:00
|
|
|
/// Currently this can only occur if the place is built off of a
|
MIR-borrowck: Big fix to `fn check_if_path_is_moved`.
Fix #44833 (a very specific instance of a very broad bug).
In `check_if_path_is_moved(L)`, check nearest prefix of L with
MovePath, and suffixes of L with MovePaths.
Over the course of review, ariel pointed out a number of issues that
led to this version of the commit:
1. Looking solely at supporting prefixes does not suffice: it
overlooks checking if the path was ever actually initialized in the
first place. So you need to be willing to consider non-supporting
prefixes. Once you are looking at all prefixes, you *could* just
look at the local that forms the base of the projection, but to
handle partial initialization (which still needs to be formally
specified), this code instead looks at the nearest prefix of L that
has an associated MovePath (which, in the limit, will end up being
a local).
2. You also need to consider the suffixes of the given Lvalue, due to
how dataflow is representing partial moves of individual fields out
of struct values.
3. (There was originally a third search, but ariel pointed out that
the first and third could be folded into one.)
Also includes some drive-by refactorings to simplify some method
signatures and prefer `for _ in _` over `loop { }` (at least when it
comes semi-naturally).
2017-10-04 15:46:46 +00:00
|
|
|
/// static variable, as we do not track those in the MoveData.
|
2020-03-04 21:25:03 +00:00
|
|
|
fn move_path_closest_to(&mut self, place: PlaceRef<'tcx>) -> (PlaceRef<'tcx>, MovePathIndex) {
|
2019-11-22 22:08:03 +00:00
|
|
|
match self.move_data.rev_lookup.find(place) {
|
|
|
|
LookupResult::Parent(Some(mpi)) | LookupResult::Exact(mpi) => {
|
2019-12-11 13:39:24 +00:00
|
|
|
(self.move_data.move_paths[mpi].place.as_ref(), mpi)
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
2019-12-11 13:39:24 +00:00
|
|
|
LookupResult::Parent(None) => panic!("should have move path for every Local"),
|
MIR-borrowck: Big fix to `fn check_if_path_is_moved`.
Fix #44833 (a very specific instance of a very broad bug).
In `check_if_path_is_moved(L)`, check nearest prefix of L with
MovePath, and suffixes of L with MovePaths.
Over the course of review, ariel pointed out a number of issues that
led to this version of the commit:
1. Looking solely at supporting prefixes does not suffice: it
overlooks checking if the path was ever actually initialized in the
first place. So you need to be willing to consider non-supporting
prefixes. Once you are looking at all prefixes, you *could* just
look at the local that forms the base of the projection, but to
handle partial initialization (which still needs to be formally
specified), this code instead looks at the nearest prefix of L that
has an associated MovePath (which, in the limit, will end up being
a local).
2. You also need to consider the suffixes of the given Lvalue, due to
how dataflow is representing partial moves of individual fields out
of struct values.
3. (There was originally a third search, but ariel pointed out that
the first and third could be folded into one.)
Also includes some drive-by refactorings to simplify some method
signatures and prefer `for _ in _` over `loop { }` (at least when it
comes semi-naturally).
2017-10-04 15:46:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 21:25:03 +00:00
|
|
|
fn move_path_for_place(&mut self, place: PlaceRef<'tcx>) -> Option<MovePathIndex> {
|
2017-07-05 12:52:18 +00:00
|
|
|
// If returns None, then there is no move path corresponding
|
2017-12-01 12:39:51 +00:00
|
|
|
// to a direct owner of `place` (which means there is nothing
|
2017-07-05 12:52:18 +00:00
|
|
|
// that borrowck tracks for its analysis).
|
|
|
|
|
2017-12-01 12:39:51 +00:00
|
|
|
match self.move_data.rev_lookup.find(place) {
|
2017-07-05 12:52:18 +00:00
|
|
|
LookupResult::Parent(_) => None,
|
|
|
|
LookupResult::Exact(mpi) => Some(mpi),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 09:47:02 +00:00
|
|
|
fn check_if_assigned_path_is_moved(
|
|
|
|
&mut self,
|
2019-05-01 21:03:17 +00:00
|
|
|
location: Location,
|
2020-03-30 20:49:33 +00:00
|
|
|
(place, span): (Place<'tcx>, Span),
|
2024-11-26 03:02:10 +00:00
|
|
|
state: &BorrowckDomain,
|
2017-11-17 09:47:02 +00:00
|
|
|
) {
|
2018-03-26 20:00:14 +00:00
|
|
|
debug!("check_if_assigned_path_is_moved place: {:?}", place);
|
2019-04-30 16:58:24 +00:00
|
|
|
|
|
|
|
// None case => assigning to `x` does not require `x` be initialized.
|
2021-01-02 18:31:37 +00:00
|
|
|
for (place_base, elem) in place.iter_projections().rev() {
|
2019-07-29 22:07:28 +00:00
|
|
|
match elem {
|
2019-04-30 16:58:24 +00:00
|
|
|
ProjectionElem::Index(_/*operand*/) |
|
2023-08-16 05:43:30 +00:00
|
|
|
ProjectionElem::Subtype(_) |
|
2022-07-27 11:58:34 +00:00
|
|
|
ProjectionElem::OpaqueCast(_) |
|
2019-04-30 16:58:24 +00:00
|
|
|
ProjectionElem::ConstantIndex { .. } |
|
|
|
|
// assigning to P[i] requires P to be valid.
|
|
|
|
ProjectionElem::Downcast(_/*adt_def*/, _/*variant_idx*/) =>
|
|
|
|
// assigning to (P->variant) is okay if assigning to `P` is okay
|
|
|
|
//
|
2021-08-22 14:20:58 +00:00
|
|
|
// FIXME: is this true even if P is an adt with a dtor?
|
2019-04-30 16:58:24 +00:00
|
|
|
{ }
|
|
|
|
|
|
|
|
// assigning to (*P) requires P to be initialized
|
|
|
|
ProjectionElem::Deref => {
|
|
|
|
self.check_if_full_path_is_moved(
|
|
|
|
location, InitializationRequiringAction::Use,
|
2024-09-13 06:27:24 +00:00
|
|
|
(place_base, span), state);
|
2019-04-30 16:58:24 +00:00
|
|
|
// (base initialized; no need to
|
|
|
|
// recur further)
|
2017-07-05 12:52:18 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-04-30 16:58:24 +00:00
|
|
|
|
|
|
|
ProjectionElem::Subslice { .. } => {
|
2023-07-25 21:17:39 +00:00
|
|
|
panic!("we don't allow assignments to subslices, location: {location:?}");
|
2019-04-30 16:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ProjectionElem::Field(..) => {
|
|
|
|
// if type of `P` has a dtor, then
|
|
|
|
// assigning to `P.f` requires `P` itself
|
|
|
|
// be already initialized
|
|
|
|
let tcx = self.infcx.tcx;
|
2021-01-16 10:34:22 +00:00
|
|
|
let base_ty = place_base.ty(self.body(), tcx).ty;
|
2020-08-02 22:49:11 +00:00
|
|
|
match base_ty.kind() {
|
2019-04-30 16:58:24 +00:00
|
|
|
ty::Adt(def, _) if def.has_dtor(tcx) => {
|
|
|
|
self.check_if_path_or_subpath_is_moved(
|
|
|
|
location, InitializationRequiringAction::Assignment,
|
2024-09-13 06:27:24 +00:00
|
|
|
(place_base, span), state);
|
2019-04-30 16:58:24 +00:00
|
|
|
|
2018-04-01 08:01:51 +00:00
|
|
|
// (base initialized; no need to
|
|
|
|
// recur further)
|
|
|
|
break;
|
2018-03-26 20:00:14 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 16:58:24 +00:00
|
|
|
// Once `let s; s.x = V; read(s.x);`,
|
|
|
|
// is allowed, remove this match arm.
|
|
|
|
ty::Adt(..) | ty::Tuple(..) => {
|
2024-09-13 06:27:24 +00:00
|
|
|
check_parent_of_field(self, location, place_base, span, state);
|
2017-07-05 12:52:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 16:58:24 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
2017-07-05 12:52:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-21 19:15:04 +00:00
|
|
|
|
2024-09-05 23:18:17 +00:00
|
|
|
fn check_parent_of_field<'a, 'tcx>(
|
|
|
|
this: &mut MirBorrowckCtxt<'a, '_, 'tcx>,
|
2019-05-01 21:03:17 +00:00
|
|
|
location: Location,
|
2020-03-04 21:25:03 +00:00
|
|
|
base: PlaceRef<'tcx>,
|
2018-11-03 15:54:35 +00:00
|
|
|
span: Span,
|
2024-11-26 03:02:10 +00:00
|
|
|
state: &BorrowckDomain,
|
2018-11-03 15:54:35 +00:00
|
|
|
) {
|
2018-10-09 21:14:31 +00:00
|
|
|
// rust-lang/rust#21232: Until Rust allows reads from the
|
|
|
|
// initialized parts of partially initialized structs, we
|
|
|
|
// will, starting with the 2018 edition, reject attempts
|
|
|
|
// to write to structs that are not fully initialized.
|
|
|
|
//
|
|
|
|
// In other words, *until* we allow this:
|
|
|
|
//
|
|
|
|
// 1. `let mut s; s.x = Val; read(s.x);`
|
|
|
|
//
|
|
|
|
// we will for now disallow this:
|
|
|
|
//
|
|
|
|
// 2. `let mut s; s.x = Val;`
|
|
|
|
//
|
|
|
|
// and also this:
|
|
|
|
//
|
|
|
|
// 3. `let mut s = ...; drop(s); s.x=Val;`
|
|
|
|
//
|
|
|
|
// This does not use check_if_path_or_subpath_is_moved,
|
|
|
|
// because we want to *allow* reinitializations of fields:
|
2018-11-27 02:59:49 +00:00
|
|
|
// e.g., want to allow
|
2018-10-09 21:14:31 +00:00
|
|
|
//
|
|
|
|
// `let mut s = ...; drop(s.x); s.x=Val;`
|
|
|
|
//
|
|
|
|
// This does not use check_if_full_path_is_moved on
|
|
|
|
// `base`, because that would report an error about the
|
|
|
|
// `base` as a whole, but in this scenario we *really*
|
|
|
|
// want to report an error about the actual thing that was
|
|
|
|
// moved, which may be some prefix of `base`.
|
|
|
|
|
|
|
|
// Shallow so that we'll stop at any dereference; we'll
|
|
|
|
// report errors about issues with such bases elsewhere.
|
2024-09-13 06:27:24 +00:00
|
|
|
let maybe_uninits = &state.uninits;
|
2018-10-09 21:14:31 +00:00
|
|
|
|
|
|
|
// Find the shortest uninitialized prefix you can reach
|
|
|
|
// without going over a Deref.
|
|
|
|
let mut shortest_uninit_seen = None;
|
2019-07-11 17:25:37 +00:00
|
|
|
for prefix in this.prefixes(base, PrefixSet::Shallow) {
|
2022-02-18 23:48:49 +00:00
|
|
|
let Some(mpi) = this.move_path_for_place(prefix) else { continue };
|
2018-10-09 21:14:31 +00:00
|
|
|
|
|
|
|
if maybe_uninits.contains(mpi) {
|
|
|
|
debug!(
|
|
|
|
"check_parent_of_field updating shortest_uninit_seen from {:?} to {:?}",
|
|
|
|
shortest_uninit_seen,
|
|
|
|
Some((prefix, mpi))
|
|
|
|
);
|
|
|
|
shortest_uninit_seen = Some((prefix, mpi));
|
|
|
|
} else {
|
|
|
|
debug!("check_parent_of_field {:?} is definitely initialized", (prefix, mpi));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some((prefix, mpi)) = shortest_uninit_seen {
|
2021-08-22 15:27:18 +00:00
|
|
|
// Check for a reassignment into an uninitialized field of a union (for example,
|
2021-08-22 12:46:15 +00:00
|
|
|
// after a move out). In this case, do not report an error here. There is an
|
2018-11-03 15:54:35 +00:00
|
|
|
// exception, if this is the first assignment into the union (that is, there is
|
|
|
|
// no move out from an earlier location) then this is an attempt at initialization
|
|
|
|
// of the union - we should error in that case.
|
|
|
|
let tcx = this.infcx.tcx;
|
2024-09-11 17:32:53 +00:00
|
|
|
if base.ty(this.body(), tcx).ty.is_union()
|
|
|
|
&& this.move_data.path_map[mpi].iter().any(|moi| {
|
2021-06-02 00:00:00 +00:00
|
|
|
this.move_data.moves[*moi].source.is_predecessor_of(location, this.body)
|
2024-09-11 17:32:53 +00:00
|
|
|
})
|
|
|
|
{
|
|
|
|
return;
|
2018-11-03 15:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 21:14:31 +00:00
|
|
|
this.report_use_of_moved_or_uninitialized(
|
2019-05-01 21:03:17 +00:00
|
|
|
location,
|
2018-10-09 21:14:31 +00:00
|
|
|
InitializationRequiringAction::PartialAssignment,
|
2019-07-11 17:25:37 +00:00
|
|
|
(prefix, base, span),
|
2018-10-09 21:14:31 +00:00
|
|
|
mpi,
|
|
|
|
);
|
2023-04-28 17:35:40 +00:00
|
|
|
|
|
|
|
// rust-lang/rust#21232, #54499, #54986: during period where we reject
|
|
|
|
// partial initialization, do not complain about unnecessary `mut` on
|
|
|
|
// an attempt to do a partial initialization.
|
|
|
|
this.used_mut.insert(base.local);
|
2018-10-09 21:14:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-15 14:11:29 +00:00
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Checks the permissions for the given place and read or write kind
|
2017-11-16 16:44:24 +00:00
|
|
|
///
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Returns `true` if an error is reported.
|
2017-11-17 09:47:02 +00:00
|
|
|
fn check_access_permissions(
|
2018-02-28 09:09:08 +00:00
|
|
|
&mut self,
|
2020-03-30 20:49:33 +00:00
|
|
|
(place, span): (Place<'tcx>, Span),
|
2017-11-17 09:47:02 +00:00
|
|
|
kind: ReadOrWrite,
|
|
|
|
is_local_mutation_allowed: LocalMutationIsAllowed,
|
2024-11-26 03:02:10 +00:00
|
|
|
state: &BorrowckDomain,
|
2018-07-15 14:11:29 +00:00
|
|
|
location: Location,
|
2017-11-17 09:47:02 +00:00
|
|
|
) -> bool {
|
|
|
|
debug!(
|
2018-10-16 14:46:04 +00:00
|
|
|
"check_access_permissions({:?}, {:?}, is_local_mutation_allowed: {:?})",
|
2018-03-06 05:29:03 +00:00
|
|
|
place, kind, is_local_mutation_allowed
|
2017-11-17 09:47:02 +00:00
|
|
|
);
|
2018-06-11 17:11:48 +00:00
|
|
|
|
2018-06-12 16:00:27 +00:00
|
|
|
let error_access;
|
|
|
|
let the_place_err;
|
|
|
|
|
2017-10-21 19:15:04 +00:00
|
|
|
match kind {
|
2023-05-31 11:13:13 +00:00
|
|
|
Reservation(WriteKind::MutableBorrow(BorrowKind::Mut { kind: mut_borrow_kind }))
|
|
|
|
| Write(WriteKind::MutableBorrow(BorrowKind::Mut { kind: mut_borrow_kind })) => {
|
|
|
|
let is_local_mutation_allowed = match mut_borrow_kind {
|
2023-09-28 08:15:41 +00:00
|
|
|
// `ClosureCapture` is used for mutable variable with an immutable binding.
|
2024-10-04 00:24:34 +00:00
|
|
|
// This is only behaviour difference between `ClosureCapture` and mutable
|
|
|
|
// borrows.
|
2023-05-31 11:13:13 +00:00
|
|
|
MutBorrowKind::ClosureCapture => LocalMutationIsAllowed::Yes,
|
|
|
|
MutBorrowKind::Default | MutBorrowKind::TwoPhaseBorrow => {
|
|
|
|
is_local_mutation_allowed
|
2023-05-30 22:25:27 +00:00
|
|
|
}
|
2018-05-07 13:58:09 +00:00
|
|
|
};
|
2019-08-01 21:41:37 +00:00
|
|
|
match self.is_mutable(place.as_ref(), is_local_mutation_allowed) {
|
2018-06-11 17:11:48 +00:00
|
|
|
Ok(root_place) => {
|
2024-09-13 06:27:24 +00:00
|
|
|
self.add_used_mut(root_place, state);
|
2018-06-11 17:11:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-03-16 09:52:07 +00:00
|
|
|
Err(place_err) => {
|
2018-06-12 16:00:27 +00:00
|
|
|
error_access = AccessKind::MutableBorrow;
|
|
|
|
the_place_err = place_err;
|
2017-10-21 19:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-17 09:47:02 +00:00
|
|
|
}
|
2018-03-16 09:52:07 +00:00
|
|
|
Reservation(WriteKind::Mutate) | Write(WriteKind::Mutate) => {
|
2019-08-01 21:41:37 +00:00
|
|
|
match self.is_mutable(place.as_ref(), is_local_mutation_allowed) {
|
2018-06-11 17:11:48 +00:00
|
|
|
Ok(root_place) => {
|
2024-09-13 06:27:24 +00:00
|
|
|
self.add_used_mut(root_place, state);
|
2018-06-11 17:11:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-03-16 09:52:07 +00:00
|
|
|
Err(place_err) => {
|
2018-06-12 16:00:27 +00:00
|
|
|
error_access = AccessKind::Mutate;
|
|
|
|
the_place_err = place_err;
|
2018-03-16 09:52:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-12 16:00:27 +00:00
|
|
|
|
2020-04-17 00:38:52 +00:00
|
|
|
Reservation(
|
|
|
|
WriteKind::Move
|
2023-05-25 17:30:23 +00:00
|
|
|
| WriteKind::Replace
|
2020-04-17 00:38:52 +00:00
|
|
|
| WriteKind::StorageDeadOrDrop
|
|
|
|
| WriteKind::MutableBorrow(BorrowKind::Shared)
|
2024-04-06 22:30:28 +00:00
|
|
|
| WriteKind::MutableBorrow(BorrowKind::Fake(_)),
|
2020-04-17 00:38:52 +00:00
|
|
|
)
|
|
|
|
| Write(
|
|
|
|
WriteKind::Move
|
2023-05-25 17:30:23 +00:00
|
|
|
| WriteKind::Replace
|
2020-04-17 00:38:52 +00:00
|
|
|
| WriteKind::StorageDeadOrDrop
|
|
|
|
| WriteKind::MutableBorrow(BorrowKind::Shared)
|
2024-04-06 22:30:28 +00:00
|
|
|
| WriteKind::MutableBorrow(BorrowKind::Fake(_)),
|
2020-04-17 00:38:52 +00:00
|
|
|
) => {
|
2022-02-08 06:37:32 +00:00
|
|
|
if self.is_mutable(place.as_ref(), is_local_mutation_allowed).is_err()
|
2024-02-02 02:04:07 +00:00
|
|
|
&& !self.has_buffered_diags()
|
2022-02-08 06:37:32 +00:00
|
|
|
{
|
2019-10-02 17:12:57 +00:00
|
|
|
// rust-lang/rust#46908: In pure NLL mode this code path should be
|
2023-11-30 04:01:11 +00:00
|
|
|
// unreachable, but we use `span_delayed_bug` because we can hit this when
|
2019-10-02 17:12:57 +00:00
|
|
|
// dereferencing a non-Copy raw pointer *and* have `-Ztreat-err-as-bug`
|
|
|
|
// enabled. We don't want to ICE for that case, as other errors will have
|
|
|
|
// been emitted (#52262).
|
2023-12-18 11:21:37 +00:00
|
|
|
self.dcx().span_delayed_bug(
|
2019-10-02 17:12:57 +00:00
|
|
|
span,
|
Restrict `From<S>` for `{D,Subd}iagnosticMessage`.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that
impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static,
str>`, which are reasonable. It also includes `&String`, which is pretty
weird, and results in many places making unnecessary allocations for
patterns like this:
```
self.fatal(&format!(...))
```
This creates a string with `format!`, takes a reference, passes the
reference to `fatal`, which does an `into()`, which clones the
reference, doing a second allocation. Two allocations for a single
string, bleh.
This commit changes the `From` impls so that you can only create a
`{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static,
str>`. This requires changing all the places that currently create one
from a `&String`. Most of these are of the `&format!(...)` form
described above; each one removes an unnecessary static `&`, plus an
allocation when executed. There are also a few places where the existing
use of `&String` was more reasonable; these now just use `clone()` at
the call site.
As well as making the code nicer and more efficient, this is a step
towards possibly using `Cow<'static, str>` in
`{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing
the `From<&'a str>` impls to `From<&'static str>`, which is doable, but
I'm not yet sure if it's worthwhile.
2023-04-20 03:26:58 +00:00
|
|
|
format!(
|
2023-07-25 21:17:39 +00:00
|
|
|
"Accessing `{place:?}` with the kind `{kind:?}` shouldn't be possible",
|
2019-09-25 23:22:16 +00:00
|
|
|
),
|
2019-10-02 17:12:57 +00:00
|
|
|
);
|
2017-11-16 16:44:24 +00:00
|
|
|
}
|
2018-06-11 17:11:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Activation(..) => {
|
|
|
|
// permission checks are done at Reservation point.
|
|
|
|
return false;
|
2017-11-17 09:47:02 +00:00
|
|
|
}
|
2020-04-17 00:38:52 +00:00
|
|
|
Read(
|
2024-04-06 22:30:28 +00:00
|
|
|
ReadKind::Borrow(BorrowKind::Mut { .. } | BorrowKind::Shared | BorrowKind::Fake(_))
|
2020-04-17 00:38:52 +00:00
|
|
|
| ReadKind::Copy,
|
|
|
|
) => {
|
2018-06-11 17:11:48 +00:00
|
|
|
// Access authorized
|
|
|
|
return false;
|
|
|
|
}
|
2017-10-21 19:15:04 +00:00
|
|
|
}
|
2018-06-12 16:00:27 +00:00
|
|
|
|
2019-11-19 02:31:40 +00:00
|
|
|
// rust-lang/rust#21232, #54986: during period where we reject
|
|
|
|
// partial initialization, do not complain about mutability
|
|
|
|
// errors except for actual mutation (as opposed to an attempt
|
|
|
|
// to do a partial initialization).
|
2024-09-13 06:27:24 +00:00
|
|
|
let previously_initialized = self.is_local_ever_initialized(place.local, state);
|
2019-11-19 02:31:40 +00:00
|
|
|
|
2018-06-12 16:00:27 +00:00
|
|
|
// at this point, we have set up the error reporting state.
|
2022-12-31 12:45:22 +00:00
|
|
|
if let Some(init_index) = previously_initialized {
|
|
|
|
if let (AccessKind::Mutate, Some(_)) = (error_access, place.as_local()) {
|
|
|
|
// If this is a mutate access to an immutable local variable with no projections
|
|
|
|
// report the error as an illegal reassignment
|
|
|
|
let init = &self.move_data.inits[init_index];
|
2023-11-21 19:07:32 +00:00
|
|
|
let assigned_span = init.span(self.body);
|
2024-02-11 06:47:30 +00:00
|
|
|
self.report_illegal_reassignment((place, span), assigned_span, place);
|
2022-12-31 12:45:22 +00:00
|
|
|
} else {
|
|
|
|
self.report_mutability_error(place, span, the_place_err, error_access, location)
|
|
|
|
}
|
2018-10-16 13:25:18 +00:00
|
|
|
true
|
2018-10-16 14:46:04 +00:00
|
|
|
} else {
|
2018-10-16 13:25:18 +00:00
|
|
|
false
|
2019-11-19 02:31:40 +00:00
|
|
|
}
|
2017-10-21 19:15:04 +00:00
|
|
|
}
|
|
|
|
|
2024-11-26 03:02:10 +00:00
|
|
|
fn is_local_ever_initialized(&self, local: Local, state: &BorrowckDomain) -> Option<InitIndex> {
|
2023-10-12 17:19:19 +00:00
|
|
|
let mpi = self.move_data.rev_lookup.find_local(local)?;
|
2018-10-16 13:45:24 +00:00
|
|
|
let ii = &self.move_data.init_path_map[mpi];
|
2024-09-13 06:27:24 +00:00
|
|
|
ii.into_iter().find(|&&index| state.ever_inits.contains(index)).copied()
|
2018-10-16 13:45:24 +00:00
|
|
|
}
|
|
|
|
|
2018-04-22 06:41:44 +00:00
|
|
|
/// Adds the place into the used mutable variables set
|
2024-11-26 03:02:10 +00:00
|
|
|
fn add_used_mut(&mut self, root_place: RootPlace<'tcx>, state: &BorrowckDomain) {
|
2018-04-22 06:41:44 +00:00
|
|
|
match root_place {
|
2019-12-11 19:50:03 +00:00
|
|
|
RootPlace { place_local: local, place_projection: [], is_local_mutation_allowed } => {
|
2018-10-16 13:45:24 +00:00
|
|
|
// If the local may have been initialized, and it is now currently being
|
|
|
|
// mutated, then it is justified to be annotated with the `mut`
|
|
|
|
// keyword, since the mutation may be a possible reassignment.
|
|
|
|
if is_local_mutation_allowed != LocalMutationIsAllowed::Yes
|
2024-09-13 06:27:24 +00:00
|
|
|
&& self.is_local_ever_initialized(local, state).is_some()
|
2018-10-16 13:45:24 +00:00
|
|
|
{
|
2020-01-14 05:10:05 +00:00
|
|
|
self.used_mut.insert(local);
|
2018-04-22 06:41:44 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-03 19:12:09 +00:00
|
|
|
RootPlace {
|
2019-12-11 19:50:03 +00:00
|
|
|
place_local: _,
|
2019-04-30 16:58:24 +00:00
|
|
|
place_projection: _,
|
2018-07-03 19:12:09 +00:00
|
|
|
is_local_mutation_allowed: LocalMutationIsAllowed::Yes,
|
|
|
|
} => {}
|
2018-04-22 06:41:44 +00:00
|
|
|
RootPlace {
|
2019-12-11 19:50:03 +00:00
|
|
|
place_local,
|
2019-07-29 22:07:28 +00:00
|
|
|
place_projection: place_projection @ [.., _],
|
2018-04-22 06:41:44 +00:00
|
|
|
is_local_mutation_allowed: _,
|
|
|
|
} => {
|
2019-07-11 17:25:37 +00:00
|
|
|
if let Some(field) = self.is_upvar_field_projection(PlaceRef {
|
2019-12-11 19:50:03 +00:00
|
|
|
local: place_local,
|
|
|
|
projection: place_projection,
|
2019-04-30 16:58:24 +00:00
|
|
|
}) {
|
2018-04-22 06:41:44 +00:00
|
|
|
self.used_mut_upvars.push(field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 13:50:44 +00:00
|
|
|
/// Whether this value can be written or borrowed mutably.
|
2018-04-22 06:41:44 +00:00
|
|
|
/// Returns the root place if the place passed in is a projection.
|
2020-03-03 20:22:54 +00:00
|
|
|
fn is_mutable(
|
2017-11-17 09:47:02 +00:00
|
|
|
&self,
|
2020-03-04 21:25:03 +00:00
|
|
|
place: PlaceRef<'tcx>,
|
2017-11-17 09:47:02 +00:00
|
|
|
is_local_mutation_allowed: LocalMutationIsAllowed,
|
2020-03-04 21:25:03 +00:00
|
|
|
) -> Result<RootPlace<'tcx>, PlaceRef<'tcx>> {
|
2021-01-12 19:54:12 +00:00
|
|
|
debug!("is_mutable: place={:?}, is_local...={:?}", place, is_local_mutation_allowed);
|
2021-01-02 18:31:37 +00:00
|
|
|
match place.last_projection() {
|
|
|
|
None => {
|
|
|
|
let local = &self.body.local_decls[place.local];
|
2017-10-21 19:15:04 +00:00
|
|
|
match local.mutability {
|
2017-11-17 09:47:02 +00:00
|
|
|
Mutability::Not => match is_local_mutation_allowed {
|
2018-06-22 03:10:52 +00:00
|
|
|
LocalMutationIsAllowed::Yes => Ok(RootPlace {
|
2019-12-11 19:50:03 +00:00
|
|
|
place_local: place.local,
|
2019-08-01 21:41:37 +00:00
|
|
|
place_projection: place.projection,
|
2018-06-22 03:10:52 +00:00
|
|
|
is_local_mutation_allowed: LocalMutationIsAllowed::Yes,
|
|
|
|
}),
|
|
|
|
LocalMutationIsAllowed::ExceptUpvars => Ok(RootPlace {
|
2019-12-11 19:50:03 +00:00
|
|
|
place_local: place.local,
|
2019-08-01 21:41:37 +00:00
|
|
|
place_projection: place.projection,
|
2018-06-22 03:10:52 +00:00
|
|
|
is_local_mutation_allowed: LocalMutationIsAllowed::ExceptUpvars,
|
|
|
|
}),
|
2019-08-01 21:41:37 +00:00
|
|
|
LocalMutationIsAllowed::No => Err(place),
|
2017-11-17 09:47:02 +00:00
|
|
|
},
|
2018-06-22 03:10:52 +00:00
|
|
|
Mutability::Mut => Ok(RootPlace {
|
2019-12-11 19:50:03 +00:00
|
|
|
place_local: place.local,
|
2019-08-01 21:41:37 +00:00
|
|
|
place_projection: place.projection,
|
2018-06-22 03:10:52 +00:00
|
|
|
is_local_mutation_allowed,
|
|
|
|
}),
|
2017-10-21 19:15:04 +00:00
|
|
|
}
|
2017-11-17 09:47:02 +00:00
|
|
|
}
|
2021-01-02 18:31:37 +00:00
|
|
|
Some((place_base, elem)) => {
|
2019-07-29 22:07:28 +00:00
|
|
|
match elem {
|
2017-10-21 19:15:04 +00:00
|
|
|
ProjectionElem::Deref => {
|
2021-01-16 10:34:22 +00:00
|
|
|
let base_ty = place_base.ty(self.body(), self.infcx.tcx).ty;
|
2017-10-21 19:15:04 +00:00
|
|
|
|
2017-11-30 23:18:38 +00:00
|
|
|
// Check the kind of deref to decide
|
2020-08-02 22:49:11 +00:00
|
|
|
match base_ty.kind() {
|
2018-08-22 00:35:02 +00:00
|
|
|
ty::Ref(_, _, mutbl) => {
|
2018-05-02 13:21:05 +00:00
|
|
|
match mutbl {
|
2017-10-21 19:15:04 +00:00
|
|
|
// Shared borrowed data is never mutable
|
2019-12-16 16:28:40 +00:00
|
|
|
hir::Mutability::Not => Err(place),
|
2017-10-21 19:15:04 +00:00
|
|
|
// Mutably borrowed data is mutable, but only if we have a
|
|
|
|
// unique path to the `&mut`
|
2019-12-16 16:28:40 +00:00
|
|
|
hir::Mutability::Mut => {
|
2019-08-01 21:41:37 +00:00
|
|
|
let mode = match self.is_upvar_field_projection(place) {
|
2023-11-26 13:39:57 +00:00
|
|
|
Some(field)
|
|
|
|
if self.upvars[field.index()].is_by_ref() =>
|
|
|
|
{
|
2017-12-14 12:33:29 +00:00
|
|
|
is_local_mutation_allowed
|
|
|
|
}
|
|
|
|
_ => LocalMutationIsAllowed::Yes,
|
2017-12-07 19:11:10 +00:00
|
|
|
};
|
|
|
|
|
2021-01-02 18:31:37 +00:00
|
|
|
self.is_mutable(place_base, mode)
|
2017-11-17 09:47:02 +00:00
|
|
|
}
|
2017-10-21 19:15:04 +00:00
|
|
|
}
|
2017-11-17 09:47:02 +00:00
|
|
|
}
|
2024-03-21 21:33:10 +00:00
|
|
|
ty::RawPtr(_, mutbl) => {
|
|
|
|
match mutbl {
|
2017-10-21 19:15:04 +00:00
|
|
|
// `*const` raw pointers are not mutable
|
2019-12-16 16:28:40 +00:00
|
|
|
hir::Mutability::Not => Err(place),
|
2018-03-23 08:59:56 +00:00
|
|
|
// `*mut` raw pointers are always mutable, regardless of
|
|
|
|
// context. The users have to check by themselves.
|
2019-12-16 16:28:40 +00:00
|
|
|
hir::Mutability::Mut => Ok(RootPlace {
|
2019-12-11 19:50:03 +00:00
|
|
|
place_local: place.local,
|
2019-08-01 21:41:37 +00:00
|
|
|
place_projection: place.projection,
|
2018-06-22 03:10:52 +00:00
|
|
|
is_local_mutation_allowed,
|
2018-10-16 13:50:44 +00:00
|
|
|
}),
|
2017-10-21 19:15:04 +00:00
|
|
|
}
|
2017-11-17 09:47:02 +00:00
|
|
|
}
|
2017-11-30 23:18:38 +00:00
|
|
|
// `Box<T>` owns its content, so mutable if its location is mutable
|
2021-01-02 18:31:37 +00:00
|
|
|
_ if base_ty.is_box() => {
|
|
|
|
self.is_mutable(place_base, is_local_mutation_allowed)
|
|
|
|
}
|
2017-10-21 19:15:04 +00:00
|
|
|
// Deref should only be for reference, pointers or boxes
|
2017-11-30 23:18:38 +00:00
|
|
|
_ => bug!("Deref of unexpected type: {:?}", base_ty),
|
2017-10-21 19:15:04 +00:00
|
|
|
}
|
2017-11-17 09:47:02 +00:00
|
|
|
}
|
2017-10-21 19:15:04 +00:00
|
|
|
// All other projections are owned by their base path, so mutable if
|
|
|
|
// base path is mutable
|
2017-12-14 12:33:29 +00:00
|
|
|
ProjectionElem::Field(..)
|
|
|
|
| ProjectionElem::Index(..)
|
|
|
|
| ProjectionElem::ConstantIndex { .. }
|
|
|
|
| ProjectionElem::Subslice { .. }
|
2023-08-16 05:43:30 +00:00
|
|
|
| ProjectionElem::Subtype(..)
|
2022-07-27 11:58:34 +00:00
|
|
|
| ProjectionElem::OpaqueCast { .. }
|
2017-12-14 12:33:29 +00:00
|
|
|
| ProjectionElem::Downcast(..) => {
|
2019-08-01 21:41:37 +00:00
|
|
|
let upvar_field_projection = self.is_upvar_field_projection(place);
|
2018-07-20 16:30:31 +00:00
|
|
|
if let Some(field) = upvar_field_projection {
|
2018-05-16 12:38:32 +00:00
|
|
|
let upvar = &self.upvars[field.index()];
|
2017-12-14 12:33:29 +00:00
|
|
|
debug!(
|
2021-01-12 19:54:12 +00:00
|
|
|
"is_mutable: upvar.mutability={:?} local_mutation_is_allowed={:?} \
|
|
|
|
place={:?}, place_base={:?}",
|
|
|
|
upvar, is_local_mutation_allowed, place, place_base
|
2017-12-14 12:33:29 +00:00
|
|
|
);
|
2023-11-26 13:39:57 +00:00
|
|
|
match (upvar.mutability, is_local_mutation_allowed) {
|
2020-04-17 00:38:52 +00:00
|
|
|
(
|
|
|
|
Mutability::Not,
|
|
|
|
LocalMutationIsAllowed::No
|
|
|
|
| LocalMutationIsAllowed::ExceptUpvars,
|
|
|
|
) => Err(place),
|
2017-12-14 12:33:29 +00:00
|
|
|
(Mutability::Not, LocalMutationIsAllowed::Yes)
|
|
|
|
| (Mutability::Mut, _) => {
|
2024-10-04 00:24:34 +00:00
|
|
|
// Subtle: this is an upvar reference, so it looks like
|
|
|
|
// `self.foo` -- we want to double check that the location
|
|
|
|
// `*self` is mutable (i.e., this is not a `Fn` closure). But
|
|
|
|
// if that check succeeds, we want to *blame* the mutability on
|
|
|
|
// `place` (that is, `self.foo`). This is used to propagate the
|
|
|
|
// info about whether mutability declarations are used
|
|
|
|
// outwards, so that we register the outer variable as mutable.
|
|
|
|
// Otherwise a test like this fails to record the `mut` as
|
|
|
|
// needed:
|
2018-04-06 16:35:50 +00:00
|
|
|
// ```
|
|
|
|
// fn foo<F: FnOnce()>(_f: F) { }
|
|
|
|
// fn main() {
|
|
|
|
// let var = Vec::new();
|
|
|
|
// foo(move || {
|
|
|
|
// var.push(1);
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// ```
|
2021-01-02 18:31:37 +00:00
|
|
|
let _ =
|
|
|
|
self.is_mutable(place_base, is_local_mutation_allowed)?;
|
2018-06-22 03:10:52 +00:00
|
|
|
Ok(RootPlace {
|
2019-12-11 19:50:03 +00:00
|
|
|
place_local: place.local,
|
2019-08-01 21:41:37 +00:00
|
|
|
place_projection: place.projection,
|
2018-06-22 03:10:52 +00:00
|
|
|
is_local_mutation_allowed,
|
|
|
|
})
|
2017-12-14 12:33:29 +00:00
|
|
|
}
|
2017-11-17 09:47:02 +00:00
|
|
|
}
|
2017-12-07 19:11:10 +00:00
|
|
|
} else {
|
2021-01-02 18:31:37 +00:00
|
|
|
self.is_mutable(place_base, is_local_mutation_allowed)
|
2017-10-21 19:15:04 +00:00
|
|
|
}
|
2017-11-17 09:47:02 +00:00
|
|
|
}
|
2017-10-21 19:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-16 12:38:32 +00:00
|
|
|
|
|
|
|
/// If `place` is a field projection, and the field is being projected from a closure type,
|
|
|
|
/// then returns the index of the field being projected. Note that this closure will always
|
|
|
|
/// be `self` in the current MIR, because that is the only time we directly access the fields
|
|
|
|
/// of a closure type.
|
2023-03-28 19:32:57 +00:00
|
|
|
fn is_upvar_field_projection(&self, place_ref: PlaceRef<'tcx>) -> Option<FieldIdx> {
|
2020-05-24 01:40:55 +00:00
|
|
|
path_utils::is_upvar_field_projection(self.infcx.tcx, &self.upvars, place_ref, self.body())
|
2018-05-16 12:38:32 +00:00
|
|
|
}
|
2023-05-17 10:28:33 +00:00
|
|
|
|
|
|
|
fn dominators(&self) -> &Dominators<BasicBlock> {
|
|
|
|
// `BasicBlocks` computes dominators on-demand and caches them.
|
|
|
|
self.body.basic_blocks.dominators()
|
|
|
|
}
|
2024-12-18 17:27:26 +00:00
|
|
|
|
|
|
|
fn lint_unused_mut(&self) {
|
|
|
|
let tcx = self.infcx.tcx;
|
|
|
|
let body = self.body;
|
|
|
|
for local in body.mut_vars_and_args_iter().filter(|local| !self.used_mut.contains(local)) {
|
|
|
|
let local_decl = &body.local_decls[local];
|
|
|
|
let lint_root = match &body.source_scopes[local_decl.source_info.scope].local_data {
|
|
|
|
ClearCrossCrate::Set(data) => data.lint_root,
|
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Skip over locals that begin with an underscore or have no name
|
|
|
|
match self.local_names[local] {
|
|
|
|
Some(name) => {
|
|
|
|
if name.as_str().starts_with('_') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => continue,
|
|
|
|
}
|
|
|
|
|
|
|
|
let span = local_decl.source_info.span;
|
|
|
|
if span.desugaring_kind().is_some() {
|
|
|
|
// If the `mut` arises as part of a desugaring, we should ignore it.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut_span = tcx.sess.source_map().span_until_non_whitespace(span);
|
|
|
|
|
|
|
|
tcx.emit_node_span_lint(UNUSED_MUT, lint_root, span, VarNeedNotMut { span: mut_span })
|
|
|
|
}
|
|
|
|
}
|
2022-02-08 06:37:32 +00:00
|
|
|
}
|
|
|
|
|
2017-12-05 13:08:10 +00:00
|
|
|
/// The degree of overlap between 2 places for borrow-checking.
|
|
|
|
enum Overlap {
|
|
|
|
/// The places might partially overlap - in this case, we give
|
|
|
|
/// up and say that they might conflict. This occurs when
|
|
|
|
/// different fields of a union are borrowed. For example,
|
|
|
|
/// if `u` is a union, we have no way of telling how disjoint
|
|
|
|
/// `u.a.x` and `a.b.y` are.
|
|
|
|
Arbitrary,
|
2017-12-05 22:51:47 +00:00
|
|
|
/// The places have the same type, and are either completely disjoint
|
2018-11-27 02:59:49 +00:00
|
|
|
/// or equal - i.e., they can't "partially" overlap as can occur with
|
2017-12-05 22:51:47 +00:00
|
|
|
/// unions. This is the "base case" on which we recur for extensions
|
|
|
|
/// of the place.
|
2017-12-05 13:08:10 +00:00
|
|
|
EqualOrDisjoint,
|
|
|
|
/// The places are disjoint, so we know all extensions of them
|
|
|
|
/// will also be disjoint.
|
|
|
|
Disjoint,
|
|
|
|
}
|