Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for #[attr] if #89

Merged
merged 1 commit into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ auto_enums_derive = { version = "=0.7.2", path = "derive", default-features = fa

[dev-dependencies]
trybuild = "1.0"
rustversion = "1.0"
futures_crate = { version = "0.3", package = "futures" }
rayon_crate = { version = "1.2", package = "rayon" }
serde_crate = { version = "1.0", package = "serde" }
Expand Down
23 changes: 23 additions & 0 deletions tests/auto_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,11 @@ mod stable {
}
assert!(try_operator(None).unwrap_err().source().is_some());
}

// This hack is needed until https://github.com/rust-lang/rust/pull/69201
// makes it way into stable.
#[rustversion::nightly]
include!("auto_enum_if_attr.rs.in");
}

// nightly
Expand Down Expand Up @@ -706,4 +711,22 @@ mod nightly {
assert_eq!(marker2(i).clone().sum::<i32>(), *x - 1);
}
}

#[test]
fn non_stmt_expr() {
fn match_(x: bool) -> Option<impl Iterator<Item = u8>> {
Some(
#[auto_enum(Iterator)]
match x {
true => std::iter::once(0),
_ => std::iter::repeat(1),
},
)
}
}

// This hack is needed until https://github.com/rust-lang/rust/pull/69201
// makes it way into stable.
#[rustversion::nightly]
include!("auto_enum_if_attr_unstable.rs.in");
}
23 changes: 23 additions & 0 deletions tests/auto_enum_if_attr.rs.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// FIXME: Once https://github.com/rust-lang/rust/pull/69201 makes its
// way into stable, move this back into `auto_enum.rs

#[auto_enum]
fn a(x: bool) -> impl Iterator<Item = u8> {
let res = {
#[auto_enum(Iterator)]
if x { std::iter::once(0) } else { std::iter::repeat(1) }
};
res
}

#[auto_enum(Iterator)]
fn if_in_if(x: usize) -> impl Iterator<Item = i32> {
if x == 0 {
1..8
} else if x > 3 {
#[nested]
if x > 4 { 2..=10 } else { (11..20).map(|x| x - 1) }
} else {
(0..2).map(|x| x + 1)
}
}
9 changes: 9 additions & 0 deletions tests/auto_enum_if_attr_unstable.rs.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// FIXME: Once https://github.com/rust-lang/rust/pull/69201 makes its
// way into stable, move this back into `auto_enum.rs

fn if_(x: bool) -> Option<impl Iterator<Item = u8>> {
Some(
#[auto_enum(Iterator)]
if x { std::iter::once(0) } else { std::iter::repeat(1) },
)
}
22 changes: 22 additions & 0 deletions tests/ui/auto_enum/non-stmt-expr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use auto_enums::auto_enum;

#[auto_enum]
fn b(x: bool) -> Option<impl Iterator<Item = u8>> {
Some(
#[auto_enum(Iterator)]
if x { std::iter::once(0) } else { std::iter::repeat(1) },
)
}

#[auto_enum]
fn c(x: bool) -> Option<impl Iterator<Item = u8>> {
Some(
#[auto_enum(Iterator)]
match x {
true => std::iter::once(0),
_ => std::iter::repeat(1),
},
)
}

fn main() {}
22 changes: 22 additions & 0 deletions tests/ui/auto_enum/non-stmt-expr.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error: `#[auto_enum]` is required two or more branches or marker macros in total, there is no branch or marker macro in this statement
--> $DIR/non-stmt-expr.rs:4:1
|
4 | / fn b(x: bool) -> Option<impl Iterator<Item = u8>> {
5 | | Some(
6 | | #[auto_enum(Iterator)]
7 | | if x { std::iter::once(0) } else { std::iter::repeat(1) },
8 | | )
9 | | }
| |_^

error: `#[auto_enum]` is required two or more branches or marker macros in total, there is no branch or marker macro in this statement
--> $DIR/non-stmt-expr.rs:12:1
|
12 | / fn c(x: bool) -> Option<impl Iterator<Item = u8>> {
13 | | Some(
14 | | #[auto_enum(Iterator)]
15 | | match x {
... |
19 | | )
20 | | }
| |_^
12 changes: 0 additions & 12 deletions tests/ui/auto_enum/rejected-by-rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,4 @@ fn wrong_if(x: usize) -> impl Iterator<Item = i32> {
}
}

#[auto_enum(Iterator)]
fn if_in_if(x: usize) -> impl Iterator<Item = i32> {
if x == 0 {
1..8
} else if x > 3 {
#[nested]
if x > 4 { 2..=10 } else { (11..20).map(|x| x - 1) }
} else {
(0..2).map(|x| x + 1)
}
}

fn main() {}