Skip to content

Commit 0d0fb80

Browse files
committed
Add parse tests for anonymous enums
1 parent 99317ef commit 0d0fb80

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

tests/ui/parser/anon-enums.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Output of proposed syntax for anonymous enums from https://github.com/rust-lang/rfcs/issues/294.
2+
// https://github.com/rust-lang/rust/issues/100741
3+
4+
fn foo(x: bool | i32) -> i32 | f64 {
5+
//~^ ERROR: function parameters require top-level or-patterns in parentheses
6+
//~| ERROR: expected one of `:`, `@`, or `|`, found `)`
7+
//~| ERROR: expected one of `!`, `(`, `)`, `+`, `,`, `::`, or `<`, found `|`
8+
//~| ERROR: expected one of `!`, `(`, `+`, `::`, `<`, `where`, or `{`, found `|`
9+
match x {
10+
x: i32 => x,
11+
true => 42.,
12+
false => 0.333,
13+
}
14+
}
15+
16+
fn main() {
17+
match foo(true) {
18+
42: i32 => (),
19+
_: f64 => (),
20+
x: i32 => (),
21+
}
22+
}

tests/ui/parser/anon-enums.stderr

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error: function parameters require top-level or-patterns in parentheses
2+
--> $DIR/anon-enums.rs:4:16
3+
|
4+
LL | fn foo(x: bool | i32) -> i32 | f64 {
5+
| ^^^^^
6+
7+
error: expected one of `:`, `@`, or `|`, found `)`
8+
--> $DIR/anon-enums.rs:4:21
9+
|
10+
LL | fn foo(x: bool | i32) -> i32 | f64 {
11+
| ^ expected one of `:`, `@`, or `|`
12+
13+
error: expected one of `!`, `(`, `)`, `+`, `,`, `::`, or `<`, found `|`
14+
--> $DIR/anon-enums.rs:4:16
15+
|
16+
LL | fn foo(x: bool | i32) -> i32 | f64 {
17+
| -^ expected one of 7 possible tokens
18+
| |
19+
| help: missing `,`
20+
21+
error: expected one of `!`, `(`, `+`, `::`, `<`, `where`, or `{`, found `|`
22+
--> $DIR/anon-enums.rs:4:30
23+
|
24+
LL | fn foo(x: bool | i32) -> i32 | f64 {
25+
| ^ expected one of 7 possible tokens
26+
27+
error: aborting due to 4 previous errors
28+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//@ build-pass
2+
// Regression test for https://github.com/rust-lang/rust/issues/107461, where anon enum syntax
3+
// proposed in https://github.com/rust-lang/rfcs/issues/294 being parsed in a naïve way fails with
4+
// exisitng valid syntax for closures.
5+
6+
struct A;
7+
struct B;
8+
9+
#[derive(Debug)]
10+
#[allow(unused)]
11+
struct MyStruct {
12+
x: f32,
13+
}
14+
15+
fn main() {
16+
let x = |_: fn() -> A | B;
17+
let B = x(|| A);
18+
19+
let y = 1.0;
20+
let closure = |f: fn(&f32) -> f32| MyStruct { x: f(&y) };
21+
println!("foo: {:?}", closure(|x| *x + 3.0));
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//@ build-pass
2+
// If we ever introduce anonymous enums (https://github.com/rust-lang/rfcs/issues/294) we need to
3+
// ensure that we don't break existing macros that expect types with `|` between them.
4+
5+
macro_rules! check_ty {
6+
($Z:ty) => { compile_error!("triggered"); };
7+
($X:ty | $Y:ty) => { $X };
8+
}
9+
10+
macro_rules! check {
11+
($Z:ty) => { compile_error!("triggered"); };
12+
($X:ty | $Y:ty) => { };
13+
}
14+
15+
check! { i32 | u8 }
16+
17+
fn foo(x: check_ty! { i32 | u8 }) -> check_ty! { i32 | u8 } {
18+
x
19+
}
20+
fn main() {
21+
let x: check_ty! { i32 | u8 } = 42;
22+
let _: check_ty! { i32 | u8 } = foo(x);
23+
}

0 commit comments

Comments
 (0)