Maintain command source bounds across checkpoint revisions

This commit is contained in:
gloamxun committed 2026-12-02 02:38:43 +08:00
1 parent c8f7a0cbe6
commit 65bf302f5f
3 files changed
+70 -21

No files matched your search

+10 -12
View File
@@ -810,20 +810,20 @@ type CommandEntries = HashMap<Arc<CommandName>, Meaning, RandomState>;
#[derive(Clone, Debug)]
struct CommandShard {
entries: CommandEntries,
// Bounds may retain ranges of replaced or removed entries. Their only
// use is to prove that every surviving token is before an edit.
source_bounds: OnceLock<BTreeMap<usize, Option<usize>>>,
// Keep conservative bounds as each meaning is installed. Checkpoints
// share the bounds along with the entries, so proving a source move does
// not have to discover them from every macro body in each donor.
// Replacing or removing an entry may retain its bound.
source_bounds: BTreeMap<usize, Option<usize>>,
}
impl CommandShard {
fn new(entries: CommandEntries) -> Self {
Self { entries, source_bounds: OnceLock::new() }
fn new(hasher: RandomState) -> Self {
Self { entries: CommandEntries::with_hasher(hasher), source_bounds: BTreeMap::new() }
}
fn insert(&mut self, name: Arc<CommandName>, meaning: Meaning) -> Option<Meaning> {
if let Some(bounds) = self.source_bounds.get_mut() {
source_relocation::include_meaning_bounds(bounds, &meaning);
}
source_relocation::include_meaning_bounds(&mut self.source_bounds, &meaning);
self.entries.insert(name, meaning)
}
@@ -831,9 +831,7 @@ impl CommandShard {
let hashbrown::hash_map::Entry::Vacant(entry) = self.entries.entry(name) else {
return false;
};
if let Some(bounds) = self.source_bounds.get_mut() {
source_relocation::include_meaning_bounds(bounds, &meaning);
}
source_relocation::include_meaning_bounds(&mut self.source_bounds, &meaning);
entry.insert(meaning);
true
}
@@ -869,7 +867,7 @@ impl Default for CommandBindings {
let hasher = command_hasher();
Self {
shards: std::array::from_fn(|_| {
Arc::new(CommandShard::new(CommandEntries::with_hasher(hasher.clone())))
Arc::new(CommandShard::new(hasher.clone()))
}),
revisions: [0; COMMAND_SHARDS],
ordered_names: Arc::default(),
@@ -104,14 +104,7 @@ impl State {
impl CommandShard {
fn preserves_source_spans(&self, relocation: &SourceRelocation) -> bool {
let bounds = self.source_bounds.get_or_init(|| {
let mut bounds = BTreeMap::<usize, Option<usize>>::new();
for meaning in self.entries.values() {
include_meaning_bounds(&mut bounds, meaning);
}
bounds
});
bounds.get(&relocation.source()).is_none_or(|end| {
self.source_bounds.get(&relocation.source()).is_none_or(|end| {
end.is_some_and(|end| relocation.preserves_prefix_through(end))
})
}
@@ -170,10 +163,13 @@ fn command_bounds_track_redefinitions_and_invocation_origins() {
let mut state = State::default();
state.define(name.clone(), meaning(None), true);
state.relocate_source(&mut EventRelocation::new(&map)).unwrap();
let shard = CommandBindings::shard_index(command_hasher().hash_one(&name));
assert_eq!(state.commands.shards[shard].source_bounds[&0], Some(1));
let before = state.clone();
let token_span = Span { source: 0, start: 8, end: 9 };
state.define(name.clone(), meaning(Some(token_span)), true);
assert_eq!(state.commands.shards[shard].source_bounds[&0], Some(9));
state.relocate_source(&mut EventRelocation::new(&map)).unwrap();
let mapped = map.relocate_span(token_span).unwrap();
assert_ne!(mapped, token_span);