Skip to content

Commit f263f88

Browse files
committed
Split out a separate feature gate for impl trait in associated types
1 parent 9be9b5e commit f263f88

File tree

68 files changed

+217
-148
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+217
-148
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

+20-10
Original file line numberDiff line numberDiff line change
@@ -121,24 +121,34 @@ impl<'a> PostExpansionVisitor<'a> {
121121
}
122122

123123
/// Feature gate `impl Trait` inside `type Alias = $type_expr;`.
124-
fn check_impl_trait(&self, ty: &ast::Ty) {
124+
fn check_impl_trait(&self, ty: &ast::Ty, in_associated_ty: bool) {
125125
struct ImplTraitVisitor<'a> {
126126
vis: &'a PostExpansionVisitor<'a>,
127+
in_associated_ty: bool,
127128
}
128129
impl Visitor<'_> for ImplTraitVisitor<'_> {
129130
fn visit_ty(&mut self, ty: &ast::Ty) {
130131
if let ast::TyKind::ImplTrait(..) = ty.kind {
131-
gate_feature_post!(
132-
&self.vis,
133-
type_alias_impl_trait,
134-
ty.span,
135-
"`impl Trait` in type aliases is unstable"
136-
);
132+
if self.in_associated_ty {
133+
gate_feature_post!(
134+
&self.vis,
135+
impl_trait_in_assoc_type,
136+
ty.span,
137+
"`impl Trait` in associated types is unstable"
138+
);
139+
} else {
140+
gate_feature_post!(
141+
&self.vis,
142+
type_alias_impl_trait,
143+
ty.span,
144+
"`impl Trait` in type aliases is unstable"
145+
);
146+
}
137147
}
138148
visit::walk_ty(self, ty);
139149
}
140150
}
141-
ImplTraitVisitor { vis: self }.visit_ty(ty);
151+
ImplTraitVisitor { vis: self, in_associated_ty }.visit_ty(ty);
142152
}
143153

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

296306
ast::ItemKind::TyAlias(box ast::TyAlias { ty: Some(ty), .. }) => {
297-
self.check_impl_trait(&ty)
307+
self.check_impl_trait(&ty, false)
298308
}
299309

300310
_ => {}
@@ -517,7 +527,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
517527
);
518528
}
519529
if let Some(ty) = ty {
520-
self.check_impl_trait(ty);
530+
self.check_impl_trait(ty, true);
521531
}
522532
false
523533
}

compiler/rustc_feature/src/active.rs

+2
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,8 @@ declare_features! (
416416
(active, half_open_range_patterns_in_slices, "1.66.0", Some(67264), None),
417417
/// Allows `if let` guard in match arms.
418418
(active, if_let_guard, "1.47.0", Some(51114), None),
419+
/// Allows `impl Trait` to be used inside associated types (RFC 2515).
420+
(active, impl_trait_in_assoc_type, "CURRENT_RUSTC_VERSION", Some(63063), None),
419421
/// Allows `impl Trait` as output type in `Fn` traits in return position of functions.
420422
(active, impl_trait_in_fn_trait_return, "1.64.0", Some(99697), None),
421423
/// Allows referencing `Self` and projections in impl-trait.

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,7 @@ symbols! {
800800
ignore,
801801
impl_header_lifetime_elision,
802802
impl_lint_pass,
803+
impl_trait_in_assoc_type,
803804
impl_trait_in_bindings,
804805
impl_trait_in_fn_trait_return,
805806
impl_trait_projections,

src/librustdoc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#![feature(type_ascription)]
1616
#![feature(iter_intersperse)]
1717
#![feature(type_alias_impl_trait)]
18+
#![cfg_attr(not(bootstrap), feature(impl_trait_in_assoc_type))]
1819
#![recursion_limit = "256"]
1920
#![warn(rustc::internal)]
2021
#![allow(clippy::collapsible_if, clippy::collapsible_else_if)]

tests/rustdoc/auxiliary/issue-73061.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//edition:2018
22

3-
#![feature(type_alias_impl_trait)]
3+
#![feature(impl_trait_in_assoc_type)]
44

55
pub trait Foo {
66
type X: std::future::Future<Output = ()>;

tests/ui/associated-types/issue-63591.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
// check-pass
22

33
#![feature(associated_type_bounds)]
4-
#![feature(type_alias_impl_trait)]
4+
#![feature(impl_trait_in_assoc_type)]
55

66
fn main() {}
77

8-
trait Bar { type Assoc; }
8+
trait Bar {
9+
type Assoc;
10+
}
911

1012
trait Thing {
1113
type Out;
1214
fn func() -> Self::Out;
1315
}
1416

1517
struct AssocIsCopy;
16-
impl Bar for AssocIsCopy { type Assoc = u8; }
18+
impl Bar for AssocIsCopy {
19+
type Assoc = u8;
20+
}
1721

1822
impl Thing for AssocIsCopy {
1923
type Out = impl Bar<Assoc: Copy>;

tests/ui/async-await/in-trait/async-associated-types2.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// revisions: current next
55

66
#![feature(async_fn_in_trait)]
7-
#![feature(type_alias_impl_trait)]
7+
#![feature(impl_trait_in_assoc_type)]
88
#![allow(incomplete_features)]
99

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

2525
fn foo<'a>(&'a self) -> Self::Fut<'a> {
26-
async {
27-
*self
28-
}
26+
async { *self }
2927
}
3028
}
3129

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
trait Foo {
2+
type Bar;
3+
}
4+
5+
impl Foo for () {
6+
type Bar = impl std::fmt::Debug;
7+
//~^ ERROR: `impl Trait` in associated types is unstable
8+
}
9+
10+
struct Mop;
11+
12+
impl Mop {
13+
type Bop = impl std::fmt::Debug;
14+
//~^ ERROR: `impl Trait` in associated types is unstable
15+
//~| ERROR: inherent associated types are unstable
16+
}
17+
18+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
error[E0658]: `impl Trait` in associated types is unstable
2+
--> $DIR/feature-gate-impl_trait_in_assoc_type.rs:6:16
3+
|
4+
LL | type Bar = impl std::fmt::Debug;
5+
| ^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #63063 <https://github.com/rust-lang/rust/issues/63063> for more information
8+
= help: add `#![feature(impl_trait_in_assoc_type)]` to the crate attributes to enable
9+
10+
error[E0658]: `impl Trait` in associated types is unstable
11+
--> $DIR/feature-gate-impl_trait_in_assoc_type.rs:13:16
12+
|
13+
LL | type Bop = impl std::fmt::Debug;
14+
| ^^^^^^^^^^^^^^^^^^^^
15+
|
16+
= note: see issue #63063 <https://github.com/rust-lang/rust/issues/63063> for more information
17+
= help: add `#![feature(impl_trait_in_assoc_type)]` to the crate attributes to enable
18+
19+
error[E0658]: inherent associated types are unstable
20+
--> $DIR/feature-gate-impl_trait_in_assoc_type.rs:13:5
21+
|
22+
LL | type Bop = impl std::fmt::Debug;
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
|
25+
= note: see issue #8995 <https://github.com/rust-lang/rust/issues/8995> for more information
26+
= help: add `#![feature(inherent_associated_types)]` to the crate attributes to enable
27+
28+
error: aborting due to 3 previous errors
29+
30+
For more information about this error, try `rustc --explain E0658`.

tests/ui/generator/issue-87142.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// Regression test for #87142
55
// This test needs the above flags and the "lib" crate type.
66

7-
#![feature(type_alias_impl_trait, generator_trait, generators)]
7+
#![feature(impl_trait_in_assoc_type, generator_trait, generators)]
88
#![crate_type = "lib"]
99

1010
use std::ops::Generator;

tests/ui/generic-associated-types/issue-86218-2.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// check-pass
22

3-
#![feature(type_alias_impl_trait)]
3+
#![feature(impl_trait_in_assoc_type)]
44

55
pub trait Stream {
66
type Item;
@@ -17,7 +17,9 @@ trait Yay<AdditionalValue> {
1717

1818
impl<T> Yay<T> for () {
1919
type InnerStream<'s> = impl Stream<Item = i32> + 's;
20-
fn foo<'s>() -> Self::InnerStream<'s> { () }
20+
fn foo<'s>() -> Self::InnerStream<'s> {
21+
()
22+
}
2123
}
2224

2325
fn main() {}

tests/ui/generic-associated-types/issue-86218.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// check-pass
22

3-
#![feature(type_alias_impl_trait)]
3+
#![feature(impl_trait_in_assoc_type)]
44

55
pub trait Stream {
66
type Item;
@@ -18,7 +18,9 @@ trait Yay<AdditionalValue> {
1818
impl<'a> Yay<&'a ()> for () {
1919
type InnerStream<'s> = impl Stream<Item = i32> + 's;
2020
//^ ERROR does not fulfill the required lifetime
21-
fn foo<'s>() -> Self::InnerStream<'s> { () }
21+
fn foo<'s>() -> Self::InnerStream<'s> {
22+
()
23+
}
2224
}
2325

2426
fn main() {}

tests/ui/generic-associated-types/issue-87258_a.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(type_alias_impl_trait)]
1+
#![feature(impl_trait_in_assoc_type)]
22

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

tests/ui/generic-associated-types/issue-88595.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
#![feature(type_alias_impl_trait)]
1+
#![feature(impl_trait_in_assoc_type)]
22

33
fn main() {}
44

5+
#[rustfmt::skip]
56
trait A<'a> {
67
type B<'b>: Clone
78
// FIXME(generic_associated_types): Remove one of the below bounds

tests/ui/generic-associated-types/issue-88595.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error: non-defining opaque type use in defining scope
2-
--> $DIR/issue-88595.rs:20:35
2+
--> $DIR/issue-88595.rs:21:35
33
|
44
LL | fn a(&'a self) -> Self::B<'a> {}
55
| ^^
66
|
77
note: lifetime used multiple times
8-
--> $DIR/issue-88595.rs:17:6
8+
--> $DIR/issue-88595.rs:18:6
99
|
1010
LL | impl<'a> A<'a> for C {
1111
| ^^

tests/ui/generic-associated-types/issue-89008.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// check-pass
22
// edition:2021
33

4-
#![feature(type_alias_impl_trait)]
4+
#![feature(impl_trait_in_assoc_type)]
55

66
use std::future::Future;
77
use std::marker::PhantomData;

tests/ui/generic-associated-types/issue-90014.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// edition:2018
22

3-
#![feature(type_alias_impl_trait)]
3+
#![feature(impl_trait_in_assoc_type)]
44

55
use std::future::Future;
66

77
trait MakeFut {
8-
type Fut<'a> where Self: 'a;
8+
type Fut<'a>
9+
where
10+
Self: 'a;
911
fn make_fut<'a>(&'a self) -> Self::Fut<'a>;
1012
}
1113

tests/ui/generic-associated-types/issue-90014.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
error[E0477]: the type `&mut ()` does not fulfill the required lifetime
2-
--> $DIR/issue-90014.rs:13:20
2+
--> $DIR/issue-90014.rs:15:20
33
|
4-
LL | type Fut<'a> where Self: 'a;
4+
LL | type Fut<'a>
55
| ------------ definition of `Fut` from trait
66
...
77
LL | type Fut<'a> = impl Future<Output = ()>;
88
| ^^^^^^^^^^^^^^^^^^^^^^^^
99
|
1010
note: type must outlive the lifetime `'a` as defined here
11-
--> $DIR/issue-90014.rs:13:14
11+
--> $DIR/issue-90014.rs:15:14
1212
|
1313
LL | type Fut<'a> = impl Future<Output = ()>;
1414
| ^^

tests/ui/impl-trait/associated-impl-trait-type-generic-trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(type_alias_impl_trait)]
1+
#![feature(impl_trait_in_assoc_type)]
22
// build-pass (FIXME(62277): could be check-pass?)
33

44
trait Bar {}

tests/ui/impl-trait/associated-impl-trait-type-trivial.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(type_alias_impl_trait)]
1+
#![feature(impl_trait_in_assoc_type)]
22
// build-pass (FIXME(62277): could be check-pass?)
33

44
trait Bar {}

tests/ui/impl-trait/associated-impl-trait-type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(type_alias_impl_trait)]
1+
#![feature(impl_trait_in_assoc_type)]
22
// build-pass (FIXME(62277): could be check-pass?)
33

44
trait Bar {}

tests/ui/impl-trait/issue-55872-1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(type_alias_impl_trait)]
1+
#![feature(impl_trait_in_assoc_type)]
22

33
pub trait Bar {
44
type E: Copy;

tests/ui/impl-trait/issue-55872-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// [drop_tracking_mir] compile-flags: -Zdrop-tracking-mir
44
// edition:2018
55

6-
#![feature(type_alias_impl_trait)]
6+
#![feature(impl_trait_in_assoc_type)]
77

88
pub trait Bar {
99
type E: Send;

tests/ui/impl-trait/issue-55872-2.stderr

-8
This file was deleted.

tests/ui/impl-trait/issue-55872-3.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// edition:2018
22
// ignore-compare-mode-chalk
33

4-
#![feature(type_alias_impl_trait)]
4+
#![feature(impl_trait_in_assoc_type)]
55

66
pub trait Bar {
77
type E: Copy;

tests/ui/impl-trait/issue-55872.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(type_alias_impl_trait)]
1+
#![feature(impl_trait_in_assoc_type)]
22

33
pub trait Bar {
44
type E: Copy;

tests/ui/impl-trait/issues/issue-82139.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(type_alias_impl_trait)]
1+
#![feature(impl_trait_in_assoc_type)]
22

33
trait Trait {
44
type Associated;

0 commit comments

Comments
 (0)