Skip to content

Commit 37e7f0a

Browse files
author
Alexander Regueiro
committed
Expanded tests for enum variants with generic args.
1 parent 66409e0 commit 37e7f0a

8 files changed

+419
-139
lines changed

src/test/run-pass/enum-generic-args.rs

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#![feature(irrefutable_let_patterns)]
2+
#![feature(type_alias_enum_variants)]
3+
4+
#![allow(irrefutable_let_patterns)]
5+
6+
enum Enum<T> { TSVariant(T), SVariant { v: T } }
7+
type Alias<T> = Enum<T>;
8+
type AliasFixed = Enum<()>;
9+
10+
macro_rules! is_variant {
11+
(TSVariant, $expr:expr) => (is_variant!(@TSVariant, (_), $expr));
12+
(SVariant, $expr:expr) => (is_variant!(@SVariant, { v: _ }, $expr));
13+
(@$variant:ident, $matcher:tt, $expr:expr) => (
14+
assert!(if let Enum::$variant::<()> $matcher = $expr { true } else { false },
15+
"expr does not have correct type");
16+
);
17+
}
18+
19+
impl<T> Enum<T> {
20+
fn ts_variant() {
21+
is_variant!(TSVariant, Self::TSVariant(()));
22+
}
23+
24+
fn s_variant() {
25+
is_variant!(SVariant, Self::SVariant { v: () });
26+
}
27+
}
28+
29+
fn main() {
30+
// Tuple struct variant
31+
32+
is_variant!(TSVariant, Enum::TSVariant(()));
33+
is_variant!(TSVariant, Enum::TSVariant::<()>(()));
34+
is_variant!(TSVariant, Enum::<()>::TSVariant(()));
35+
36+
is_variant!(TSVariant, Alias::TSVariant(()));
37+
is_variant!(TSVariant, Alias::<()>::TSVariant(()));
38+
39+
is_variant!(TSVariant, AliasFixed::TSVariant(()));
40+
41+
Enum::<()>::ts_variant();
42+
43+
// Struct variant
44+
45+
is_variant!(SVariant, Enum::SVariant { v: () });
46+
is_variant!(SVariant, Enum::SVariant::<()> { v: () });
47+
is_variant!(SVariant, Enum::<()>::SVariant { v: () });
48+
49+
is_variant!(SVariant, Alias::SVariant { v: () });
50+
is_variant!(SVariant, Alias::<()>::SVariant { v: () });
51+
52+
is_variant!(SVariant, AliasFixed::SVariant { v: () });
53+
54+
Enum::<()>::s_variant();
55+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#![feature(type_alias_enum_variants)]
2+
3+
#[derive(Debug, PartialEq, Eq)]
4+
enum Foo {
5+
Bar(i32),
6+
Baz { i: i32 },
7+
}
8+
9+
type FooAlias = Foo;
10+
type OptionAlias = Option<i32>;
11+
12+
impl Foo {
13+
fn foo() -> Self {
14+
Self::Bar(3)
15+
}
16+
}
17+
18+
fn main() {
19+
let t = FooAlias::Bar(1);
20+
assert_eq!(t, Foo::Bar(1));
21+
let t = FooAlias::Baz { i: 2 };
22+
assert_eq!(t, Foo::Baz { i: 2 });
23+
match t {
24+
FooAlias::Bar(_i) => {}
25+
FooAlias::Baz { i } => { assert_eq!(i, 2); }
26+
}
27+
assert_eq!(Foo::foo(), Foo::Bar(3));
28+
29+
assert_eq!(OptionAlias::Some(4), Option::Some(4));
30+
}

src/test/ui/enum-generic-args.rs

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/test/ui/enum-generic-args.stderr

Lines changed: 0 additions & 70 deletions
This file was deleted.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#![feature(type_alias_enum_variants)]
2+
3+
enum Enum<T> { TSVariant(T), SVariant { v: T } }
4+
type Alias<T> = Enum<T>;
5+
type AliasFixed = Enum<()>;
6+
7+
impl<T> Enum<T> {
8+
fn ts_variant() {
9+
Self::TSVariant::<()>(());
10+
//~^ ERROR type parameters are not allowed on this type [E0109]
11+
Self::<()>::TSVariant(());
12+
//~^ ERROR type parameters are not allowed on this type [E0109]
13+
Self::<()>::TSVariant::<()>(());
14+
//~^ ERROR type parameters are not allowed on this type [E0109]
15+
//~^^ ERROR type parameters are not allowed on this type [E0109]
16+
}
17+
18+
fn s_variant() {
19+
Self::SVariant::<()>(());
20+
//~^ ERROR type parameters are not allowed on this type [E0109]
21+
Self::<()>::SVariant(());
22+
//~^ ERROR type parameters are not allowed on this type [E0109]
23+
Self::<()>::SVariant::<()>(());
24+
//~^ ERROR type parameters are not allowed on this type [E0109]
25+
//~^^ ERROR type parameters are not allowed on this type [E0109]
26+
}
27+
}
28+
29+
fn main() {
30+
// Tuple struct variant
31+
32+
Enum::<()>::TSVariant::<()>(());
33+
//~^ ERROR type parameters are not allowed on this type [E0109]
34+
35+
Alias::TSVariant::<()>(());
36+
//~^ ERROR type parameters are not allowed on this type [E0109]
37+
Alias::<()>::TSVariant::<()>(());
38+
//~^ ERROR type parameters are not allowed on this type [E0109]
39+
40+
AliasFixed::TSVariant::<()>(());
41+
//~^ ERROR type parameters are not allowed on this type [E0109]
42+
AliasFixed::<()>::TSVariant(());
43+
//~^ ERROR wrong number of type arguments: expected 0, found 1 [E0107]
44+
AliasFixed::<()>::TSVariant::<()>(());
45+
//~^ ERROR type parameters are not allowed on this type [E0109]
46+
//~^^ ERROR wrong number of type arguments: expected 0, found 1 [E0107]
47+
48+
// Struct variant
49+
50+
Enum::<()>::SVariant::<()>(());
51+
//~^ ERROR type parameters are not allowed on this type [E0109]
52+
53+
Alias::SVariant::<()>(());
54+
//~^ ERROR type parameters are not allowed on this type [E0109]
55+
Alias::<()>::SVariant::<()>(());
56+
//~^ ERROR type parameters are not allowed on this type [E0109]
57+
58+
AliasFixed::SVariant::<()>(());
59+
//~^ ERROR type parameters are not allowed on this type [E0109]
60+
AliasFixed::<()>::SVariant(());
61+
//~^ ERROR wrong number of type arguments: expected 0, found 1 [E0107]
62+
AliasFixed::<()>::SVariant::<()>(());
63+
//~^ ERROR type parameters are not allowed on this type [E0109]
64+
//~^^ ERROR wrong number of type arguments: expected 0, found 1 [E0107]
65+
}

0 commit comments

Comments
 (0)