Skip to content

Commit 5748825

Browse files
committed
Auto merge of #61682 - Centril:stabilize-type_alias_enum_variants, r=petrochenkov
Stabilize `type_alias_enum_variants` in Rust 1.37.0 Stabilize `#![feature(type_alias_enum_variants)]` which allows type-relative resolution with highest priority to `enum` variants in both expression and pattern contexts. For example, you may now write: ```rust enum Option<T> { None, Some(T), } type OptAlias<T> = Option<T>; fn work_on_alias(x: Option<u8>) -> u8 { match x { OptAlias::Some(y) => y + 1, OptAlias::None => 0, } } ``` Closes rust-lang/rfcs#2218 Closes #52118 r? @petrochenkov
2 parents 0af8e87 + 57e6869 commit 5748825

35 files changed

+399
-323
lines changed

src/doc/unstable-book/src/language-features/type-alias-enum-variants.md

-36
This file was deleted.

src/librustc_resolve/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#![feature(label_break_value)]
77
#![feature(nll)]
88
#![feature(rustc_diagnostic_macros)]
9-
#![feature(type_alias_enum_variants)]
9+
#![cfg_attr(bootstrap, feature(type_alias_enum_variants))]
1010

1111
#![recursion_limit="256"]
1212

src/librustc_typeck/astconv.rs

-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ use std::collections::BTreeSet;
3434
use std::iter;
3535
use std::slice;
3636

37-
use super::{check_type_alias_enum_variants_enabled};
3837
use rustc_data_structures::fx::FxHashSet;
3938

4039
#[derive(Debug)]
@@ -1595,7 +1594,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
15951594
});
15961595
if let Some(variant_def) = variant_def {
15971596
if permit_variants {
1598-
check_type_alias_enum_variants_enabled(tcx, span);
15991597
tcx.check_stability(variant_def.def_id, Some(hir_ref_id), span);
16001598
return Ok((qself_ty, DefKind::Variant, variant_def.def_id));
16011599
} else {

src/librustc_typeck/check/method/mod.rs

-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use rustc::infer::{self, InferOk};
2626
use syntax::ast;
2727
use syntax_pos::Span;
2828

29-
use crate::{check_type_alias_enum_variants_enabled};
3029
use self::probe::{IsSuggestion, ProbeScope};
3130

3231
pub fn provide(providers: &mut ty::query::Providers<'_>) {
@@ -417,8 +416,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
417416
tcx.hygienic_eq(method_name, vd.ident, adt_def.did)
418417
});
419418
if let Some(variant_def) = variant_def {
420-
check_type_alias_enum_variants_enabled(tcx, span);
421-
422419
// Braced variants generate unusable names in value namespace (reserved for
423420
// possible future use), so variants resolved as associated items may refer to
424421
// them as well. It's ok to use the variant's id as a ctor id since an

src/librustc_typeck/lib.rs

+1-16
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ use rustc::lint;
105105
use rustc::middle;
106106
use rustc::session;
107107
use rustc::util::common::ErrorReported;
108-
use rustc::session::config::{EntryFnType, nightly_options};
108+
use rustc::session::config::EntryFnType;
109109
use rustc::traits::{ObligationCause, ObligationCauseCode, TraitEngine, TraitEngineExt};
110110
use rustc::ty::subst::SubstsRef;
111111
use rustc::ty::{self, Ty, TyCtxt};
@@ -124,21 +124,6 @@ pub struct TypeAndSubsts<'tcx> {
124124
ty: Ty<'tcx>,
125125
}
126126

127-
fn check_type_alias_enum_variants_enabled<'tcx>(tcx: TyCtxt<'tcx>, span: Span) {
128-
if !tcx.features().type_alias_enum_variants {
129-
let mut err = tcx.sess.struct_span_err(
130-
span,
131-
"enum variants on type aliases are experimental"
132-
);
133-
if nightly_options::is_nightly_build() {
134-
help!(&mut err,
135-
"add `#![feature(type_alias_enum_variants)]` to the \
136-
crate attributes to enable");
137-
}
138-
err.emit();
139-
}
140-
}
141-
142127
fn require_c_abi_if_c_variadic(tcx: TyCtxt<'_>, decl: &hir::FnDecl, abi: Abi, span: Span) {
143128
if decl.c_variadic && !(abi == Abi::C || abi == Abi::Cdecl) {
144129
let mut err = struct_span_err!(tcx.sess, span, E0045,

src/libsyntax/feature_gate.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -530,9 +530,6 @@ declare_features! (
530530
// Allows using `reason` in lint attributes and the `#[expect(lint)]` lint check.
531531
(active, lint_reasons, "1.31.0", Some(54503), None),
532532

533-
// Allows paths to enum variants on type aliases.
534-
(active, type_alias_enum_variants, "1.31.0", Some(49683), None),
535-
536533
// Allows exhaustive integer pattern matching on `usize` and `isize`.
537534
(active, precise_pointer_size_matching, "1.32.0", Some(56354), None),
538535

@@ -853,6 +850,8 @@ declare_features! (
853850
(accepted, extern_crate_self, "1.34.0", Some(56409), None),
854851
// Allows arbitrary delimited token streams in non-macro attributes.
855852
(accepted, unrestricted_attribute_tokens, "1.34.0", Some(55208), None),
853+
// Allows paths to enum variants on type aliases including `Self`.
854+
(accepted, type_alias_enum_variants, "1.37.0", Some(49683), None),
856855
// Allows using `#[repr(align(X))]` on enums with equivalent semantics
857856
// to wrapping an enum in a wrapper struct with `#[repr(align(X))]`.
858857
(accepted, repr_align_enum, "1.37.0", Some(57996), None),

src/test/run-pass/type-alias-enum-variants-2.rs

-30
This file was deleted.

src/test/run-pass/type-alias-enum-variants.rs

-30
This file was deleted.

src/test/ui/feature-gates/feature-gate-type_alias_enum_variants.rs

-19
This file was deleted.

src/test/ui/feature-gates/feature-gate-type_alias_enum_variants.stderr

-34
This file was deleted.

src/test/ui/type-alias-enum-variants-panic.rs

-17
This file was deleted.

src/test/ui/type-alias-enum-variants-panic.stderr

-21
This file was deleted.

src/test/ui/type-alias-enum-variants-priority-2.rs

-13
This file was deleted.

src/test/ui/type-alias-enum-variants-priority-2.stderr

-12
This file was deleted.

src/test/ui/type-alias-enum-variants-priority-3.rs

-10
This file was deleted.

src/test/ui/type-alias-enum-variants-priority.rs

-19
This file was deleted.

src/test/ui/type-alias-enum-variants.rs

-11
This file was deleted.

src/test/ui/pattern/enum-variant-generic-args.rs src/test/ui/type-alias-enum-variants/enum-variant-generic-args-pass.rs

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
// run-pass
22

3-
#![feature(type_alias_enum_variants)]
3+
// Check that resolving, in the value namespace, to an `enum` variant
4+
// through a type alias is well behaved in the presence of generics.
5+
// We check for situations with:
6+
// 1. a generic type `Alias<T>`, we can type-apply `Alias` when referring to a variant.
7+
// 2. a monotype `AliasFixed` of generic `Enum<T>`, we can refer to variants
8+
// and the type-application of `T` in `AliasFixed` is kept.
49

510
#![allow(irrefutable_let_patterns)]
611

7-
#[allow(dead_code)]
8-
enum Enum<T> { TSVariant(T), SVariant { v: T } }
12+
enum Enum<T> { TSVariant(T), SVariant { v: T }, UVariant }
913
type Alias<T> = Enum<T>;
1014
type AliasFixed = Enum<()>;
1115

1216
macro_rules! is_variant {
1317
(TSVariant, $expr:expr) => (is_variant!(@check TSVariant, (_), $expr));
1418
(SVariant, $expr:expr) => (is_variant!(@check SVariant, { v: _ }, $expr));
19+
(UVariant, $expr:expr) => (is_variant!(@check UVariant, {}, $expr));
1520
(@check $variant:ident, $matcher:tt, $expr:expr) => (
1621
assert!(if let Enum::$variant::<()> $matcher = $expr { true } else { false },
1722
"expr does not have correct type");
@@ -40,4 +45,15 @@ fn main() {
4045
is_variant!(SVariant, Alias::<()>::SVariant { v: () });
4146

4247
is_variant!(SVariant, AliasFixed::SVariant { v: () });
48+
49+
// Unit variant
50+
51+
is_variant!(UVariant, Enum::UVariant);
52+
is_variant!(UVariant, Enum::UVariant::<()>);
53+
is_variant!(UVariant, Enum::<()>::UVariant);
54+
55+
is_variant!(UVariant, Alias::UVariant);
56+
is_variant!(UVariant, Alias::<()>::UVariant);
57+
58+
is_variant!(UVariant, AliasFixed::UVariant);
4359
}

0 commit comments

Comments
 (0)