Skip to content

Commit

Permalink
Auto merge of #61682 - Centril:stabilize-type_alias_enum_variants, r=…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
bors committed Jul 1, 2019
2 parents 0af8e87 + 57e6869 commit 5748825
Show file tree
Hide file tree
Showing 35 changed files with 399 additions and 323 deletions.

This file was deleted.

2 changes: 1 addition & 1 deletion src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#![feature(label_break_value)]
#![feature(nll)]
#![feature(rustc_diagnostic_macros)]
#![feature(type_alias_enum_variants)]
#![cfg_attr(bootstrap, feature(type_alias_enum_variants))]

#![recursion_limit="256"]

Expand Down
2 changes: 0 additions & 2 deletions src/librustc_typeck/astconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ use std::collections::BTreeSet;
use std::iter;
use std::slice;

use super::{check_type_alias_enum_variants_enabled};
use rustc_data_structures::fx::FxHashSet;

#[derive(Debug)]
Expand Down Expand Up @@ -1595,7 +1594,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
});
if let Some(variant_def) = variant_def {
if permit_variants {
check_type_alias_enum_variants_enabled(tcx, span);
tcx.check_stability(variant_def.def_id, Some(hir_ref_id), span);
return Ok((qself_ty, DefKind::Variant, variant_def.def_id));
} else {
Expand Down
3 changes: 0 additions & 3 deletions src/librustc_typeck/check/method/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use rustc::infer::{self, InferOk};
use syntax::ast;
use syntax_pos::Span;

use crate::{check_type_alias_enum_variants_enabled};
use self::probe::{IsSuggestion, ProbeScope};

pub fn provide(providers: &mut ty::query::Providers<'_>) {
Expand Down Expand Up @@ -417,8 +416,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
tcx.hygienic_eq(method_name, vd.ident, adt_def.did)
});
if let Some(variant_def) = variant_def {
check_type_alias_enum_variants_enabled(tcx, span);

// Braced variants generate unusable names in value namespace (reserved for
// possible future use), so variants resolved as associated items may refer to
// them as well. It's ok to use the variant's id as a ctor id since an
Expand Down
17 changes: 1 addition & 16 deletions src/librustc_typeck/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ use rustc::lint;
use rustc::middle;
use rustc::session;
use rustc::util::common::ErrorReported;
use rustc::session::config::{EntryFnType, nightly_options};
use rustc::session::config::EntryFnType;
use rustc::traits::{ObligationCause, ObligationCauseCode, TraitEngine, TraitEngineExt};
use rustc::ty::subst::SubstsRef;
use rustc::ty::{self, Ty, TyCtxt};
Expand All @@ -124,21 +124,6 @@ pub struct TypeAndSubsts<'tcx> {
ty: Ty<'tcx>,
}

fn check_type_alias_enum_variants_enabled<'tcx>(tcx: TyCtxt<'tcx>, span: Span) {
if !tcx.features().type_alias_enum_variants {
let mut err = tcx.sess.struct_span_err(
span,
"enum variants on type aliases are experimental"
);
if nightly_options::is_nightly_build() {
help!(&mut err,
"add `#![feature(type_alias_enum_variants)]` to the \
crate attributes to enable");
}
err.emit();
}
}

fn require_c_abi_if_c_variadic(tcx: TyCtxt<'_>, decl: &hir::FnDecl, abi: Abi, span: Span) {
if decl.c_variadic && !(abi == Abi::C || abi == Abi::Cdecl) {
let mut err = struct_span_err!(tcx.sess, span, E0045,
Expand Down
5 changes: 2 additions & 3 deletions src/libsyntax/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,6 @@ declare_features! (
// Allows using `reason` in lint attributes and the `#[expect(lint)]` lint check.
(active, lint_reasons, "1.31.0", Some(54503), None),

// Allows paths to enum variants on type aliases.
(active, type_alias_enum_variants, "1.31.0", Some(49683), None),

// Allows exhaustive integer pattern matching on `usize` and `isize`.
(active, precise_pointer_size_matching, "1.32.0", Some(56354), None),

Expand Down Expand Up @@ -853,6 +850,8 @@ declare_features! (
(accepted, extern_crate_self, "1.34.0", Some(56409), None),
// Allows arbitrary delimited token streams in non-macro attributes.
(accepted, unrestricted_attribute_tokens, "1.34.0", Some(55208), None),
// Allows paths to enum variants on type aliases including `Self`.
(accepted, type_alias_enum_variants, "1.37.0", Some(49683), None),
// Allows using `#[repr(align(X))]` on enums with equivalent semantics
// to wrapping an enum in a wrapper struct with `#[repr(align(X))]`.
(accepted, repr_align_enum, "1.37.0", Some(57996), None),
Expand Down
30 changes: 0 additions & 30 deletions src/test/run-pass/type-alias-enum-variants-2.rs

This file was deleted.

30 changes: 0 additions & 30 deletions src/test/run-pass/type-alias-enum-variants.rs

This file was deleted.

19 changes: 0 additions & 19 deletions src/test/ui/feature-gates/feature-gate-type_alias_enum_variants.rs

This file was deleted.

This file was deleted.

17 changes: 0 additions & 17 deletions src/test/ui/type-alias-enum-variants-panic.rs

This file was deleted.

21 changes: 0 additions & 21 deletions src/test/ui/type-alias-enum-variants-panic.stderr

This file was deleted.

13 changes: 0 additions & 13 deletions src/test/ui/type-alias-enum-variants-priority-2.rs

This file was deleted.

12 changes: 0 additions & 12 deletions src/test/ui/type-alias-enum-variants-priority-2.stderr

This file was deleted.

10 changes: 0 additions & 10 deletions src/test/ui/type-alias-enum-variants-priority-3.rs

This file was deleted.

19 changes: 0 additions & 19 deletions src/test/ui/type-alias-enum-variants-priority.rs

This file was deleted.

11 changes: 0 additions & 11 deletions src/test/ui/type-alias-enum-variants.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
// run-pass

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

#![allow(irrefutable_let_patterns)]

#[allow(dead_code)]
enum Enum<T> { TSVariant(T), SVariant { v: T } }
enum Enum<T> { TSVariant(T), SVariant { v: T }, UVariant }
type Alias<T> = Enum<T>;
type AliasFixed = Enum<()>;

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

is_variant!(SVariant, AliasFixed::SVariant { v: () });

// Unit variant

is_variant!(UVariant, Enum::UVariant);
is_variant!(UVariant, Enum::UVariant::<()>);
is_variant!(UVariant, Enum::<()>::UVariant);

is_variant!(UVariant, Alias::UVariant);
is_variant!(UVariant, Alias::<()>::UVariant);

is_variant!(UVariant, AliasFixed::UVariant);
}
Loading

0 comments on commit 5748825

Please sign in to comment.