Skip to content

Commit 77ff384

Browse files
committed
type-alias-enum-variants-pass: harden; account for unit variants.
1 parent d87cae3 commit 77ff384

File tree

1 file changed

+51
-12
lines changed

1 file changed

+51
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,69 @@
11
// run-pass
22

3+
// Check that it is possible to resolve, in the value namespace,
4+
// to an `enum` variant through a type alias. This includes `Self`.
5+
// Type qualified syntax `<Type>::Variant` also works when syntactically valid.
6+
37
#[derive(Debug, PartialEq, Eq)]
48
enum Foo {
59
Bar(i32),
610
Baz { i: i32 },
11+
Qux,
712
}
813

914
type FooAlias = Foo;
1015
type OptionAlias = Option<i32>;
1116

17+
macro_rules! check_pat {
18+
($x:expr, $p:pat) => {
19+
assert!(if let $p = $x { true } else { false });
20+
};
21+
}
22+
1223
impl Foo {
13-
fn foo() -> Self {
14-
Self::Bar(3)
24+
fn bar() -> Self {
25+
let x = Self::Bar(3);
26+
assert_eq!(x, <Self>::Bar(3));
27+
check_pat!(x, Self::Bar(3));
28+
x
29+
}
30+
31+
fn baz() -> Self {
32+
let x = Self::Baz { i: 42 };
33+
check_pat!(x, Self::Baz { i: 42 });
34+
x
35+
}
36+
37+
fn qux() -> Self {
38+
let x = Self::Qux;
39+
assert_eq!(x, <Self>::Qux);
40+
check_pat!(x, Self::Qux);
41+
check_pat!(x, <Self>::Qux);
42+
x
1543
}
1644
}
1745

1846
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));
47+
let bar = Foo::Bar(1);
48+
assert_eq!(bar, FooAlias::Bar(1));
49+
assert_eq!(bar, <FooAlias>::Bar(1));
50+
check_pat!(bar, FooAlias::Bar(1));
51+
52+
let baz = FooAlias::Baz { i: 2 };
53+
assert_eq!(baz, Foo::Baz { i: 2 });
54+
check_pat!(baz, FooAlias::Baz { i: 2 });
55+
56+
let qux = Foo::Qux;
57+
assert_eq!(qux, FooAlias::Qux);
58+
assert_eq!(qux, <FooAlias>::Qux);
59+
check_pat!(qux, FooAlias::Qux);
60+
check_pat!(qux, <FooAlias>::Qux);
61+
62+
assert_eq!(Foo::bar(), Foo::Bar(3));
63+
assert_eq!(Foo::baz(), Foo::Baz { i: 42 });
64+
assert_eq!(Foo::qux(), Foo::Qux);
2865

29-
assert_eq!(OptionAlias::Some(4), Option::Some(4));
66+
let some = Option::Some(4);
67+
assert_eq!(some, OptionAlias::Some(4));
68+
check_pat!(some, OptionAlias::Some(4));
3069
}

0 commit comments

Comments
 (0)