Skip to content

Commit

Permalink
try_read_wire_{value=>concrete}
Browse files Browse the repository at this point in the history
  • Loading branch information
acl-cqc committed Nov 23, 2024
1 parent 77b6739 commit caa8882
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
11 changes: 4 additions & 7 deletions hugr-passes/src/dataflow/results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,10 @@ impl<V: AbstractValue, C: DFContext<V>> AnalysisResults<V, C> {
/// [PartialValue::Value] or a [PartialValue::PartialSum] with a single possible tag.)
///
/// # Errors
/// `None` if the analysis did not produce a result for that wire
/// `None` if the analysis did not produce a result for that wire, or if
/// the Hugr did not have a [Type](hugr_core::types::Type) for the specified wire
/// `Some(e)` if [conversion to a concrete value](PartialValue::try_into_concrete) failed with error `e`
///
/// # Panics
///
/// If a [Type](hugr_core::types::Type) for the specified wire could not be extracted from the Hugr
pub fn try_read_wire_value<V2, VE, SE>(
pub fn try_read_wire_concrete<V2, VE, SE>(
&self,
w: Wire,
) -> Result<V2, Option<ExtractValueError<V, VE, SE>>>
Expand All @@ -99,7 +96,7 @@ impl<V: AbstractValue, C: DFContext<V>> AnalysisResults<V, C> {
.hugr()
.out_value_types(w.node())
.find(|(p, _)| *p == w.source())
.unwrap();
.ok_or(None)?;
v.try_into_concrete(&typ).map_err(Some)
}
}
Expand Down
14 changes: 8 additions & 6 deletions hugr-passes/src/dataflow/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn test_make_tuple() {

let results = Machine::default().run(TestContext(hugr), []);

let x: Value = results.try_read_wire_value(v3).unwrap();
let x: Value = results.try_read_wire_concrete(v3).unwrap();
assert_eq!(x, Value::tuple([Value::false_val(), Value::true_val()]));
}

Expand All @@ -84,9 +84,9 @@ fn test_unpack_tuple_const() {

let results = Machine::default().run(TestContext(hugr), []);

let o1_r: Value = results.try_read_wire_value(o1).unwrap();
let o1_r: Value = results.try_read_wire_concrete(o1).unwrap();
assert_eq!(o1_r, Value::false_val());
let o2_r: Value = results.try_read_wire_value(o2).unwrap();
let o2_r: Value = results.try_read_wire_concrete(o2).unwrap();
assert_eq!(o2_r, Value::true_val());
}

Expand All @@ -110,7 +110,7 @@ fn test_tail_loop_never_iterates() {

let results = Machine::default().run(TestContext(hugr), []);

let o_r: Value = results.try_read_wire_value(tl_o).unwrap();
let o_r: Value = results.try_read_wire_concrete(tl_o).unwrap();
assert_eq!(o_r, r_v);
assert_eq!(
Some(TailLoopTermination::NeverContinues),
Expand Down Expand Up @@ -298,9 +298,11 @@ fn test_conditional() {
));
let results = Machine::default().run(TestContext(hugr), [(0.into(), arg_pv)]);

let cond_r1: Value = results.try_read_wire_value(cond_o1).unwrap();
let cond_r1: Value = results.try_read_wire_concrete(cond_o1).unwrap();
assert_eq!(cond_r1, Value::false_val());
assert!(results.try_read_wire_value::<Value, _, _>(cond_o2).is_err());
assert!(results
.try_read_wire_concrete::<Value, _, _>(cond_o2)
.is_err());

assert_eq!(results.case_reachable(case1.node()), Some(false)); // arg_pv is variant 1 or 2 only
assert_eq!(results.case_reachable(case2.node()), Some(true));
Expand Down

0 comments on commit caa8882

Please sign in to comment.