Skip to content

Commit d49994b

Browse files
committed
Auto merge of #126023 - amandasystems:you-dropped-this-again, r=nikomatsakis
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 #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` should be inspected by a reviewer, in particular the one on [L#259](https://github.com/rust-lang/rust/compare/master...amandasystems:you-dropped-this-again?expand=1#diff-aa727290e6670264df2face84f012897878e11a70e9c8b156543cfcd9619bac3R259) in this PR, which should be trivial for someone with the right background knowledge to address. I also included some lints I found on the way there that I couldn't help myself from addressing.
2 parents bcf94de + 7b5b7a7 commit d49994b

File tree

13 files changed

+57
-51
lines changed

13 files changed

+57
-51
lines changed

compiler/rustc_borrowck/src/borrow_set.rs

+1-1
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

+1-1
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

+3-3
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

-1
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

+5-5
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
_ => {
@@ -652,7 +652,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
652652
let upvar_index = self.regioncx.get_upvar_index_for_region(self.infcx.tcx, fr)?;
653653
let (upvar_name, upvar_span) = self.regioncx.get_upvar_name_and_span_for_region(
654654
self.infcx.tcx,
655-
&self.upvars,
655+
self.upvars,
656656
upvar_index,
657657
);
658658
let region_name = self.synthesize_region_name();
@@ -717,7 +717,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
717717
.output;
718718
span = output.span();
719719
if let hir::FnRetTy::Return(ret) = output {
720-
hir_ty = Some(self.get_future_inner_return_ty(*ret));
720+
hir_ty = Some(self.get_future_inner_return_ty(ret));
721721
}
722722
" of async function"
723723
}
@@ -958,7 +958,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
958958
{
959959
let (upvar_name, upvar_span) = self.regioncx.get_upvar_name_and_span_for_region(
960960
self.infcx.tcx,
961-
&self.upvars,
961+
self.upvars,
962962
upvar_index,
963963
);
964964
let region_name = self.synthesize_region_name();

compiler/rustc_borrowck/src/nll.rs

-1
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

+2-2
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ fn sccs_info<'tcx>(infcx: &BorrowckInferCtxt<'tcx>, sccs: &ConstraintSccs) {
344344

345345
for (reg_var_idx, scc_idx) in sccs.scc_indices().iter().enumerate() {
346346
let reg_var = ty::RegionVid::from_usize(reg_var_idx);
347-
let origin = var_to_origin.get(&reg_var).unwrap_or_else(|| &RegionCtxt::Unknown);
347+
let origin = var_to_origin.get(&reg_var).unwrap_or(&RegionCtxt::Unknown);
348348
components[scc_idx.as_usize()].insert((reg_var, *origin));
349349
}
350350

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

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

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+1-3
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

+2-8
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,

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ pub(super) fn populate_access_facts<'a, 'tcx>(
8787
body: &Body<'tcx>,
8888
move_data: &MoveData<'tcx>,
8989
) {
90-
debug!("populate_access_facts()");
91-
let location_table = typeck.borrowck_context.location_table;
92-
9390
if let Some(facts) = typeck.borrowck_context.all_facts.as_mut() {
91+
debug!("populate_access_facts()");
92+
let location_table = typeck.borrowck_context.location_table;
93+
9494
let mut extractor = UseFactsExtractor {
9595
var_defined_at: &mut facts.var_defined_at,
9696
var_used_at: &mut facts.var_used_at,

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

+36-19
Original file line numberDiff line numberDiff line change
@@ -217,35 +217,52 @@ impl<'me, 'typeck, 'flow, 'tcx> LivenessResults<'me, 'typeck, 'flow, 'tcx> {
217217
/// Add facts for all locals with free regions, since regions may outlive
218218
/// the function body only at certain nodes in the CFG.
219219
fn add_extra_drop_facts(&mut self, relevant_live_locals: &[Local]) -> Option<()> {
220-
let drop_used = self
221-
.cx
222-
.typeck
223-
.borrowck_context
224-
.all_facts
225-
.as_ref()
226-
.map(|facts| facts.var_dropped_at.clone())?;
220+
// This collect is more necessary than immediately apparent
221+
// because these facts go into `add_drop_live_facts_for()`,
222+
// which also writes to `all_facts`, and so this is genuinely
223+
// a simulatneous overlapping mutable borrow.
224+
// FIXME for future hackers: investigate whether this is
225+
// actually necessary; these facts come from Polonius
226+
// and probably maybe plausibly does not need to go back in.
227+
// It may be necessary to just pick out the parts of
228+
// `add_drop_live_facts_for()` that make sense.
229+
let facts_to_add: Vec<_> = {
230+
let drop_used = &self.cx.typeck.borrowck_context.all_facts.as_ref()?.var_dropped_at;
231+
232+
let relevant_live_locals: FxIndexSet<_> =
233+
relevant_live_locals.iter().copied().collect();
234+
235+
drop_used
236+
.iter()
237+
.filter_map(|(local, location_index)| {
238+
let local_ty = self.cx.body.local_decls[*local].ty;
239+
if relevant_live_locals.contains(local) || !local_ty.has_free_regions() {
240+
return None;
241+
}
227242

228-
let relevant_live_locals: FxIndexSet<_> = relevant_live_locals.iter().copied().collect();
229-
230-
let locations = IntervalSet::new(self.cx.elements.num_points());
231-
232-
for (local, location_index) in drop_used {
233-
if !relevant_live_locals.contains(&local) {
234-
let local_ty = self.cx.body.local_decls[local].ty;
235-
if local_ty.has_free_regions() {
236243
let location = match self
237244
.cx
238245
.typeck
239246
.borrowck_context
240247
.location_table
241-
.to_location(location_index)
248+
.to_location(*location_index)
242249
{
243250
RichLocation::Start(l) => l,
244251
RichLocation::Mid(l) => l,
245252
};
246-
self.cx.add_drop_live_facts_for(local, local_ty, &[location], &locations);
247-
}
248-
}
253+
254+
Some((*local, local_ty, location))
255+
})
256+
.collect()
257+
};
258+
259+
// FIXME: these locations seem to have a special meaning (e.g. everywhere, at the end, ...), but I don't know which one. Please help me rename it to something descriptive!
260+
// Also, if this IntervalSet is used in many places, it maybe should have a newtype'd
261+
// name with a description of what it means for future mortals passing by.
262+
let locations = IntervalSet::new(self.cx.elements.num_points());
263+
264+
for (local, local_ty, location) in facts_to_add {
265+
self.cx.add_drop_live_facts_for(local, local_ty, &[location], &locations);
249266
}
250267
Some(())
251268
}

compiler/rustc_borrowck/src/type_check/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ pub(crate) fn type_check<'mir, 'tcx>(
133133
move_data: &MoveData<'tcx>,
134134
elements: &Rc<DenseLocationMap>,
135135
upvars: &[&ty::CapturedPlace<'tcx>],
136-
use_polonius: bool,
137136
) -> MirTypeckResults<'tcx> {
138137
let implicit_region_bound = ty::Region::new_var(infcx.tcx, universal_regions.fr_fn_body);
139138
let mut constraints = MirTypeckRegionConstraints {
@@ -189,7 +188,7 @@ pub(crate) fn type_check<'mir, 'tcx>(
189188
checker.equate_inputs_and_outputs(body, universal_regions, &normalized_inputs_and_output);
190189
checker.check_signature_annotation(body);
191190

192-
liveness::generate(&mut checker, body, elements, flow_inits, move_data, use_polonius);
191+
liveness::generate(&mut checker, body, elements, flow_inits, move_data);
193192

194193
translate_outlives_facts(&mut checker);
195194
let opaque_type_values = infcx.take_opaque_types();

0 commit comments

Comments
 (0)