-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #56225 - alexreg:type_alias_enum_variants, r=petrochenkov
Implement RFC 2338, "Type alias enum variants" This PR implements [RFC 2338](rust-lang/rfcs#2338), allowing one to write code like the following. ```rust #![feature(type_alias_enum_variants)] enum Foo { Bar(i32), Baz { i: i32 }, } type Alias = Foo; fn main() { let t = Alias::Bar(0); let t = Alias::Baz { i: 0 }; match t { Alias::Bar(_i) => {} Alias::Baz { i: _i } => {} } } ``` Since `Self` can be considered a type alias in this context, it also enables using `Self::Variant` as both a constructor and pattern. Fixes issues #56199 and #56611. N.B., after discussing the syntax for type arguments on enum variants with @petrochenkov and @eddyb (there are also a few comments on the [tracking issue](#49683)), the consensus seems to be treat the syntax as follows, which ought to be backwards-compatible. ```rust Option::<u8>::None; // OK Option::None::<u8>; // OK, but lint in near future (hard error next edition?) Alias::<u8>::None; // OK Alias::None::<u8>; // Error ``` I do not know if this will need an FCP, but let's start one if so.
- Loading branch information
Showing
111 changed files
with
1,605 additions
and
758 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
src/doc/unstable-book/src/language-features/type-alias-enum-variants.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# `type_alias_enum_variants` | ||
|
||
The tracking issue for this feature is: [#49683] | ||
|
||
[#49683]: https://github.com/rust-lang/rust/issues/49683 | ||
|
||
------------------------ | ||
|
||
The `type_alias_enum_variants` feature enables the use of variants on type | ||
aliases that refer to enums, as both a constructor and a pattern. That is, | ||
it allows for the syntax `EnumAlias::Variant`, which behaves exactly the same | ||
as `Enum::Variant` (assuming that `EnumAlias` is an alias for some enum type | ||
`Enum`). | ||
|
||
Note that since `Self` exists as a type alias, this feature also enables the | ||
use of the syntax `Self::Variant` within an impl block for an enum type. | ||
|
||
```rust | ||
#![feature(type_alias_enum_variants)] | ||
|
||
enum Foo { | ||
Bar(i32), | ||
Baz { i: i32 }, | ||
} | ||
|
||
type Alias = Foo; | ||
|
||
fn main() { | ||
let t = Alias::Bar(0); | ||
let t = Alias::Baz { i: 0 }; | ||
match t { | ||
Alias::Bar(_i) => {} | ||
Alias::Baz { i: _i } => {} | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.