Skip to content

Commit

Permalink
add more compile-fail tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MingweiSamuel committed Aug 22, 2024
1 parent 59ccd55 commit 648004b
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 2 deletions.
5 changes: 4 additions & 1 deletion hydroflow/src/util/demux_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ pub trait DemuxEnum<Outputs>: DemuxEnumBase {
}

/// Special case of [`DemuxEnum`] for when there is only one variant.
#[diagnostic::on_unimplemented(note = "requires that the enum only have one variant.")]
#[diagnostic::on_unimplemented(
note = "requires that the enum have only one variant.",
note = "ensure there are no missing outputs; there must be exactly one output for each enum variant."
)]
pub trait SingleVariant: DemuxEnumBase {
/// Output tuple type.
type Output;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use hydroflow::hydroflow_syntax;
use hydroflow::util::demux_enum::DemuxEnum;

fn main() {
#[derive(DemuxEnum)]
enum Shape {
Square(f64),
}

let mut df = hydroflow_syntax! {
my_demux = source_iter([
Shape::Square(9.0),
]) -> demux_enum::<Shape>();
my_demux[Square] -> for_each(std::mem::drop);
my_demux[Square] -> for_each(std::mem::drop);
};
df.run_available();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: Output connection conflicts with below ($DIR/tests/compile-fail/surface_demuxenum_port_duplicate_one.rs:15:18) (1/2)
--> tests/compile-fail/surface_demuxenum_port_duplicate_one.rs:14:18
|
14 | my_demux[Square] -> for_each(std::mem::drop);
| ^^^^^^

error: Output connection conflicts with above ($DIR/tests/compile-fail/surface_demuxenum_port_duplicate_one.rs:14:18) (2/2)
--> tests/compile-fail/surface_demuxenum_port_duplicate_one.rs:15:18
|
15 | my_demux[Square] -> for_each(std::mem::drop);
| ^^^^^^
14 changes: 14 additions & 0 deletions hydroflow/tests/compile-fail/surface_demuxenum_port_extra_zero.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use hydroflow::util::demux_enum::DemuxEnum;
use hydroflow::hydroflow_syntax;

fn main() {
#[derive(DemuxEnum)]
enum Shape {
}

let mut df = hydroflow_syntax! {
my_demux = source_iter([]) -> demux_enum::<Shape>();
my_demux[Square] -> for_each(std::mem::drop);
};
df.run_available();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
error[E0599]: no variant named `Square` found for enum `Shape`
--> tests/compile-fail/surface_demuxenum_port_extra_zero.rs:11:18
|
6 | enum Shape {
| ---------- variant `Square` not found here
...
11 | my_demux[Square] -> for_each(std::mem::drop);
| ^^^^^^ variant not found in `Shape`

error[E0277]: the trait bound `Shape: SingleVariant` is not satisfied
--> tests/compile-fail/surface_demuxenum_port_extra_zero.rs:10:52
|
10 | my_demux = source_iter([]) -> demux_enum::<Shape>();
| ^^^^^ the trait `SingleVariant` is not implemented for `Shape`
|
= note: requires that the enum have only one variant.
= note: ensure there are no missing outputs; there must be exactly one output for each enum variant.

error[E0277]: the trait bound `Shape: SingleVariant` is not satisfied
--> tests/compile-fail/surface_demuxenum_port_extra_zero.rs:10:39
|
10 | my_demux = source_iter([]) -> demux_enum::<Shape>();
| ^^^^^^^^^^^^^^^^^^^^^ the trait `SingleVariant` is not implemented for `Shape`
|
= note: requires that the enum have only one variant.
= note: ensure there are no missing outputs; there must be exactly one output for each enum variant.

error[E0277]: the trait bound `Shape: SingleVariant` is not satisfied
--> tests/compile-fail/surface_demuxenum_port_extra_zero.rs:10:39
|
10 | my_demux = source_iter([]) -> demux_enum::<Shape>();
| _______________________________________^
11 | | my_demux[Square] -> for_each(std::mem::drop);
| |____________________________________________________^ the trait `SingleVariant` is not implemented for `Shape`
|
= note: requires that the enum have only one variant.
= note: ensure there are no missing outputs; there must be exactly one output for each enum variant.
19 changes: 19 additions & 0 deletions hydroflow/tests/compile-fail/surface_demuxenum_port_missing_one.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use hydroflow::util::demux_enum::DemuxEnum;
use hydroflow::hydroflow_syntax;

fn main() {
#[derive(DemuxEnum)]
enum Shape {
Square(f64),
Rectangle { w: f64, h: f64 },
}

let mut df = hydroflow_syntax! {
my_demux = source_iter([
Shape::Rectangle { w: 10.0, h: 8.0 },
Shape::Square(9.0),
]) -> demux_enum::<Shape>();
my_demux[Rectangle] -> for_each(std::mem::drop);
};
df.run_available();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
error[E0277]: the trait bound `Shape: SingleVariant` is not satisfied
--> tests/compile-fail/surface_demuxenum_port_missing_one.rs:15:28
|
15 | ]) -> demux_enum::<Shape>();
| ^^^^^ the trait `SingleVariant` is not implemented for `Shape`
|
= note: requires that the enum have only one variant.
= note: ensure there are no missing outputs; there must be exactly one output for each enum variant.

error[E0277]: the trait bound `Shape: SingleVariant` is not satisfied
--> tests/compile-fail/surface_demuxenum_port_missing_one.rs:15:15
|
15 | ]) -> demux_enum::<Shape>();
| ^^^^^^^^^^^^^^^^^^^^^ the trait `SingleVariant` is not implemented for `Shape`
|
= note: requires that the enum have only one variant.
= note: ensure there are no missing outputs; there must be exactly one output for each enum variant.

error[E0277]: the trait bound `Shape: SingleVariant` is not satisfied
--> tests/compile-fail/surface_demuxenum_port_missing_one.rs:15:15
|
15 | ]) -> demux_enum::<Shape>();
| _______________^
16 | | my_demux[Rectangle] -> for_each(std::mem::drop);
| |_______________________________________________________^ the trait `SingleVariant` is not implemented for `Shape`
|
= note: requires that the enum have only one variant.
= note: ensure there are no missing outputs; there must be exactly one output for each enum variant.
17 changes: 17 additions & 0 deletions hydroflow/tests/compile-fail/surface_demuxenum_port_wrong_one.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use hydroflow::util::demux_enum::DemuxEnum;
use hydroflow::hydroflow_syntax;

fn main() {
#[derive(DemuxEnum)]
enum Shape {
Square(f64),
}

let mut df = hydroflow_syntax! {
my_demux = source_iter([
Shape::Square(9.0),
]) -> demux_enum::<Shape>();
my_demux[Circle] -> for_each(std::mem::drop);
};
df.run_available();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error[E0599]: no variant named `Circle` found for enum `Shape`
--> tests/compile-fail/surface_demuxenum_port_wrong_one.rs:14:18
|
6 | enum Shape {
| ---------- variant `Circle` not found here
...
14 | my_demux[Circle] -> for_each(std::mem::drop);
| ^^^^^^ variant not found in `Shape`
3 changes: 2 additions & 1 deletion hydroflow_lang/src/graph/ops/demux_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ pub const DEMUX_ENUM: OperatorConstraints = OperatorConstraints {
let write_iterator = match outputs.len() {
// 0 => super::null_write_iterator_fn(wc),
1 => {
let map_fn = quote_spanned! {op_span=>
// Use `enum_type`'s span.
let map_fn = quote_spanned! {enum_type.span()=>
<#enum_type as #root::util::demux_enum::SingleVariant>::single_variant
};
if is_pull {
Expand Down

0 comments on commit 648004b

Please sign in to comment.