Skip to content

Commit 5e68c60

Browse files
authored
Rollup merge of #82516 - PoignardAzur:inherent-impl-ty, r=oli-obk
Add incomplete feature gate for inherent associate types. Mentored by ``````@oli-obk`````` So far the only change is that instead of giving an automatic error, the following code compiles: ```rust struct Foo; impl Foo { type Bar = isize; } ``` The backend work to make it actually usable isn't there yet. In particular, this: ```rust let x : Foo::Bar; ``` will give you: ```sh error[E0223]: ambiguous associated type --> /$RUSTC_DIR/src/test/ui/assoc-inherent.rs:15:13 | LL | let x : Foo::Bar; | ^^^^^^^^ help: use fully-qualified syntax: `<Foo as Trait>::Bar` ```
2 parents ae5e024 + 4f4e15d commit 5e68c60

File tree

12 files changed

+89
-45
lines changed

12 files changed

+89
-45
lines changed

Diff for: compiler/rustc_error_codes/src/error_codes.rs

-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ E0198: include_str!("./error_codes/E0198.md"),
103103
E0199: include_str!("./error_codes/E0199.md"),
104104
E0200: include_str!("./error_codes/E0200.md"),
105105
E0201: include_str!("./error_codes/E0201.md"),
106-
E0202: include_str!("./error_codes/E0202.md"),
107106
E0203: include_str!("./error_codes/E0203.md"),
108107
E0204: include_str!("./error_codes/E0204.md"),
109108
E0205: include_str!("./error_codes/E0205.md"),

Diff for: compiler/rustc_error_codes/src/error_codes/E0202.md

-15
This file was deleted.

Diff for: compiler/rustc_feature/src/active.rs

+4
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,9 @@ declare_features! (
641641
/// Allows `pub` on `macro_rules` items.
642642
(active, pub_macro_rules, "1.52.0", Some(78855), None),
643643

644+
/// Allows associated types in inherent impls.
645+
(active, inherent_associated_types, "1.52.0", Some(8995), None),
646+
644647
// -------------------------------------------------------------------------
645648
// feature-group-end: actual feature gates
646649
// -------------------------------------------------------------------------
@@ -666,6 +669,7 @@ pub const INCOMPLETE_FEATURES: &[Symbol] = &[
666669
sym::unsized_locals,
667670
sym::capture_disjoint_fields,
668671
sym::const_generics_defaults,
672+
sym::inherent_associated_types,
669673
];
670674

671675
/// Some features are not allowed to be used together at the same time, if

Diff for: compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,7 @@ symbols! {
634634
index_mut,
635635
infer_outlives_requirements,
636636
infer_static_outlives_requirements,
637+
inherent_associated_types,
637638
inlateout,
638639
inline,
639640
inline_const,

Diff for: compiler/rustc_typeck/src/collect/type_of.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::errors::AssocTypeOnInherentImpl;
21
use rustc_data_structures::fx::FxHashSet;
32
use rustc_errors::{Applicability, ErrorReported, StashKey};
43
use rustc_hir as hir;
@@ -294,7 +293,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
294293
}
295294
ImplItemKind::TyAlias(ref ty) => {
296295
if tcx.impl_trait_ref(tcx.hir().get_parent_did(hir_id).to_def_id()).is_none() {
297-
report_assoc_ty_on_inherent_impl(tcx, item.span);
296+
check_feature_inherent_assoc_ty(tcx, item.span);
298297
}
299298

300299
icx.to_ty(ty)
@@ -746,6 +745,16 @@ fn infer_placeholder_type(
746745
})
747746
}
748747

749-
fn report_assoc_ty_on_inherent_impl(tcx: TyCtxt<'_>, span: Span) {
750-
tcx.sess.emit_err(AssocTypeOnInherentImpl { span });
748+
fn check_feature_inherent_assoc_ty(tcx: TyCtxt<'_>, span: Span) {
749+
if !tcx.features().inherent_associated_types {
750+
use rustc_session::parse::feature_err;
751+
use rustc_span::symbol::sym;
752+
feature_err(
753+
&tcx.sess.parse_sess,
754+
sym::inherent_associated_types,
755+
span,
756+
"inherent associated types are unstable",
757+
)
758+
.emit();
759+
}
751760
}

Diff for: compiler/rustc_typeck/src/errors.rs

-7
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,6 @@ pub struct CopyImplOnTypeWithDtor {
8282
pub span: Span,
8383
}
8484

85-
#[derive(SessionDiagnostic)]
86-
#[error = "E0202"]
87-
pub struct AssocTypeOnInherentImpl {
88-
#[message = "associated types are not yet supported in inherent impls (see #8995)"]
89-
pub span: Span,
90-
}
91-
9285
#[derive(SessionDiagnostic)]
9386
#[error = "E0203"]
9487
pub struct MultipleRelaxedDefaultBounds {

Diff for: src/test/ui/assoc-inherent.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
// Test associated types are, until #8995 is implemented, forbidden in inherent impls.
1+
// Test that inherent associated types work with
2+
// inherent_associated_types feature gate.
3+
4+
#![feature(inherent_associated_types)]
5+
#![allow(incomplete_features)]
26

37
struct Foo;
48

59
impl Foo {
6-
type Bar = isize; //~ERROR associated types are not yet supported in inherent impls (see #8995)
10+
type Bar = isize;
711
}
812

9-
fn main() {}
13+
impl Foo {
14+
type Baz; //~ ERROR associated type in `impl` without body
15+
}
16+
17+
fn main() {
18+
let x : Foo::Bar; //~ERROR ambiguous associated type
19+
x = 0isize;
20+
}

Diff for: src/test/ui/assoc-inherent.stderr

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
error[E0202]: associated types are not yet supported in inherent impls (see #8995)
2-
--> $DIR/assoc-inherent.rs:6:5
1+
error: associated type in `impl` without body
2+
--> $DIR/assoc-inherent.rs:14:5
33
|
4-
LL | type Bar = isize;
5-
| ^^^^^^^^^^^^^^^^^
4+
LL | type Baz;
5+
| ^^^^^^^^-
6+
| |
7+
| help: provide a definition for the type: `= <type>;`
68

7-
error: aborting due to previous error
9+
error[E0223]: ambiguous associated type
10+
--> $DIR/assoc-inherent.rs:18:13
11+
|
12+
LL | let x : Foo::Bar;
13+
| ^^^^^^^^ help: use fully-qualified syntax: `<Foo as Trait>::Bar`
14+
15+
error: aborting due to 2 previous errors
816

9-
For more information about this error, try `rustc --explain E0202`.
17+
For more information about this error, try `rustc --explain E0223`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Test that inherent associated types cannot be used when inherent_associated_types
2+
// feature gate is not used.
3+
4+
struct Foo;
5+
6+
impl Foo {
7+
type Bar = isize; //~ERROR inherent associated types are unstable
8+
}
9+
10+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0658]: inherent associated types are unstable
2+
--> $DIR/feature-gate-inherent_associated_types.rs:7:5
3+
|
4+
LL | type Bar = isize;
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #8995 <https://github.com/rust-lang/rust/issues/8995> for more information
8+
= help: add `#![feature(inherent_associated_types)]` to the crate attributes to enable
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0658`.

Diff for: src/test/ui/parser/impl-item-type-no-body-semantic-fail.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ struct X;
88
impl X {
99
type Y;
1010
//~^ ERROR associated type in `impl` without body
11-
//~| ERROR associated types are not yet supported in inherent impls
11+
//~| ERROR inherent associated types are unstable
1212
type Z: Ord;
1313
//~^ ERROR associated type in `impl` without body
1414
//~| ERROR bounds on `type`s in `impl`s have no effect
15-
//~| ERROR associated types are not yet supported in inherent impls
15+
//~| ERROR inherent associated types are unstable
1616
type W: Ord where Self: Eq;
1717
//~^ ERROR associated type in `impl` without body
1818
//~| ERROR bounds on `type`s in `impl`s have no effect
19-
//~| ERROR associated types are not yet supported in inherent impls
19+
//~| ERROR inherent associated types are unstable
2020
type W where Self: Eq;
2121
//~^ ERROR associated type in `impl` without body
22-
//~| ERROR associated types are not yet supported in inherent impls
22+
//~| ERROR inherent associated types are unstable
2323
}

Diff for: src/test/ui/parser/impl-item-type-no-body-semantic-fail.stderr

+17-5
Original file line numberDiff line numberDiff line change
@@ -51,30 +51,42 @@ LL | #![feature(generic_associated_types)]
5151
= note: `#[warn(incomplete_features)]` on by default
5252
= note: see issue #44265 <https://github.com/rust-lang/rust/issues/44265> for more information
5353

54-
error[E0202]: associated types are not yet supported in inherent impls (see #8995)
54+
error[E0658]: inherent associated types are unstable
5555
--> $DIR/impl-item-type-no-body-semantic-fail.rs:9:5
5656
|
5757
LL | type Y;
5858
| ^^^^^^^
59+
|
60+
= note: see issue #8995 <https://github.com/rust-lang/rust/issues/8995> for more information
61+
= help: add `#![feature(inherent_associated_types)]` to the crate attributes to enable
5962

60-
error[E0202]: associated types are not yet supported in inherent impls (see #8995)
63+
error[E0658]: inherent associated types are unstable
6164
--> $DIR/impl-item-type-no-body-semantic-fail.rs:12:5
6265
|
6366
LL | type Z: Ord;
6467
| ^^^^^^^^^^^^
68+
|
69+
= note: see issue #8995 <https://github.com/rust-lang/rust/issues/8995> for more information
70+
= help: add `#![feature(inherent_associated_types)]` to the crate attributes to enable
6571

66-
error[E0202]: associated types are not yet supported in inherent impls (see #8995)
72+
error[E0658]: inherent associated types are unstable
6773
--> $DIR/impl-item-type-no-body-semantic-fail.rs:16:5
6874
|
6975
LL | type W: Ord where Self: Eq;
7076
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
77+
|
78+
= note: see issue #8995 <https://github.com/rust-lang/rust/issues/8995> for more information
79+
= help: add `#![feature(inherent_associated_types)]` to the crate attributes to enable
7180

72-
error[E0202]: associated types are not yet supported in inherent impls (see #8995)
81+
error[E0658]: inherent associated types are unstable
7382
--> $DIR/impl-item-type-no-body-semantic-fail.rs:20:5
7483
|
7584
LL | type W where Self: Eq;
7685
| ^^^^^^^^^^^^^^^^^^^^^^
86+
|
87+
= note: see issue #8995 <https://github.com/rust-lang/rust/issues/8995> for more information
88+
= help: add `#![feature(inherent_associated_types)]` to the crate attributes to enable
7789

7890
error: aborting due to 10 previous errors; 1 warning emitted
7991

80-
For more information about this error, try `rustc --explain E0202`.
92+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)