Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split out a separate feature gate for impl trait in associated types #110237

Merged
merged 1 commit into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,34 @@ impl<'a> PostExpansionVisitor<'a> {
}

/// Feature gate `impl Trait` inside `type Alias = $type_expr;`.
fn check_impl_trait(&self, ty: &ast::Ty) {
fn check_impl_trait(&self, ty: &ast::Ty, in_associated_ty: bool) {
struct ImplTraitVisitor<'a> {
vis: &'a PostExpansionVisitor<'a>,
in_associated_ty: bool,
}
impl Visitor<'_> for ImplTraitVisitor<'_> {
fn visit_ty(&mut self, ty: &ast::Ty) {
if let ast::TyKind::ImplTrait(..) = ty.kind {
gate_feature_post!(
&self.vis,
type_alias_impl_trait,
ty.span,
"`impl Trait` in type aliases is unstable"
);
if self.in_associated_ty {
gate_feature_post!(
&self.vis,
impl_trait_in_assoc_type,
ty.span,
"`impl Trait` in associated types is unstable"
);
} else {
gate_feature_post!(
&self.vis,
type_alias_impl_trait,
ty.span,
"`impl Trait` in type aliases is unstable"
);
}
}
visit::walk_ty(self, ty);
}
}
ImplTraitVisitor { vis: self }.visit_ty(ty);
ImplTraitVisitor { vis: self, in_associated_ty }.visit_ty(ty);
}

fn check_late_bound_lifetime_defs(&self, params: &[ast::GenericParam]) {
Expand Down Expand Up @@ -294,7 +304,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
}

ast::ItemKind::TyAlias(box ast::TyAlias { ty: Some(ty), .. }) => {
self.check_impl_trait(&ty)
self.check_impl_trait(&ty, false)
}

_ => {}
Expand Down Expand Up @@ -517,7 +527,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
);
}
if let Some(ty) = ty {
self.check_impl_trait(ty);
self.check_impl_trait(ty, true);
}
false
}
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,8 @@ declare_features! (
(active, half_open_range_patterns_in_slices, "1.66.0", Some(67264), None),
/// Allows `if let` guard in match arms.
(active, if_let_guard, "1.47.0", Some(51114), None),
/// Allows `impl Trait` to be used inside associated types (RFC 2515).
(active, impl_trait_in_assoc_type, "CURRENT_RUSTC_VERSION", Some(63063), None),
/// Allows `impl Trait` as output type in `Fn` traits in return position of functions.
(active, impl_trait_in_fn_trait_return, "1.64.0", Some(99697), None),
/// Allows referencing `Self` and projections in impl-trait.
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,7 @@ symbols! {
ignore,
impl_header_lifetime_elision,
impl_lint_pass,
impl_trait_in_assoc_type,
impl_trait_in_bindings,
impl_trait_in_fn_trait_return,
impl_trait_projections,
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#![feature(type_ascription)]
#![feature(iter_intersperse)]
#![feature(type_alias_impl_trait)]
#![cfg_attr(not(bootstrap), feature(impl_trait_in_assoc_type))]
#![recursion_limit = "256"]
#![warn(rustc::internal)]
#![allow(clippy::collapsible_if, clippy::collapsible_else_if)]
Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc/auxiliary/issue-73061.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//edition:2018

#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)]

pub trait Foo {
type X: std::future::Future<Output = ()>;
Expand Down
10 changes: 7 additions & 3 deletions tests/ui/associated-types/issue-63591.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
// check-pass

#![feature(associated_type_bounds)]
#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)]

fn main() {}

trait Bar { type Assoc; }
trait Bar {
type Assoc;
}

trait Thing {
type Out;
fn func() -> Self::Out;
}

struct AssocIsCopy;
impl Bar for AssocIsCopy { type Assoc = u8; }
impl Bar for AssocIsCopy {
type Assoc = u8;
}

impl Thing for AssocIsCopy {
type Out = impl Bar<Assoc: Copy>;
Expand Down
6 changes: 2 additions & 4 deletions tests/ui/async-await/in-trait/async-associated-types2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// revisions: current next

#![feature(async_fn_in_trait)]
#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)]
#![allow(incomplete_features)]

use std::future::Future;
Expand All @@ -23,9 +23,7 @@ impl MyTrait for i32 {
Self: 'a;

fn foo<'a>(&'a self) -> Self::Fut<'a> {
async {
*self
}
async { *self }
}
}

Expand Down
18 changes: 18 additions & 0 deletions tests/ui/feature-gates/feature-gate-impl_trait_in_assoc_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
trait Foo {
type Bar;
}

impl Foo for () {
type Bar = impl std::fmt::Debug;
//~^ ERROR: `impl Trait` in associated types is unstable
}

struct Mop;

impl Mop {
type Bop = impl std::fmt::Debug;
//~^ ERROR: `impl Trait` in associated types is unstable
//~| ERROR: inherent associated types are unstable
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
error[E0658]: `impl Trait` in associated types is unstable
--> $DIR/feature-gate-impl_trait_in_assoc_type.rs:6:16
|
LL | type Bar = impl std::fmt::Debug;
| ^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #63063 <https://github.com/rust-lang/rust/issues/63063> for more information
= help: add `#![feature(impl_trait_in_assoc_type)]` to the crate attributes to enable

error[E0658]: `impl Trait` in associated types is unstable
--> $DIR/feature-gate-impl_trait_in_assoc_type.rs:13:16
|
LL | type Bop = impl std::fmt::Debug;
| ^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #63063 <https://github.com/rust-lang/rust/issues/63063> for more information
= help: add `#![feature(impl_trait_in_assoc_type)]` to the crate attributes to enable

error[E0658]: inherent associated types are unstable
--> $DIR/feature-gate-impl_trait_in_assoc_type.rs:13:5
|
LL | type Bop = impl std::fmt::Debug;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #8995 <https://github.com/rust-lang/rust/issues/8995> for more information
= help: add `#![feature(inherent_associated_types)]` to the crate attributes to enable

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0658`.
2 changes: 1 addition & 1 deletion tests/ui/generator/issue-87142.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Regression test for #87142
// This test needs the above flags and the "lib" crate type.

#![feature(type_alias_impl_trait, generator_trait, generators)]
#![feature(impl_trait_in_assoc_type, generator_trait, generators)]
#![crate_type = "lib"]

use std::ops::Generator;
Expand Down
6 changes: 4 additions & 2 deletions tests/ui/generic-associated-types/issue-86218-2.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// check-pass

#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)]

pub trait Stream {
type Item;
Expand All @@ -17,7 +17,9 @@ trait Yay<AdditionalValue> {

impl<T> Yay<T> for () {
type InnerStream<'s> = impl Stream<Item = i32> + 's;
fn foo<'s>() -> Self::InnerStream<'s> { () }
fn foo<'s>() -> Self::InnerStream<'s> {
()
}
}

fn main() {}
6 changes: 4 additions & 2 deletions tests/ui/generic-associated-types/issue-86218.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// check-pass

#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)]

pub trait Stream {
type Item;
Expand All @@ -18,7 +18,9 @@ trait Yay<AdditionalValue> {
impl<'a> Yay<&'a ()> for () {
type InnerStream<'s> = impl Stream<Item = i32> + 's;
//^ ERROR does not fulfill the required lifetime
fn foo<'s>() -> Self::InnerStream<'s> { () }
fn foo<'s>() -> Self::InnerStream<'s> {
()
}
}

fn main() {}
2 changes: 1 addition & 1 deletion tests/ui/generic-associated-types/issue-87258_a.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)]

// See https://github.com/rust-lang/rust/issues/87258#issuecomment-883293367

Expand Down
3 changes: 2 additions & 1 deletion tests/ui/generic-associated-types/issue-88595.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)]

fn main() {}

#[rustfmt::skip]
trait A<'a> {
type B<'b>: Clone
// FIXME(generic_associated_types): Remove one of the below bounds
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/generic-associated-types/issue-88595.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
error: non-defining opaque type use in defining scope
--> $DIR/issue-88595.rs:20:35
--> $DIR/issue-88595.rs:21:35
|
LL | fn a(&'a self) -> Self::B<'a> {}
| ^^
|
note: lifetime used multiple times
--> $DIR/issue-88595.rs:17:6
--> $DIR/issue-88595.rs:18:6
|
LL | impl<'a> A<'a> for C {
| ^^
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/generic-associated-types/issue-89008.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// check-pass
// edition:2021

#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)]

use std::future::Future;
use std::marker::PhantomData;
Expand Down
6 changes: 4 additions & 2 deletions tests/ui/generic-associated-types/issue-90014.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// edition:2018

#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)]

use std::future::Future;

trait MakeFut {
type Fut<'a> where Self: 'a;
type Fut<'a>
where
Self: 'a;
fn make_fut<'a>(&'a self) -> Self::Fut<'a>;
}

Expand Down
6 changes: 3 additions & 3 deletions tests/ui/generic-associated-types/issue-90014.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
error[E0477]: the type `&mut ()` does not fulfill the required lifetime
--> $DIR/issue-90014.rs:13:20
--> $DIR/issue-90014.rs:15:20
|
LL | type Fut<'a> where Self: 'a;
LL | type Fut<'a>
| ------------ definition of `Fut` from trait
...
LL | type Fut<'a> = impl Future<Output = ()>;
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
note: type must outlive the lifetime `'a` as defined here
--> $DIR/issue-90014.rs:13:14
--> $DIR/issue-90014.rs:15:14
|
LL | type Fut<'a> = impl Future<Output = ()>;
| ^^
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)]
// build-pass (FIXME(62277): could be check-pass?)

trait Bar {}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/impl-trait/associated-impl-trait-type-trivial.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)]
// build-pass (FIXME(62277): could be check-pass?)

trait Bar {}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/impl-trait/associated-impl-trait-type.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)]
// build-pass (FIXME(62277): could be check-pass?)

trait Bar {}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/impl-trait/issue-55872-1.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)]

pub trait Bar {
type E: Copy;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/impl-trait/issue-55872-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// [drop_tracking_mir] compile-flags: -Zdrop-tracking-mir
// edition:2018

#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)]

pub trait Bar {
type E: Send;
Expand Down
8 changes: 0 additions & 8 deletions tests/ui/impl-trait/issue-55872-2.stderr

This file was deleted.

2 changes: 1 addition & 1 deletion tests/ui/impl-trait/issue-55872-3.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// edition:2018
// ignore-compare-mode-chalk

#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)]

pub trait Bar {
type E: Copy;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/impl-trait/issue-55872.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)]

pub trait Bar {
type E: Copy;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/impl-trait/issues/issue-82139.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)]

trait Trait {
type Associated;
Expand Down
Loading