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

Commit b47eebf

Browse files
author
Hendrik van Antwerpen
committed
Do not unwrap in cycle detector, since it cannot guarantee itself that this is safe
1 parent 1e8d06b commit b47eebf

File tree

5 files changed

+28
-24
lines changed

5 files changed

+28
-24
lines changed

stack-graphs/src/cycles.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,20 @@ impl<A: Appendable + Clone> AppendingCycleDetector<A> {
178178
self.appendages.push_front(appendables, appendage);
179179
}
180180

181+
/// Tests if the path is cyclic. Returns a vector indicating the kind of cycles that were found.
182+
/// If appending or concatenating all fragments succeeds, this function will never raise and error.
181183
pub fn is_cyclic(
182184
&self,
183185
graph: &StackGraph,
184186
partials: &mut PartialPaths,
185187
ctx: &mut A::Ctx,
186188
appendables: &mut Appendables<A>,
187-
) -> Vec<Cyclicity> {
189+
) -> Result<Vec<Cyclicity>, PathResolutionError> {
188190
let mut cycles = Vec::new();
189191

190192
let end_node = match self.appendages.clone().pop_front(appendables) {
191193
Some(appendage) => appendage.end_node(ctx),
192-
None => return cycles,
194+
None => return Ok(cycles),
193195
};
194196

195197
let mut maybe_cyclic_path = None;
@@ -207,31 +209,23 @@ impl<A: Appendable + Clone> AppendingCycleDetector<A> {
207209
break;
208210
}
209211
}
210-
None => return cycles,
212+
None => return Ok(cycles),
211213
}
212214
}
213215

214216
// build prefix path -- prefix starts at end_node, because this is a cycle
215217
let mut prefix_path = PartialPath::from_node(graph, partials, end_node);
216218
while let Some(appendage) = prefix_appendages.pop_front(appendables) {
217-
prefix_path
218-
.resolve_to(graph, partials, appendage.start_node(ctx))
219-
.expect("resolving cycle prefix path failed");
220-
appendage
221-
.append_to(graph, partials, ctx, &mut prefix_path)
222-
.expect("appending cycle prefix path failed");
219+
prefix_path.resolve_to(graph, partials, appendage.start_node(ctx))?;
220+
appendage.append_to(graph, partials, ctx, &mut prefix_path)?;
223221
}
224222

225223
// build cyclic path
226224
let cyclic_path = maybe_cyclic_path
227225
.unwrap_or_else(|| PartialPath::from_node(graph, partials, end_node));
228-
prefix_path
229-
.resolve_to(graph, partials, cyclic_path.start_node)
230-
.expect("resolving cyclic path failed");
226+
prefix_path.resolve_to(graph, partials, cyclic_path.start_node)?;
231227
prefix_path.ensure_no_overlapping_variables(partials, &cyclic_path);
232-
prefix_path
233-
.concatenate(graph, partials, &cyclic_path)
234-
.expect("concatenating cyclic path failed ");
228+
prefix_path.concatenate(graph, partials, &cyclic_path)?;
235229
if let Some(cyclicity) = prefix_path.is_cyclic(graph, partials) {
236230
cycles.push(cyclicity);
237231
}

stack-graphs/src/partial.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2743,6 +2743,7 @@ impl PartialPaths {
27432743
}
27442744
} else if !path_cycle_detector
27452745
.is_cyclic(graph, self, &mut (), &mut edges)
2746+
.expect("cyclic test failed when finding partial paths")
27462747
.is_empty()
27472748
{
27482749
copious_debugging!(" * cycle");

stack-graphs/src/paths.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::arena::DequeArena;
2626
use crate::arena::Handle;
2727
use crate::arena::List;
2828
use crate::arena::ListArena;
29-
use crate::cycles::Appendages;
29+
use crate::cycles::Appendables;
3030
use crate::cycles::AppendingCycleDetector;
3131
use crate::cycles::SimilarPathDetector;
3232
use crate::graph::Edge;
@@ -875,7 +875,7 @@ impl Path {
875875
&self,
876876
graph: &StackGraph,
877877
paths: &mut Paths,
878-
edges: &mut Appendages<Edge>,
878+
edges: &mut Appendables<Edge>,
879879
path_cycle_detector: AppendingCycleDetector<Edge>,
880880
result: &mut R,
881881
) {
@@ -924,7 +924,7 @@ impl Paths {
924924
})
925925
.collect::<VecDeque<_>>();
926926
let mut partials = PartialPaths::new();
927-
let mut edges = Appendages::new();
927+
let mut edges = Appendables::new();
928928
while let Some((path, path_cycle_detector)) = queue.pop_front() {
929929
cancellation_flag.check("finding paths")?;
930930
if !similar_path_detector_detector
@@ -936,6 +936,7 @@ impl Paths {
936936
}
937937
if !path_cycle_detector
938938
.is_cyclic(graph, &mut partials, &mut (), &mut edges)
939+
.expect("cyclic test failed when finding paths")
939940
.into_iter()
940941
.all(|c| c == Cyclicity::StrengthensPrecondition)
941942
{

stack-graphs/src/stitching.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ pub struct PathStitcher {
482482
VecDeque<Path>,
483483
VecDeque<AppendingCycleDetector<OwnedOrDatabasePath>>,
484484
),
485-
appended_paths: Appendages<OwnedOrDatabasePath>,
485+
appended_paths: Appendables<OwnedOrDatabasePath>,
486486
cycle_detector: SimilarPathDetector<Path>,
487487
max_work_per_phase: usize,
488488
#[cfg(feature = "copious-debugging")]
@@ -517,7 +517,7 @@ impl PathStitcher {
517517
copious_debugging!(" Initial node {}", node.display(graph));
518518
db.find_candidate_partial_paths_from_node(graph, partials, node, &mut candidate_paths);
519519
}
520-
let mut appended_paths = Appendages::new();
520+
let mut appended_paths = Appendables::new();
521521
let next_iteration = candidate_paths
522522
.iter()
523523
.filter_map(|partial_path| {
@@ -620,8 +620,9 @@ impl PathStitcher {
620620
continue;
621621
}
622622
new_cycle_detector.append(&mut self.appended_paths, extension.into());
623-
let cycles =
624-
new_cycle_detector.is_cyclic(graph, partials, db, &mut self.appended_paths);
623+
let cycles = new_cycle_detector
624+
.is_cyclic(graph, partials, db, &mut self.appended_paths)
625+
.expect("cyclic test failed when stitching paths");
625626
if !cycles
626627
.into_iter()
627628
.all(|c| c == Cyclicity::StrengthensPrecondition)
@@ -963,8 +964,9 @@ impl ForwardPartialPathStitcher {
963964
continue;
964965
}
965966
new_cycle_detector.append(&mut self.appended_paths, extension.into());
966-
let cycles =
967-
new_cycle_detector.is_cyclic(graph, partials, db, &mut self.appended_paths);
967+
let cycles = new_cycle_detector
968+
.is_cyclic(graph, partials, db, &mut self.appended_paths)
969+
.expect("cyclic test failed when stitching partial paths");
968970
if !cycles
969971
.into_iter()
970972
.all(|c| c == Cyclicity::StrengthensPrecondition)

stack-graphs/tests/it/cycles.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,14 @@ fn finding_simple_identity_cycle_is_detected() {
177177
cd.append(&mut edges, *edge);
178178
assert!(cd
179179
.is_cyclic(&graph, &mut partials, ctx, &mut edges)
180+
.unwrap()
180181
.is_empty());
181182
}
182183
cd.append(&mut edges, edge(foo_def, r, 0));
183184
assert_eq!(
184185
vec![Cyclicity::StrengthensPostcondition],
185186
cd.is_cyclic(&graph, &mut partials, ctx, &mut edges)
187+
.unwrap()
186188
);
187189
}
188190

@@ -227,6 +229,7 @@ fn stitching_simple_identity_cycle_is_detected() {
227229
assert_eq!(
228230
vec![Cyclicity::StrengthensPostcondition],
229231
cd.is_cyclic(&graph, &mut partials, &mut db, &mut paths)
232+
.unwrap()
230233
);
231234
}
232235
}
@@ -267,12 +270,14 @@ fn finding_composite_identity_cycle_is_detected() {
267270
cd.append(&mut edges, *edge);
268271
assert!(cd
269272
.is_cyclic(&graph, &mut partials, ctx, &mut edges)
273+
.unwrap()
270274
.is_empty());
271275
}
272276
cd.append(&mut edges, edge(bar_ref, s, 0));
273277
assert_eq!(
274278
vec![Cyclicity::StrengthensPostcondition],
275279
cd.is_cyclic(&graph, &mut partials, ctx, &mut edges)
280+
.unwrap()
276281
);
277282
}
278283

@@ -320,6 +325,7 @@ fn stitching_composite_identity_cycle_is_detected() {
320325
assert_eq!(
321326
vec![Cyclicity::StrengthensPostcondition],
322327
cd.is_cyclic(&graph, &mut partials, &mut db, &mut paths)
328+
.unwrap()
323329
);
324330
}
325331
}

0 commit comments

Comments
 (0)