Skip to content

Commit 7b5b7a7

Browse files
committed
Remove confusing use_polonius flag and do less cloning
The `use_polonius` flag is both redundant and confusing since every function it's propagated to also checks if `all_facts` is `Some`, the true test of whether to generate Polonius facts for Polonius or for external consumers. This PR makes that path clearer by simply doing away with the argument and handling the logic in precisely two places: where facts are populated (check for `Some`), and where `all_facts` are initialised. It also delays some statements until after that check to avoid the miniscule performance penalty of executing them when Polonius is disabled. This also addresses @lqd's concern in rust-lang#125652 by reducing the size of what is cloned out of Polonius facts to just the facts being added, as opposed to the entire vector of potential inputs, and added descriptive comments. *Reviewer note*: the comments in [add_extra_drop_facts](https://github.com/rust-lang/rust/blob/85f90a461262f7ca37a6e629933d455fa9c3ee48/compiler/rustc_borrowck/src/type_check/liveness/trace.rs#L219) should be inspected by a reviewer, in particular the one on L#259 in this PR, which should be trivial for someone with the right background knowledge. I also included some minor lints I found on the way there that I couldn't help myself from addressing.
1 parent 85f90a4 commit 7b5b7a7

File tree

13 files changed

+57
-51
lines changed

13 files changed

+57
-51
lines changed

compiler/rustc_borrowck/src/borrow_set.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl<'tcx> BorrowSet<'tcx> {
126126
) -> Self {
127127
let mut visitor = GatherBorrows {
128128
tcx,
129-
body: body,
129+
body,
130130
location_map: Default::default(),
131131
activation_map: Default::default(),
132132
local_map: Default::default(),

compiler/rustc_borrowck/src/borrowck_errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
213213
via(msg_old),
214214
);
215215

216-
if msg_new == "" {
216+
if msg_new.is_empty() {
217217
// If `msg_new` is empty, then this isn't a borrow of a union field.
218218
err.span_label(span, format!("{kind_new} borrow occurs here"));
219219
err.span_label(old_span, format!("{kind_old} borrow occurs here"));

compiler/rustc_borrowck/src/dataflow.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ impl<'mir, 'tcx> ResultsVisitable<'tcx> for BorrowckResults<'mir, 'tcx> {
4343
}
4444

4545
fn reset_to_block_entry(&self, state: &mut Self::FlowState, block: BasicBlock) {
46-
state.borrows.clone_from(&self.borrows.entry_set_for_block(block));
47-
state.uninits.clone_from(&self.uninits.entry_set_for_block(block));
48-
state.ever_inits.clone_from(&self.ever_inits.entry_set_for_block(block));
46+
state.borrows.clone_from(self.borrows.entry_set_for_block(block));
47+
state.uninits.clone_from(self.uninits.entry_set_for_block(block));
48+
state.ever_inits.clone_from(self.ever_inits.entry_set_for_block(block));
4949
}
5050

5151
fn reconstruct_before_statement_effect(

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,6 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
895895
for alias_ty in alias_tys {
896896
if alias_ty.span.desugaring_kind().is_some() {
897897
// Skip `async` desugaring `impl Future`.
898-
()
899898
}
900899
if let TyKind::TraitObject(_, lt, _) = alias_ty.kind {
901900
if lt.ident.name == kw::Empty {

compiler/rustc_borrowck/src/diagnostics/region_name.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
519519
}
520520

521521
// Otherwise, let's descend into the referent types.
522-
search_stack.push((*referent_ty, &referent_hir_ty.ty));
522+
search_stack.push((*referent_ty, referent_hir_ty.ty));
523523
}
524524

525525
// Match up something like `Foo<'1>`
@@ -558,7 +558,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
558558
}
559559

560560
(ty::RawPtr(mut_ty, _), hir::TyKind::Ptr(mut_hir_ty)) => {
561-
search_stack.push((*mut_ty, &mut_hir_ty.ty));
561+
search_stack.push((*mut_ty, mut_hir_ty.ty));
562562
}
563563

564564
_ => {
@@ -654,7 +654,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
654654
let upvar_index = self.regioncx.get_upvar_index_for_region(self.infcx.tcx, fr)?;
655655
let (upvar_name, upvar_span) = self.regioncx.get_upvar_name_and_span_for_region(
656656
self.infcx.tcx,
657-
&self.upvars,
657+
self.upvars,
658658
upvar_index,
659659
);
660660
let region_name = self.synthesize_region_name();
@@ -719,7 +719,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
719719
.output;
720720
span = output.span();
721721
if let hir::FnRetTy::Return(ret) = output {
722-
hir_ty = Some(self.get_future_inner_return_ty(*ret));
722+
hir_ty = Some(self.get_future_inner_return_ty(ret));
723723
}
724724
" of async function"
725725
}
@@ -960,7 +960,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
960960
{
961961
let (upvar_name, upvar_span) = self.regioncx.get_upvar_name_and_span_for_region(
962962
self.infcx.tcx,
963-
&self.upvars,
963+
self.upvars,
964964
upvar_index,
965965
);
966966
let region_name = self.synthesize_region_name();

compiler/rustc_borrowck/src/nll.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ pub(crate) fn compute_regions<'cx, 'tcx>(
114114
move_data,
115115
elements,
116116
upvars,
117-
polonius_input,
118117
);
119118

120119
// Create the region inference context, taking ownership of the

compiler/rustc_borrowck/src/polonius/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ pub(crate) fn emit_facts<'tcx>(
4343
emit_universal_region_facts(
4444
all_facts,
4545
borrow_set,
46-
&universal_regions,
47-
&universal_region_relations,
46+
universal_regions,
47+
universal_region_relations,
4848
);
4949
emit_cfg_and_loan_kills_facts(all_facts, tcx, location_table, body, borrow_set);
5050
emit_loan_invalidations_facts(all_facts, tcx, location_table, body, borrow_set);

compiler/rustc_borrowck/src/region_infer/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ fn sccs_info<'tcx>(infcx: &BorrowckInferCtxt<'tcx>, sccs: Rc<Sccs<RegionVid, Con
269269

270270
for (reg_var_idx, scc_idx) in sccs.scc_indices().iter().enumerate() {
271271
let reg_var = ty::RegionVid::from_usize(reg_var_idx);
272-
let origin = var_to_origin.get(&reg_var).unwrap_or_else(|| &RegionCtxt::Unknown);
272+
let origin = var_to_origin.get(&reg_var).unwrap_or(&RegionCtxt::Unknown);
273273
components[scc_idx.as_usize()].insert((reg_var, *origin));
274274
}
275275

@@ -2224,7 +2224,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
22242224
// #114907 where this happens via liveness and dropck outlives results.
22252225
// Therefore, we return a default value in case that happens, which should at worst emit a
22262226
// suboptimal error, instead of the ICE.
2227-
self.universe_causes.get(&universe).cloned().unwrap_or_else(|| UniverseInfo::other())
2227+
self.universe_causes.get(&universe).cloned().unwrap_or_else(UniverseInfo::other)
22282228
}
22292229

22302230
/// Tries to find the terminator of the loop in which the region 'r' resides.

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,9 +418,7 @@ fn check_opaque_type_parameter_valid<'tcx>(
418418
let opaque_param = opaque_generics.param_at(i, tcx);
419419
let kind = opaque_param.kind.descr();
420420

421-
if let Err(guar) = opaque_env.param_is_error(i) {
422-
return Err(guar);
423-
}
421+
opaque_env.param_is_error(i)?;
424422

425423
return Err(tcx.dcx().emit_err(NonGenericOpaqueTypeParam {
426424
ty: arg,

compiler/rustc_borrowck/src/type_check/liveness/mod.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ use rustc_mir_dataflow::ResultsCursor;
1212
use std::rc::Rc;
1313

1414
use crate::{
15-
constraints::OutlivesConstraintSet,
16-
facts::{AllFacts, AllFactsExt},
17-
region_infer::values::LivenessValues,
15+
constraints::OutlivesConstraintSet, region_infer::values::LivenessValues,
1816
universal_regions::UniversalRegions,
1917
};
2018

@@ -38,7 +36,6 @@ pub(super) fn generate<'mir, 'tcx>(
3836
elements: &Rc<DenseLocationMap>,
3937
flow_inits: &mut ResultsCursor<'mir, 'tcx, MaybeInitializedPlaces<'mir, 'tcx>>,
4038
move_data: &MoveData<'tcx>,
41-
use_polonius: bool,
4239
) {
4340
debug!("liveness::generate");
4441

@@ -49,11 +46,8 @@ pub(super) fn generate<'mir, 'tcx>(
4946
);
5047
let (relevant_live_locals, boring_locals) =
5148
compute_relevant_live_locals(typeck.tcx(), &free_regions, body);
52-
let facts_enabled = use_polonius || AllFacts::enabled(typeck.tcx());
5349

54-
if facts_enabled {
55-
polonius::populate_access_facts(typeck, body, move_data);
56-
};
50+
polonius::populate_access_facts(typeck, body, move_data);
5751

5852
trace::trace(
5953
typeck,

0 commit comments

Comments
 (0)