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

Commit 81986ba

Browse files
author
Hendrik van Antwerpen
committed
Add method to eliminate precondition stack variables
1 parent f7199b3 commit 81986ba

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

stack-graphs/src/partial.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2302,6 +2302,45 @@ impl PartialPath {
23022302
.with_offset(scope_variable_offset);
23032303
}
23042304

2305+
/// Replaces stack variables in the precondition with empty stacks.
2306+
pub fn eliminate_precondition_stack_variables(&mut self, partials: &mut PartialPaths) {
2307+
let mut symbol_bindings = PartialSymbolStackBindings::new();
2308+
let mut scope_bindings = PartialScopeStackBindings::new();
2309+
if let Some(symbol_variable) = self.symbol_stack_precondition.variable() {
2310+
symbol_bindings
2311+
.add(
2312+
partials,
2313+
symbol_variable,
2314+
PartialSymbolStack::empty(),
2315+
&mut scope_bindings,
2316+
)
2317+
.unwrap();
2318+
}
2319+
if let Some(scope_variable) = self.scope_stack_precondition.variable() {
2320+
scope_bindings
2321+
.add(partials, scope_variable, PartialScopeStack::empty())
2322+
.unwrap();
2323+
}
2324+
2325+
self.symbol_stack_precondition = self
2326+
.symbol_stack_precondition
2327+
.apply_partial_bindings(partials, &symbol_bindings, &scope_bindings)
2328+
.unwrap();
2329+
self.scope_stack_precondition = self
2330+
.scope_stack_precondition
2331+
.apply_partial_bindings(partials, &scope_bindings)
2332+
.unwrap();
2333+
2334+
self.symbol_stack_postcondition = self
2335+
.symbol_stack_postcondition
2336+
.apply_partial_bindings(partials, &symbol_bindings, &scope_bindings)
2337+
.unwrap();
2338+
self.scope_stack_postcondition = self
2339+
.scope_stack_postcondition
2340+
.apply_partial_bindings(partials, &scope_bindings)
2341+
.unwrap();
2342+
}
2343+
23052344
/// Attempts to append an edge to the end of a partial path. If the edge is not a valid
23062345
/// extension of this partial path, we return an error describing why.
23072346
pub fn append(

stack-graphs/tests/it/partial.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,5 +675,23 @@ fn can_append_partial_paths() -> Result<(), PathResolutionError> {
675675
"<%1> ($1) [test(7) push scoped baz test(6)] -> [jump to scope] <baz/([test(6)],$1),%1> ($1)",
676676
);
677677

678+
// verify that without stack variables in the precondition, the precondition cannot grow because of concatenation
679+
{
680+
let left = &[scope0];
681+
let right = &[scope0, bar_def];
682+
683+
let mut g = StackGraph::new();
684+
g.add_from_graph(&graph).expect("");
685+
686+
let mut ps = PartialPaths::new();
687+
let mut l = create_partial_path_and_edges(&mut g, &mut ps, left).expect("");
688+
l.eliminate_precondition_stack_variables(&mut ps);
689+
let mut r = create_partial_path_and_edges(&mut g, &mut ps, right).expect("");
690+
691+
r.ensure_no_overlapping_variables(&mut ps, &l);
692+
let result = l.concatenate(&g, &mut ps, &r);
693+
result.expect_err("");
694+
}
695+
678696
Ok(())
679697
}

0 commit comments

Comments
 (0)