Skip to content

Commit 9dc5770

Browse files
factor out transition & assert on no_exposed_references
1 parent b5b8291 commit 9dc5770

File tree

1 file changed

+77
-71
lines changed
  • src/borrow_tracker/tree_borrows

1 file changed

+77
-71
lines changed

src/borrow_tracker/tree_borrows/tree.rs

Lines changed: 77 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use rustc_data_structures::fx::FxHashSet;
1818
use rustc_span::Span;
1919
use smallvec::SmallVec;
2020

21+
use super::diagnostics::AccessCause;
2122
use super::wildcard::WildcardState;
2223
use crate::borrow_tracker::tree_borrows::Permission;
2324
use crate::borrow_tracker::tree_borrows::diagnostics::{
@@ -82,6 +83,47 @@ impl LocationState {
8283
self.permission
8384
}
8485

86+
fn perform_transition(
87+
&mut self,
88+
idx: UniIndex,
89+
nodes: &mut UniValMap<Node>,
90+
wildcard_accesses: &mut UniValMap<WildcardState>,
91+
access_kind: AccessKind,
92+
access_cause: AccessCause,
93+
access_range: Option<AllocRange>,
94+
relatedness: AccessRelatedness,
95+
span: Span,
96+
location_range: Range<u64>,
97+
protected: bool,
98+
) -> Result<(), TransitionError> {
99+
// Call this function now (i.e. only if we know `relatedness`), which
100+
// ensures it is only called when `skip_if_known_noop` returns
101+
// `Recurse`, due to the contract of `traverse_this_parents_children_other`.
102+
self.record_new_access(access_kind, relatedness);
103+
104+
let transition = self.perform_access(access_kind, relatedness, protected)?;
105+
if !transition.is_noop() {
106+
let node = nodes.get_mut(idx).unwrap();
107+
// Record the event as part of the history.
108+
node.debug_info.history.push(diagnostics::Event {
109+
transition,
110+
is_foreign: relatedness.is_foreign(),
111+
access_cause,
112+
access_range,
113+
transition_range: location_range,
114+
span,
115+
});
116+
117+
// We need to update the wildcard state, if the permission
118+
// of an exposed pointer changes.
119+
if node.is_exposed {
120+
let access_type = self.permission.strongest_allowed_child_access(protected);
121+
WildcardState::update_exposure(idx, access_type, nodes, wildcard_accesses);
122+
}
123+
}
124+
Ok(())
125+
}
126+
85127
/// Apply the effect of an access to one location, including
86128
/// - applying `Permission::perform_access` to the inner `Permission`,
87129
/// - emitting protector UB if the location is accessed,
@@ -870,37 +912,20 @@ impl<'tcx> Tree {
870912
let mut perm = args.loc.perms.entry(args.idx);
871913

872914
let state = perm.or_insert(node.default_location_state());
873-
// Call this function now, which ensures it is only called when
874-
// `skip_if_known_noop` returns `Recurse`, due to the contract of
875-
// `traverse_this_parents_children_other`.
876-
state.record_new_access(access_kind, args.rel_pos);
877915

878916
let protected = global.borrow().protected_tags.contains_key(&node.tag);
879-
let transition = state.perform_access(access_kind, args.rel_pos, protected)?;
880-
// Record the event as part of the history
881-
if !transition.is_noop() {
882-
node.debug_info.history.push(diagnostics::Event {
883-
transition,
884-
is_foreign: args.rel_pos.is_foreign(),
885-
access_cause,
886-
access_range: access_range_and_kind.map(|x| x.0),
887-
transition_range: perms_range,
888-
span,
889-
});
890-
891-
// We need to update the wildcard access tracking information,
892-
// if the permission of an exposed pointer changes
893-
if node.is_exposed {
894-
let access_type = state.permission.strongest_allowed_child_access(protected);
895-
WildcardState::update_exposure(
896-
args.idx,
897-
access_type,
898-
args.nodes,
899-
&mut args.loc.wildcard_accesses,
900-
);
901-
}
902-
}
903-
Ok(())
917+
state.perform_transition(
918+
args.idx,
919+
args.nodes,
920+
&mut args.loc.wildcard_accesses,
921+
access_kind,
922+
access_cause,
923+
/* access_range */ access_range_and_kind.map(|x| x.0),
924+
args.rel_pos,
925+
span,
926+
perms_range,
927+
protected,
928+
)
904929
};
905930

906931
// Error handler in case `node_app` goes wrong.
@@ -1180,6 +1205,7 @@ impl<'tcx> Tree {
11801205
// There doesn't exist a valid exposed reference for this access to
11811206
// happen through.
11821207
// If this fails for one id, then it fails for all ids.
1208+
assert_eq!(self.root, args.idx);
11831209
return Err(no_valid_exposed_references_error(
11841210
alloc_id,
11851211
loc_range.start,
@@ -1192,50 +1218,30 @@ impl<'tcx> Tree {
11921218
// to this node, but we still update each of its children.
11931219
return Ok(());
11941220
};
1195-
1196-
// Call this function now (i.e. only if we know `relatedness`), which
1197-
// ensures it is only called when `skip_if_known_noop` returns
1198-
// `Recurse`, due to the contract of `traverse_this_parents_children_other`.
1199-
perm.record_new_access(access_kind, relatedness);
1200-
1201-
let transition = perm
1202-
.perform_access(access_kind, relatedness, protected)
1203-
.map_err(|trans| {
1204-
TbError {
1205-
conflicting_info: &node.debug_info,
1206-
access_cause,
1207-
alloc_id,
1208-
error_offset: loc_range.start,
1209-
error_kind: trans,
1210-
accessed_info: None,
1211-
}
1212-
.build()
1213-
})?;
1214-
if !transition.is_noop() {
1215-
// Record the event as part of the history.
1216-
node.debug_info.history.push(diagnostics::Event {
1217-
transition,
1218-
is_foreign: relatedness.is_foreign(),
1221+
perm.perform_transition(
1222+
args.idx,
1223+
args.nodes,
1224+
&mut args.loc.wildcard_accesses,
1225+
access_kind,
1226+
access_cause,
1227+
Some(access_range),
1228+
relatedness,
1229+
span,
1230+
loc_range.clone(),
1231+
protected,
1232+
)
1233+
.map_err(|trans| {
1234+
let node = args.nodes.get(args.idx).unwrap();
1235+
TbError {
1236+
conflicting_info: &node.debug_info,
12191237
access_cause,
1220-
access_range: access_range_and_kind.map(|x| x.0),
1221-
transition_range: loc_range.clone(),
1222-
span,
1223-
});
1224-
1225-
// We need to update the wildcard state, if the permission
1226-
// of an exposed pointer changes.
1227-
if node.is_exposed {
1228-
let access_type =
1229-
perm.permission.strongest_allowed_child_access(protected);
1230-
WildcardState::update_exposure(
1231-
args.idx,
1232-
access_type,
1233-
args.nodes,
1234-
&mut args.loc.wildcard_accesses,
1235-
);
1238+
alloc_id,
1239+
error_offset: loc_range.start,
1240+
error_kind: trans,
1241+
accessed_info: None,
12361242
}
1237-
}
1238-
Ok(())
1243+
.build()
1244+
})
12391245
},
12401246
|err| err.error_kind,
12411247
)?;

0 commit comments

Comments
 (0)