From 4f4e15d5eb9750c4ac8c140207612122562d6c51 Mon Sep 17 00:00:00 2001 From: Olivier FAURE Date: Thu, 25 Feb 2021 14:09:52 +0100 Subject: [PATCH] Add feature gate for inherent associate types. --- compiler/rustc_error_codes/src/error_codes.rs | 1 - .../src/error_codes/E0202.md | 15 ------------- compiler/rustc_feature/src/active.rs | 4 ++++ compiler/rustc_span/src/symbol.rs | 1 + compiler/rustc_typeck/src/collect/type_of.rs | 17 ++++++++++---- compiler/rustc_typeck/src/errors.rs | 7 ------ src/test/ui/assoc-inherent.rs | 17 +++++++++++--- src/test/ui/assoc-inherent.stderr | 20 ++++++++++++----- .../feature-gate-inherent_associated_types.rs | 10 +++++++++ ...ture-gate-inherent_associated_types.stderr | 12 ++++++++++ .../impl-item-type-no-body-semantic-fail.rs | 8 +++---- ...mpl-item-type-no-body-semantic-fail.stderr | 22 ++++++++++++++----- 12 files changed, 89 insertions(+), 45 deletions(-) delete mode 100644 compiler/rustc_error_codes/src/error_codes/E0202.md create mode 100644 src/test/ui/feature-gates/feature-gate-inherent_associated_types.rs create mode 100644 src/test/ui/feature-gates/feature-gate-inherent_associated_types.stderr diff --git a/compiler/rustc_error_codes/src/error_codes.rs b/compiler/rustc_error_codes/src/error_codes.rs index c4330694504..4b529734328 100644 --- a/compiler/rustc_error_codes/src/error_codes.rs +++ b/compiler/rustc_error_codes/src/error_codes.rs @@ -103,7 +103,6 @@ E0199: include_str!("./error_codes/E0199.md"), E0200: include_str!("./error_codes/E0200.md"), E0201: include_str!("./error_codes/E0201.md"), -E0202: include_str!("./error_codes/E0202.md"), E0203: include_str!("./error_codes/E0203.md"), E0204: include_str!("./error_codes/E0204.md"), E0205: include_str!("./error_codes/E0205.md"), diff --git a/compiler/rustc_error_codes/src/error_codes/E0202.md b/compiler/rustc_error_codes/src/error_codes/E0202.md deleted file mode 100644 index afc61ec2e48..00000000000 --- a/compiler/rustc_error_codes/src/error_codes/E0202.md +++ /dev/null @@ -1,15 +0,0 @@ -Inherent associated types were part of [RFC 195] but are not yet implemented. -See [the tracking issue][iss8995] for the status of this implementation. - -Erroneous code example: - -```compile_fail,E0202 -struct Foo; - -impl Foo { - type Bar = isize; // error! -} -``` - -[RFC 195]: https://github.com/rust-lang/rfcs/blob/master/text/0195-associated-items.md -[iss8995]: https://github.com/rust-lang/rust/issues/8995 diff --git a/compiler/rustc_feature/src/active.rs b/compiler/rustc_feature/src/active.rs index 3f484ab5686..23c53ffb49d 100644 --- a/compiler/rustc_feature/src/active.rs +++ b/compiler/rustc_feature/src/active.rs @@ -641,6 +641,9 @@ pub fn set(&self, features: &mut Features, span: Span) { /// Allows `pub` on `macro_rules` items. (active, pub_macro_rules, "1.52.0", Some(78855), None), + /// Allows associated types in inherent impls. + (active, inherent_associated_types, "1.52.0", Some(8995), None), + // ------------------------------------------------------------------------- // feature-group-end: actual feature gates // ------------------------------------------------------------------------- @@ -666,6 +669,7 @@ pub fn set(&self, features: &mut Features, span: Span) { sym::unsized_locals, sym::capture_disjoint_fields, sym::const_generics_defaults, + sym::inherent_associated_types, ]; /// Some features are not allowed to be used together at the same time, if diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index b112402ffe3..db0913cff5b 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -627,6 +627,7 @@ index_mut, infer_outlives_requirements, infer_static_outlives_requirements, + inherent_associated_types, inlateout, inline, inline_const, diff --git a/compiler/rustc_typeck/src/collect/type_of.rs b/compiler/rustc_typeck/src/collect/type_of.rs index a2aa3b308ec..e6f771cfd53 100644 --- a/compiler/rustc_typeck/src/collect/type_of.rs +++ b/compiler/rustc_typeck/src/collect/type_of.rs @@ -1,4 +1,3 @@ -use crate::errors::AssocTypeOnInherentImpl; use rustc_data_structures::fx::FxHashSet; use rustc_errors::{Applicability, ErrorReported, StashKey}; use rustc_hir as hir; @@ -294,7 +293,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { } ImplItemKind::TyAlias(ref ty) => { if tcx.impl_trait_ref(tcx.hir().get_parent_did(hir_id).to_def_id()).is_none() { - report_assoc_ty_on_inherent_impl(tcx, item.span); + check_feature_inherent_assoc_ty(tcx, item.span); } icx.to_ty(ty) @@ -746,6 +745,16 @@ fn infer_placeholder_type( }) } -fn report_assoc_ty_on_inherent_impl(tcx: TyCtxt<'_>, span: Span) { - tcx.sess.emit_err(AssocTypeOnInherentImpl { span }); +fn check_feature_inherent_assoc_ty(tcx: TyCtxt<'_>, span: Span) { + if !tcx.features().inherent_associated_types { + use rustc_session::parse::feature_err; + use rustc_span::symbol::sym; + feature_err( + &tcx.sess.parse_sess, + sym::inherent_associated_types, + span, + "inherent associated types are unstable", + ) + .emit(); + } } diff --git a/compiler/rustc_typeck/src/errors.rs b/compiler/rustc_typeck/src/errors.rs index a769e48d2ca..5068242692a 100644 --- a/compiler/rustc_typeck/src/errors.rs +++ b/compiler/rustc_typeck/src/errors.rs @@ -82,13 +82,6 @@ pub struct CopyImplOnTypeWithDtor { pub span: Span, } -#[derive(SessionDiagnostic)] -#[error = "E0202"] -pub struct AssocTypeOnInherentImpl { - #[message = "associated types are not yet supported in inherent impls (see #8995)"] - pub span: Span, -} - #[derive(SessionDiagnostic)] #[error = "E0203"] pub struct MultipleRelaxedDefaultBounds { diff --git a/src/test/ui/assoc-inherent.rs b/src/test/ui/assoc-inherent.rs index 05329a27142..c579c962ffc 100644 --- a/src/test/ui/assoc-inherent.rs +++ b/src/test/ui/assoc-inherent.rs @@ -1,9 +1,20 @@ -// Test associated types are, until #8995 is implemented, forbidden in inherent impls. +// Test that inherent associated types work with +// inherent_associated_types feature gate. + +#![feature(inherent_associated_types)] +#![allow(incomplete_features)] struct Foo; impl Foo { - type Bar = isize; //~ERROR associated types are not yet supported in inherent impls (see #8995) + type Bar = isize; } -fn main() {} +impl Foo { + type Baz; //~ ERROR associated type in `impl` without body +} + +fn main() { + let x : Foo::Bar; //~ERROR ambiguous associated type + x = 0isize; +} diff --git a/src/test/ui/assoc-inherent.stderr b/src/test/ui/assoc-inherent.stderr index f9ea3365cb8..b703453fa03 100644 --- a/src/test/ui/assoc-inherent.stderr +++ b/src/test/ui/assoc-inherent.stderr @@ -1,9 +1,17 @@ -error[E0202]: associated types are not yet supported in inherent impls (see #8995) - --> $DIR/assoc-inherent.rs:6:5 +error: associated type in `impl` without body + --> $DIR/assoc-inherent.rs:14:5 | -LL | type Bar = isize; - | ^^^^^^^^^^^^^^^^^ +LL | type Baz; + | ^^^^^^^^- + | | + | help: provide a definition for the type: `= ;` -error: aborting due to previous error +error[E0223]: ambiguous associated type + --> $DIR/assoc-inherent.rs:18:13 + | +LL | let x : Foo::Bar; + | ^^^^^^^^ help: use fully-qualified syntax: `::Bar` -For more information about this error, try `rustc --explain E0202`. +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0223`. diff --git a/src/test/ui/feature-gates/feature-gate-inherent_associated_types.rs b/src/test/ui/feature-gates/feature-gate-inherent_associated_types.rs new file mode 100644 index 00000000000..fc7c6dbc02e --- /dev/null +++ b/src/test/ui/feature-gates/feature-gate-inherent_associated_types.rs @@ -0,0 +1,10 @@ +// Test that inherent associated types cannot be used when inherent_associated_types +// feature gate is not used. + +struct Foo; + +impl Foo { + type Bar = isize; //~ERROR inherent associated types are unstable +} + +fn main() {} diff --git a/src/test/ui/feature-gates/feature-gate-inherent_associated_types.stderr b/src/test/ui/feature-gates/feature-gate-inherent_associated_types.stderr new file mode 100644 index 00000000000..76e65d239f8 --- /dev/null +++ b/src/test/ui/feature-gates/feature-gate-inherent_associated_types.stderr @@ -0,0 +1,12 @@ +error[E0658]: inherent associated types are unstable + --> $DIR/feature-gate-inherent_associated_types.rs:7:5 + | +LL | type Bar = isize; + | ^^^^^^^^^^^^^^^^^ + | + = note: see issue #8995 for more information + = help: add `#![feature(inherent_associated_types)]` to the crate attributes to enable + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0658`. diff --git a/src/test/ui/parser/impl-item-type-no-body-semantic-fail.rs b/src/test/ui/parser/impl-item-type-no-body-semantic-fail.rs index fa9c7ababcf..1ccc9497d9d 100644 --- a/src/test/ui/parser/impl-item-type-no-body-semantic-fail.rs +++ b/src/test/ui/parser/impl-item-type-no-body-semantic-fail.rs @@ -8,16 +8,16 @@ fn main() {} impl X { type Y; //~^ ERROR associated type in `impl` without body - //~| ERROR associated types are not yet supported in inherent impls + //~| ERROR inherent associated types are unstable type Z: Ord; //~^ ERROR associated type in `impl` without body //~| ERROR bounds on `type`s in `impl`s have no effect - //~| ERROR associated types are not yet supported in inherent impls + //~| ERROR inherent associated types are unstable type W: Ord where Self: Eq; //~^ ERROR associated type in `impl` without body //~| ERROR bounds on `type`s in `impl`s have no effect - //~| ERROR associated types are not yet supported in inherent impls + //~| ERROR inherent associated types are unstable type W where Self: Eq; //~^ ERROR associated type in `impl` without body - //~| ERROR associated types are not yet supported in inherent impls + //~| ERROR inherent associated types are unstable } diff --git a/src/test/ui/parser/impl-item-type-no-body-semantic-fail.stderr b/src/test/ui/parser/impl-item-type-no-body-semantic-fail.stderr index 214467793bc..818d73c898d 100644 --- a/src/test/ui/parser/impl-item-type-no-body-semantic-fail.stderr +++ b/src/test/ui/parser/impl-item-type-no-body-semantic-fail.stderr @@ -51,30 +51,42 @@ LL | #![feature(generic_associated_types)] = note: `#[warn(incomplete_features)]` on by default = note: see issue #44265 for more information -error[E0202]: associated types are not yet supported in inherent impls (see #8995) +error[E0658]: inherent associated types are unstable --> $DIR/impl-item-type-no-body-semantic-fail.rs:9:5 | LL | type Y; | ^^^^^^^ + | + = note: see issue #8995 for more information + = help: add `#![feature(inherent_associated_types)]` to the crate attributes to enable -error[E0202]: associated types are not yet supported in inherent impls (see #8995) +error[E0658]: inherent associated types are unstable --> $DIR/impl-item-type-no-body-semantic-fail.rs:12:5 | LL | type Z: Ord; | ^^^^^^^^^^^^ + | + = note: see issue #8995 for more information + = help: add `#![feature(inherent_associated_types)]` to the crate attributes to enable -error[E0202]: associated types are not yet supported in inherent impls (see #8995) +error[E0658]: inherent associated types are unstable --> $DIR/impl-item-type-no-body-semantic-fail.rs:16:5 | LL | type W: Ord where Self: Eq; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: see issue #8995 for more information + = help: add `#![feature(inherent_associated_types)]` to the crate attributes to enable -error[E0202]: associated types are not yet supported in inherent impls (see #8995) +error[E0658]: inherent associated types are unstable --> $DIR/impl-item-type-no-body-semantic-fail.rs:20:5 | LL | type W where Self: Eq; | ^^^^^^^^^^^^^^^^^^^^^^ + | + = note: see issue #8995 for more information + = help: add `#![feature(inherent_associated_types)]` to the crate attributes to enable error: aborting due to 10 previous errors; 1 warning emitted -For more information about this error, try `rustc --explain E0202`. +For more information about this error, try `rustc --explain E0658`.