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

Commit 9fc177e

Browse files
author
Hendrik van Antwerpen
committed
Add GraphEdges instead of () and support file filtering
1 parent a5a732f commit 9fc177e

File tree

3 files changed

+35
-24
lines changed

3 files changed

+35
-24
lines changed

stack-graphs/src/partial.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ use crate::graph::StackGraph;
5656
use crate::graph::Symbol;
5757
use crate::paths::Extend;
5858
use crate::paths::PathResolutionError;
59+
use crate::stitching::GraphEdges;
5960
use crate::utils::cmp_option;
6061
use crate::utils::equals_option;
6162
use crate::CancellationError;
@@ -2549,7 +2550,7 @@ impl PartialPaths {
25492550
copious_debugging!(" * visit");
25502551
visit(graph, self, path);
25512552
} else if !path_cycle_detector
2552-
.is_cyclic(graph, self, &(), &mut edges)
2553+
.is_cyclic(graph, self, &GraphEdges(Some(file)), &mut edges)
25532554
.expect("cyclic test failed when finding partial paths")
25542555
.is_empty()
25552556
{
@@ -2608,7 +2609,7 @@ impl PartialPaths {
26082609
visit(graph, self, path.clone());
26092610
}
26102611
if !path_cycle_detector
2611-
.is_cyclic(graph, &mut partials, &(), &mut edges)
2612+
.is_cyclic(graph, &mut partials, &GraphEdges(None), &mut edges)
26122613
.expect("cyclic test failed when finding complete paths")
26132614
.into_iter()
26142615
.all(|c| c == Cyclicity::StrengthensPrecondition)

stack-graphs/src/stitching.rs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ use crate::cycles::Appendables;
5151
use crate::cycles::AppendingCycleDetector;
5252
use crate::cycles::SimilarPathDetector;
5353
use crate::graph::Edge;
54+
use crate::graph::File;
5455
use crate::graph::Node;
5556
use crate::graph::StackGraph;
5657
use crate::graph::Symbol;
@@ -162,18 +163,6 @@ where
162163
fn get_appendable<'a>(&'a self, handle: &'a H) -> &'a A;
163164
}
164165

165-
impl ToAppendable<Handle<PartialPath>, PartialPath> for Database {
166-
fn get_appendable<'a>(&'a self, handle: &'a Handle<PartialPath>) -> &'a PartialPath {
167-
&self[*handle]
168-
}
169-
}
170-
171-
impl<A: Appendable + Clone> ToAppendable<A, A> for () {
172-
fn get_appendable<'a>(&'a self, value: &'a A) -> &'a A {
173-
value
174-
}
175-
}
176-
177166
//-------------------------------------------------------------------------------------------------
178167
// Candidates
179168

@@ -188,7 +177,19 @@ pub trait Candidates<H> {
188177
R: std::iter::Extend<H>;
189178
}
190179

191-
impl Candidates<Edge> for () {
180+
//-------------------------------------------------------------------------------------------------
181+
// FileEdges
182+
183+
/// Acts as a database of the edges in the graph.
184+
pub struct GraphEdges(pub Option<Handle<File>>);
185+
186+
impl ToAppendable<Edge, Edge> for GraphEdges {
187+
fn get_appendable<'a>(&'a self, value: &'a Edge) -> &'a Edge {
188+
value
189+
}
190+
}
191+
192+
impl Candidates<Edge> for GraphEdges {
192193
fn find_candidates<R>(
193194
&mut self,
194195
graph: &StackGraph,
@@ -198,7 +199,11 @@ impl Candidates<Edge> for () {
198199
) where
199200
R: std::iter::Extend<Edge>,
200201
{
201-
result.extend(graph.outgoing_edges(path.end_node));
202+
result.extend(
203+
graph
204+
.outgoing_edges(path.end_node)
205+
.filter(|e| self.0.map_or(true, |file| graph[e.sink].is_in_file(file))),
206+
);
202207
}
203208
}
204209

@@ -469,6 +474,12 @@ impl std::ops::Index<Handle<PartialPath>> for Database {
469474
}
470475
}
471476

477+
impl ToAppendable<Handle<PartialPath>, PartialPath> for Database {
478+
fn get_appendable<'a>(&'a self, handle: &'a Handle<PartialPath>) -> &'a PartialPath {
479+
&self[*handle]
480+
}
481+
}
482+
472483
impl Candidates<Handle<PartialPath>> for Database {
473484
fn find_candidates<R>(
474485
&mut self,

stack-graphs/tests/it/cycles.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use stack_graphs::partial::Cyclicity;
1414
use stack_graphs::partial::PartialPath;
1515
use stack_graphs::partial::PartialPaths;
1616
use stack_graphs::stitching::Database;
17+
use stack_graphs::stitching::GraphEdges;
1718
use stack_graphs::CancelAfterDuration;
1819
use std::time::Duration;
1920

@@ -169,7 +170,7 @@ fn finding_simple_identity_cycle_is_detected() {
169170
{
170171
let mut edges = Appendables::new();
171172
let mut cd = AppendingCycleDetector::new();
172-
let ctx = &mut ();
173+
let db = &GraphEdges(None);
173174

174175
for edge in &[
175176
edge(r, foo_ref, 0),
@@ -178,15 +179,14 @@ fn finding_simple_identity_cycle_is_detected() {
178179
] {
179180
cd.append(&mut edges, *edge);
180181
assert!(cd
181-
.is_cyclic(&graph, &mut partials, ctx, &mut edges)
182+
.is_cyclic(&graph, &mut partials, db, &mut edges)
182183
.unwrap()
183184
.is_empty());
184185
}
185186
cd.append(&mut edges, edge(foo_def, r, 0));
186187
assert_eq!(
187188
enum_set![Cyclicity::StrengthensPostcondition],
188-
cd.is_cyclic(&graph, &mut partials, ctx, &mut edges)
189-
.unwrap()
189+
cd.is_cyclic(&graph, &mut partials, db, &mut edges).unwrap()
190190
);
191191
}
192192

@@ -259,7 +259,7 @@ fn finding_composite_identity_cycle_is_detected() {
259259
{
260260
let mut edges = Appendables::new();
261261
let mut cd = AppendingCycleDetector::new();
262-
let ctx = &mut ();
262+
let db = &GraphEdges(None);
263263
for edge in &[
264264
edge(r, s, 0),
265265
edge(r, s, 0),
@@ -271,15 +271,14 @@ fn finding_composite_identity_cycle_is_detected() {
271271
] {
272272
cd.append(&mut edges, *edge);
273273
assert!(cd
274-
.is_cyclic(&graph, &mut partials, ctx, &mut edges)
274+
.is_cyclic(&graph, &mut partials, db, &mut edges)
275275
.unwrap()
276276
.is_empty());
277277
}
278278
cd.append(&mut edges, edge(bar_ref, s, 0));
279279
assert_eq!(
280280
enum_set![Cyclicity::StrengthensPostcondition],
281-
cd.is_cyclic(&graph, &mut partials, ctx, &mut edges)
282-
.unwrap()
281+
cd.is_cyclic(&graph, &mut partials, db, &mut edges).unwrap()
283282
);
284283
}
285284

0 commit comments

Comments
 (0)