Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b93cdbc

Browse files
committedOct 26, 2019
Remove QualifResolver abstraction
This is a relic from earlier attempts at dataflow-based const validation that attempted to do promotion at the same time. #63812 takes a different approach: `IsNotPromotable` is no longer a `Qualif` and is computed lazily instead of eagerly. As a result, there's no need for an eager `TempPromotionResolver`, and we can use the only implementer of `QualifResolver` directly instead of through a trait.
1 parent 6538656 commit b93cdbc

File tree

3 files changed

+112
-206
lines changed

3 files changed

+112
-206
lines changed
 

‎src/librustc_mir/transform/check_consts/resolver.rs

Lines changed: 9 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
//! Propagate `Qualif`s between locals and query the results.
22
//!
3-
//! This also contains the dataflow analysis used to track `Qualif`s on complex control-flow
4-
//! graphs.
3+
//! This contains the dataflow analysis used to track `Qualif`s on complex control-flow graphs.
54
65
use rustc::mir::visit::Visitor;
76
use rustc::mir::{self, BasicBlock, Local, Location};
87
use rustc_index::bit_set::BitSet;
98

10-
use std::cell::RefCell;
119
use std::marker::PhantomData;
1210

1311
use crate::dataflow::{self as old_dataflow, generic as dataflow};
1412
use super::{Item, Qualif};
15-
use self::old_dataflow::IndirectlyMutableLocals;
1613

1714
/// A `Visitor` that propagates qualifs between locals. This defines the transfer function of
18-
/// `FlowSensitiveAnalysis` as well as the logic underlying `TempPromotionResolver`.
15+
/// `FlowSensitiveAnalysis`.
1916
///
2017
/// This transfer does nothing when encountering an indirect assignment. Consumers should rely on
2118
/// the `IndirectlyMutableLocals` dataflow pass to see if a `Local` may have become qualified via
@@ -147,145 +144,6 @@ where
147144
}
148145
}
149146

150-
/// Types that can compute the qualifs of each local at each location in a `mir::Body`.
151-
///
152-
/// Code that wishes to use a `QualifResolver` must call `visit_{statement,terminator}` for each
153-
/// statement or terminator, processing blocks in reverse post-order beginning from the
154-
/// `START_BLOCK`. Calling code may optionally call `get` after visiting each statement or
155-
/// terminator to query the qualification state immediately before that statement or terminator.
156-
///
157-
/// These conditions are much more restrictive than woud be required by `FlowSensitiveResolver`
158-
/// alone. This is to allow a linear, on-demand `TempPromotionResolver` that can operate
159-
/// efficiently on simple CFGs.
160-
pub trait QualifResolver<Q> {
161-
/// Get the qualifs of each local at the last location visited.
162-
///
163-
/// This takes `&mut self` so qualifs can be computed lazily.
164-
fn get(&mut self) -> &BitSet<Local>;
165-
166-
/// A convenience method for `self.get().contains(local)`.
167-
fn contains(&mut self, local: Local) -> bool {
168-
self.get().contains(local)
169-
}
170-
171-
/// Resets the resolver to the `START_BLOCK`. This allows a resolver to be reused
172-
/// for multiple passes over a `mir::Body`.
173-
fn reset(&mut self);
174-
}
175-
176-
pub type IndirectlyMutableResults<'mir, 'tcx> =
177-
old_dataflow::DataflowResultsCursor<'mir, 'tcx, IndirectlyMutableLocals<'mir, 'tcx>>;
178-
179-
/// A resolver for qualifs that works on arbitrarily complex CFGs.
180-
///
181-
/// As soon as a `Local` becomes writable through a reference (as determined by the
182-
/// `IndirectlyMutableLocals` dataflow pass), we must assume that it takes on all other qualifs
183-
/// possible for its type. This is because no effort is made to track qualifs across indirect
184-
/// assignments (e.g. `*p = x` or calls to opaque functions).
185-
///
186-
/// It is possible to be more precise here by waiting until an indirect assignment actually occurs
187-
/// before marking a borrowed `Local` as qualified.
188-
pub struct FlowSensitiveResolver<'a, 'mir, 'tcx, Q>
189-
where
190-
Q: Qualif,
191-
{
192-
location: Location,
193-
indirectly_mutable_locals: &'a RefCell<IndirectlyMutableResults<'mir, 'tcx>>,
194-
cursor: dataflow::ResultsCursor<'mir, 'tcx, FlowSensitiveAnalysis<'a, 'mir, 'tcx, Q>>,
195-
qualifs_per_local: BitSet<Local>,
196-
197-
/// The value of `Q::in_any_value_of_ty` for each local.
198-
qualifs_in_any_value_of_ty: BitSet<Local>,
199-
}
200-
201-
impl<Q> FlowSensitiveResolver<'a, 'mir, 'tcx, Q>
202-
where
203-
Q: Qualif,
204-
{
205-
pub fn new(
206-
_: Q,
207-
item: &'a Item<'mir, 'tcx>,
208-
indirectly_mutable_locals: &'a RefCell<IndirectlyMutableResults<'mir, 'tcx>>,
209-
dead_unwinds: &BitSet<BasicBlock>,
210-
) -> Self {
211-
let analysis = FlowSensitiveAnalysis {
212-
item,
213-
_qualif: PhantomData,
214-
};
215-
let results =
216-
dataflow::Engine::new(item.tcx, item.body, item.def_id, dead_unwinds, analysis)
217-
.iterate_to_fixpoint();
218-
let cursor = dataflow::ResultsCursor::new(item.body, results);
219-
220-
let mut qualifs_in_any_value_of_ty = BitSet::new_empty(item.body.local_decls.len());
221-
for (local, decl) in item.body.local_decls.iter_enumerated() {
222-
if Q::in_any_value_of_ty(item, decl.ty) {
223-
qualifs_in_any_value_of_ty.insert(local);
224-
}
225-
}
226-
227-
FlowSensitiveResolver {
228-
cursor,
229-
indirectly_mutable_locals,
230-
qualifs_per_local: BitSet::new_empty(item.body.local_decls.len()),
231-
qualifs_in_any_value_of_ty,
232-
location: Location { block: mir::START_BLOCK, statement_index: 0 },
233-
}
234-
}
235-
}
236-
237-
impl<Q> Visitor<'tcx> for FlowSensitiveResolver<'_, '_, 'tcx, Q>
238-
where
239-
Q: Qualif
240-
{
241-
fn visit_statement(&mut self, _: &mir::Statement<'tcx>, location: Location) {
242-
self.location = location;
243-
}
244-
245-
fn visit_terminator(&mut self, _: &mir::Terminator<'tcx>, location: Location) {
246-
self.location = location;
247-
}
248-
}
249-
250-
impl<Q> QualifResolver<Q> for FlowSensitiveResolver<'_, '_, '_, Q>
251-
where
252-
Q: Qualif
253-
{
254-
fn get(&mut self) -> &BitSet<Local> {
255-
let mut indirectly_mutable_locals = self.indirectly_mutable_locals.borrow_mut();
256-
257-
indirectly_mutable_locals.seek(self.location);
258-
self.cursor.seek_before(self.location);
259-
260-
self.qualifs_per_local.overwrite(indirectly_mutable_locals.get());
261-
self.qualifs_per_local.union(self.cursor.get());
262-
self.qualifs_per_local.intersect(&self.qualifs_in_any_value_of_ty);
263-
&self.qualifs_per_local
264-
}
265-
266-
fn contains(&mut self, local: Local) -> bool {
267-
// No need to update the cursor if we know that `Local` cannot possibly be qualified.
268-
if !self.qualifs_in_any_value_of_ty.contains(local) {
269-
return false;
270-
}
271-
272-
// Otherwise, return `true` if this local is qualified or was indirectly mutable at any
273-
// point before this statement.
274-
self.cursor.seek_before(self.location);
275-
if self.cursor.get().contains(local) {
276-
return true;
277-
}
278-
279-
let mut indirectly_mutable_locals = self.indirectly_mutable_locals.borrow_mut();
280-
indirectly_mutable_locals.seek(self.location);
281-
indirectly_mutable_locals.get().contains(local)
282-
}
283-
284-
fn reset(&mut self) {
285-
self.location = Location { block: mir::START_BLOCK, statement_index: 0 };
286-
}
287-
}
288-
289147
/// The dataflow analysis used to propagate qualifs on arbitrary CFGs.
290148
pub(super) struct FlowSensitiveAnalysis<'a, 'mir, 'tcx, Q> {
291149
item: &'a Item<'mir, 'tcx>,
@@ -296,6 +154,13 @@ impl<'a, 'mir, 'tcx, Q> FlowSensitiveAnalysis<'a, 'mir, 'tcx, Q>
296154
where
297155
Q: Qualif,
298156
{
157+
pub(super) fn new(_: Q, item: &'a Item<'mir, 'tcx>) -> Self {
158+
FlowSensitiveAnalysis {
159+
item,
160+
_qualif: PhantomData,
161+
}
162+
}
163+
299164
fn transfer_function(
300165
&self,
301166
state: &'a mut BitSet<Local>,

‎src/librustc_mir/transform/check_consts/validation.rs

Lines changed: 102 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ use rustc_target::spec::abi::Abi;
99
use syntax::symbol::sym;
1010
use syntax_pos::Span;
1111

12-
use std::cell::RefCell;
1312
use std::fmt;
1413
use std::ops::Deref;
1514

16-
use crate::dataflow as old_dataflow;
17-
use super::{ConstKind, Item, Qualif, is_lang_panic_fn};
18-
use super::resolver::{FlowSensitiveResolver, IndirectlyMutableResults, QualifResolver};
19-
use super::qualifs::{HasMutInterior, NeedsDrop};
15+
use crate::dataflow::{self as old_dataflow, generic as dataflow};
16+
use self::old_dataflow::IndirectlyMutableLocals;
2017
use super::ops::{self, NonConstOp};
18+
use super::qualifs::{HasMutInterior, NeedsDrop};
19+
use super::resolver::FlowSensitiveAnalysis;
20+
use super::{ConstKind, Item, Qualif, is_lang_panic_fn};
2121

2222
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
2323
pub enum CheckOpResult {
@@ -26,9 +26,75 @@ pub enum CheckOpResult {
2626
Allowed,
2727
}
2828

29+
pub type IndirectlyMutableResults<'mir, 'tcx> =
30+
old_dataflow::DataflowResultsCursor<'mir, 'tcx, IndirectlyMutableLocals<'mir, 'tcx>>;
31+
32+
struct QualifCursor<'a, 'mir, 'tcx, Q: Qualif> {
33+
cursor: dataflow::ResultsCursor<'mir, 'tcx, FlowSensitiveAnalysis<'a, 'mir, 'tcx, Q>>,
34+
in_any_value_of_ty: BitSet<Local>,
35+
}
36+
37+
impl<Q: Qualif> QualifCursor<'a, 'mir, 'tcx, Q> {
38+
pub fn new(
39+
q: Q,
40+
item: &'a Item<'mir, 'tcx>,
41+
dead_unwinds: &BitSet<BasicBlock>,
42+
) -> Self {
43+
let analysis = FlowSensitiveAnalysis::new(q, item);
44+
let results =
45+
dataflow::Engine::new(item.tcx, item.body, item.def_id, dead_unwinds, analysis)
46+
.iterate_to_fixpoint();
47+
let cursor = dataflow::ResultsCursor::new(item.body, results);
48+
49+
let mut in_any_value_of_ty = BitSet::new_empty(item.body.local_decls.len());
50+
for (local, decl) in item.body.local_decls.iter_enumerated() {
51+
if Q::in_any_value_of_ty(item, decl.ty) {
52+
in_any_value_of_ty.insert(local);
53+
}
54+
}
55+
56+
QualifCursor {
57+
cursor,
58+
in_any_value_of_ty,
59+
}
60+
}
61+
}
62+
2963
pub struct Qualifs<'a, 'mir, 'tcx> {
30-
has_mut_interior: FlowSensitiveResolver<'a, 'mir, 'tcx, HasMutInterior>,
31-
needs_drop: FlowSensitiveResolver<'a, 'mir, 'tcx, NeedsDrop>,
64+
has_mut_interior: QualifCursor<'a, 'mir, 'tcx, HasMutInterior>,
65+
needs_drop: QualifCursor<'a, 'mir, 'tcx, NeedsDrop>,
66+
indirectly_mutable: IndirectlyMutableResults<'mir, 'tcx>,
67+
}
68+
69+
impl Qualifs<'a, 'mir, 'tcx> {
70+
fn indirectly_mutable(&mut self, local: Local, location: Location) -> bool {
71+
self.indirectly_mutable.seek(location);
72+
self.indirectly_mutable.get().contains(local)
73+
}
74+
75+
/// Returns `true` if `local` is `NeedsDrop` at the given `Location`.
76+
///
77+
/// Only updates the cursor if absolutely necessary
78+
fn needs_drop_lazy_seek(&mut self, local: Local, location: Location) -> bool {
79+
if !self.needs_drop.in_any_value_of_ty.contains(local) {
80+
return false;
81+
}
82+
83+
self.needs_drop.cursor.seek_before(location);
84+
self.needs_drop.cursor.get().contains(local)
85+
|| self.indirectly_mutable(local, location)
86+
}
87+
88+
/// Returns `true` if `local` is `HasMutInterior`, but requires the `has_mut_interior` and
89+
/// `indirectly_mutable` cursors to be updated beforehand.
90+
fn has_mut_interior_eager_seek(&self, local: Local) -> bool {
91+
if !self.has_mut_interior.in_any_value_of_ty.contains(local) {
92+
return false;
93+
}
94+
95+
self.has_mut_interior.cursor.get().contains(local)
96+
|| self.indirectly_mutable.get().contains(local)
97+
}
3298
}
3399

34100
pub struct Validator<'a, 'mir, 'tcx> {
@@ -63,53 +129,43 @@ impl Deref for Validator<'_, 'mir, 'tcx> {
63129
}
64130
}
65131

66-
pub fn compute_indirectly_mutable_locals<'mir, 'tcx>(
67-
item: &Item<'mir, 'tcx>,
68-
) -> RefCell<IndirectlyMutableResults<'mir, 'tcx>> {
69-
let dead_unwinds = BitSet::new_empty(item.body.basic_blocks().len());
70-
71-
let indirectly_mutable_locals = old_dataflow::do_dataflow(
72-
item.tcx,
73-
item.body,
74-
item.def_id,
75-
&item.tcx.get_attrs(item.def_id),
76-
&dead_unwinds,
77-
old_dataflow::IndirectlyMutableLocals::new(item.tcx, item.body, item.param_env),
78-
|_, local| old_dataflow::DebugFormatted::new(&local),
79-
);
80-
81-
let indirectly_mutable_locals = old_dataflow::DataflowResultsCursor::new(
82-
indirectly_mutable_locals,
83-
item.body,
84-
);
85-
86-
RefCell::new(indirectly_mutable_locals)
87-
}
88-
89132
impl Validator<'a, 'mir, 'tcx> {
90133
pub fn new(
91134
item: &'a Item<'mir, 'tcx>,
92-
indirectly_mutable_locals: &'a RefCell<IndirectlyMutableResults<'mir, 'tcx>>,
93135
) -> Self {
94136
let dead_unwinds = BitSet::new_empty(item.body.basic_blocks().len());
95137

96-
let needs_drop = FlowSensitiveResolver::new(
138+
let needs_drop = QualifCursor::new(
97139
NeedsDrop,
98140
item,
99-
indirectly_mutable_locals,
100141
&dead_unwinds,
101142
);
102143

103-
let has_mut_interior = FlowSensitiveResolver::new(
144+
let has_mut_interior = QualifCursor::new(
104145
HasMutInterior,
105146
item,
106-
indirectly_mutable_locals,
107147
&dead_unwinds,
108148
);
109149

150+
let indirectly_mutable = old_dataflow::do_dataflow(
151+
item.tcx,
152+
item.body,
153+
item.def_id,
154+
&item.tcx.get_attrs(item.def_id),
155+
&dead_unwinds,
156+
old_dataflow::IndirectlyMutableLocals::new(item.tcx, item.body, item.param_env),
157+
|_, local| old_dataflow::DebugFormatted::new(&local),
158+
);
159+
160+
let indirectly_mutable = old_dataflow::DataflowResultsCursor::new(
161+
indirectly_mutable,
162+
item.body,
163+
);
164+
110165
let qualifs = Qualifs {
111166
needs_drop,
112167
has_mut_interior,
168+
indirectly_mutable,
113169
};
114170

115171
Validator {
@@ -122,14 +178,6 @@ impl Validator<'a, 'mir, 'tcx> {
122178
}
123179
}
124180

125-
/// Resets the `QualifResolver`s used by this `Validator` and returns them so they can be
126-
/// reused.
127-
pub fn into_qualifs(mut self) -> Qualifs<'a, 'mir, 'tcx> {
128-
self.qualifs.needs_drop.reset();
129-
self.qualifs.has_mut_interior.reset();
130-
self.qualifs
131-
}
132-
133181
pub fn take_errors(&mut self) -> Vec<(Span, String)> {
134182
std::mem::replace(&mut self.errors, vec![])
135183
}
@@ -304,10 +352,16 @@ impl Visitor<'tcx> for Validator<'_, 'mir, 'tcx> {
304352
// it depends on `HasMutInterior` being set for mutable borrows as well as values with
305353
// interior mutability.
306354
if let Rvalue::Ref(_, kind, ref borrowed_place) = *rvalue {
307-
let rvalue_has_mut_interior = {
308-
let has_mut_interior = self.qualifs.has_mut_interior.get();
309-
HasMutInterior::in_rvalue(&self.item, &|l| has_mut_interior.contains(l), rvalue)
310-
};
355+
// FIXME: Change the `in_*` methods to take a `FnMut` so we don't have to manually seek
356+
// the cursors beforehand.
357+
self.qualifs.has_mut_interior.cursor.seek_before(location);
358+
self.qualifs.indirectly_mutable.seek(location);
359+
360+
let rvalue_has_mut_interior = HasMutInterior::in_rvalue(
361+
&self.item,
362+
&|local| self.qualifs.has_mut_interior_eager_seek(local),
363+
rvalue,
364+
);
311365

312366
if rvalue_has_mut_interior {
313367
let is_derived_from_illegal_borrow = match borrowed_place.as_local() {
@@ -402,9 +456,6 @@ impl Visitor<'tcx> for Validator<'_, 'mir, 'tcx> {
402456
fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
403457
trace!("visit_statement: statement={:?} location={:?}", statement, location);
404458

405-
self.qualifs.needs_drop.visit_statement(statement, location);
406-
self.qualifs.has_mut_interior.visit_statement(statement, location);
407-
408459
match statement.kind {
409460
StatementKind::Assign(..) => {
410461
self.super_statement(statement, location);
@@ -424,15 +475,6 @@ impl Visitor<'tcx> for Validator<'_, 'mir, 'tcx> {
424475
}
425476
}
426477

427-
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
428-
trace!("visit_terminator: terminator={:?} location={:?}", terminator, location);
429-
430-
self.qualifs.needs_drop.visit_terminator(terminator, location);
431-
self.qualifs.has_mut_interior.visit_terminator(terminator, location);
432-
433-
self.super_terminator(terminator, location);
434-
}
435-
436478
fn visit_terminator_kind(&mut self, kind: &TerminatorKind<'tcx>, location: Location) {
437479
trace!("visit_terminator_kind: kind={:?} location={:?}", kind, location);
438480
self.super_terminator_kind(kind, location);
@@ -511,7 +553,7 @@ impl Visitor<'tcx> for Validator<'_, 'mir, 'tcx> {
511553
let needs_drop = if let Some(local) = dropped_place.as_local() {
512554
// Use the span where the local was declared as the span of the drop error.
513555
err_span = self.body.local_decls[local].source_info.span;
514-
self.qualifs.needs_drop.contains(local)
556+
self.qualifs.needs_drop_lazy_seek(local, location)
515557
} else {
516558
true
517559
};

‎src/librustc_mir/transform/qualify_consts.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -968,8 +968,7 @@ impl<'a, 'tcx> Checker<'a, 'tcx> {
968968
}
969969

970970
let item = new_checker::Item::new(self.tcx, self.def_id, self.body);
971-
let mut_borrowed_locals = new_checker::validation::compute_indirectly_mutable_locals(&item);
972-
let mut validator = new_checker::validation::Validator::new(&item, &mut_borrowed_locals);
971+
let mut validator = new_checker::validation::Validator::new(&item);
973972

974973
validator.suppress_errors = !use_new_validator;
975974
self.suppress_errors = use_new_validator;

0 commit comments

Comments
 (0)
Please sign in to comment.