Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit b80ae09

Browse files
author
Hendrik van Antwerpen
committed
Clarify various resolve method names and add test
1 parent ac8f6fb commit b80ae09

File tree

3 files changed

+76
-12
lines changed

3 files changed

+76
-12
lines changed

stack-graphs/src/cycles.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,14 @@ impl<A: Appendable + Clone> AppendingCycleDetector<A> {
216216
// build prefix path -- prefix starts at end_node, because this is a cycle
217217
let mut prefix_path = PartialPath::from_node(graph, partials, end_node);
218218
while let Some(appendage) = prefix_appendages.pop_front(appendables) {
219-
prefix_path.resolve_to(graph, partials, appendage.start_node(ctx))?;
219+
prefix_path.resolve_to_node(graph, partials, appendage.start_node(ctx))?;
220220
appendage.append_to(graph, partials, ctx, &mut prefix_path)?;
221221
}
222222

223223
// build cyclic path
224224
let cyclic_path = maybe_cyclic_path
225225
.unwrap_or_else(|| PartialPath::from_node(graph, partials, end_node));
226-
prefix_path.resolve_to(graph, partials, cyclic_path.start_node)?;
226+
prefix_path.resolve_to_node(graph, partials, cyclic_path.start_node)?;
227227
prefix_path.ensure_no_overlapping_variables(partials, &cyclic_path);
228228
prefix_path.concatenate(graph, partials, &cyclic_path)?;
229229
if let Some(cyclicity) = prefix_path.is_cyclic(graph, partials) {

stack-graphs/src/partial.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,15 +2371,15 @@ impl PartialPath {
23712371
},
23722372
);
23732373

2374-
self.resolve(graph, partials)?;
2374+
self.resolve_from_postcondition(graph, partials)?;
23752375

23762376
Ok(())
23772377
}
23782378

2379-
/// Attempts to resolve any _jump to scope_ node at the end of a partial path. If the partial
2380-
/// path does not end in a _jump to scope_ node, we do nothing. If it does, and we cannot
2381-
/// resolve it, then we return an error describing why.
2382-
pub fn resolve(
2379+
/// Attempts to resolve any _jump to scope_ node at the end of a partial path from the postcondition
2380+
/// scope stack. If the partial path does not end in a _jump to scope_ node, we do nothing. If it
2381+
/// does, and we cannot resolve it, then we return an error describing why.
2382+
pub fn resolve_from_postcondition(
23832383
&mut self,
23842384
graph: &StackGraph,
23852385
partials: &mut PartialPaths,
@@ -2405,10 +2405,11 @@ impl PartialPath {
24052405
Ok(())
24062406
}
24072407

2408-
/// Attempts to resolve any _jump to scope_ node at the end of a partial path to the given node.
2409-
/// If the partial path does not end in a _jump to scope_ node, we do nothing. If it does, and we
2410-
/// cannot resolve it, then we return an error describing why.
2411-
pub fn resolve_to(
2408+
/// Resolve any _jump to scope_ node at the end of a partial path to the given node, updating the
2409+
/// precondition to include the given node. If the partial path does not end in a _jump to scope_
2410+
/// node, we do nothing. If it does, and we cannot resolve it, then we return an error describing
2411+
/// why.
2412+
pub fn resolve_to_node(
24122413
&mut self,
24132414
graph: &StackGraph,
24142415
partials: &mut PartialPaths,
@@ -2971,7 +2972,7 @@ impl PartialPath {
29712972
}
29722973
lhs.end_node = rhs.end_node;
29732974

2974-
lhs.resolve(graph, partials)?;
2975+
lhs.resolve_from_postcondition(graph, partials)?;
29752976

29762977
Ok(())
29772978
}

stack-graphs/tests/it/partial.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,3 +672,66 @@ fn can_append_partial_paths() -> Result<(), PathResolutionError> {
672672

673673
Ok(())
674674
}
675+
676+
#[test]
677+
fn can_resolve_to_node() -> Result<(), PathResolutionError> {
678+
let mut graph = StackGraph::new();
679+
let file = graph.add_file("test").expect("");
680+
681+
let jump_to_scope_node = StackGraph::jump_to_node();
682+
let exported_scope = create_scope_node(&mut graph, file, true);
683+
let baz_def = create_pop_scoped_symbol_node(&mut graph, file, "baz", false);
684+
685+
// resolved node ends up in precondition scope stack
686+
{
687+
let mut g = StackGraph::new();
688+
g.add_from_graph(&graph).expect("");
689+
690+
let mut ps = PartialPaths::new();
691+
let mut p =
692+
create_partial_path_and_edges(&mut g, &mut ps, &[jump_to_scope_node]).expect("");
693+
694+
p.resolve_to_node(&g, &mut ps, exported_scope).expect("");
695+
assert_eq!(
696+
Some(exported_scope),
697+
p.scope_stack_precondition.pop_front(&mut ps)
698+
);
699+
}
700+
701+
// precondition is fixed, and resolving fails
702+
{
703+
let mut g = StackGraph::new();
704+
g.add_from_graph(&graph).expect("");
705+
706+
let mut ps = PartialPaths::new();
707+
let mut p =
708+
create_partial_path_and_edges(&mut g, &mut ps, &[jump_to_scope_node]).expect("");
709+
710+
p.eliminate_precondition_stack_variables(&mut ps);
711+
p.resolve_to_node(&g, &mut ps, exported_scope)
712+
.expect_err("");
713+
}
714+
715+
// resolved node ends up in scoped symbol in precondition symbol stack
716+
// resolving succeeds even though the scope stack is finite
717+
{
718+
let mut g = StackGraph::new();
719+
g.add_from_graph(&graph).expect("");
720+
721+
let mut ps = PartialPaths::new();
722+
let mut p = create_partial_path_and_edges(&mut g, &mut ps, &[baz_def, jump_to_scope_node])
723+
.expect("");
724+
725+
p.eliminate_precondition_stack_variables(&mut ps);
726+
p.resolve_to_node(&g, &mut ps, exported_scope).expect("");
727+
assert_eq!(
728+
Some(exported_scope),
729+
p.symbol_stack_precondition
730+
.pop_front(&mut ps)
731+
.and_then(|s| s.scopes.into_option())
732+
.and_then(|mut st| st.pop_front(&mut ps))
733+
);
734+
}
735+
736+
Ok(())
737+
}

0 commit comments

Comments
 (0)