Skip to content

Commit 05dce48

Browse files
authored
Rollup merge of #150650 - enthropy7:main, r=BoxyUwU
Forbid generic parameters in types of #[type_const] items fixes #150614 we enforce the same restriction on `#[type_const]` const items that already exists for const generic parameters - when `generic_const_parameter_types` feature gate is not enabled, the type of a `#[type_const]` item cannot reference generic parameters. implementation follows the same pattern used for const generic parameters. we check if the item has the `#[type_const]` attribute and if the feature gate is disabled, then we apply `RibKind::ConstParamTy` restrictions when visiting the type, which prevents the use of generic parameters. check is added in three places: - Free const items (`ItemKind::Const`) - Trait associated consts (`AssocItemKind::Const` in traits) - Impl associated consts (`AssocItemKind::Const` in impls) added tests for new feature, hope i get this task right :> r? `@BoxyUwU`
2 parents f63ab49 + 46bb414 commit 05dce48

12 files changed

Lines changed: 228 additions & 18 deletions

compiler/rustc_resolve/src/late.rs

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2851,6 +2851,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
28512851
ref define_opaque,
28522852
..
28532853
}) => {
2854+
let is_type_const = attr::contains_name(&item.attrs, sym::type_const);
28542855
self.with_generic_param_rib(
28552856
&generics.params,
28562857
RibKind::Item(
@@ -2869,7 +2870,22 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
28692870

28702871
this.with_lifetime_rib(
28712872
LifetimeRibKind::Elided(LifetimeRes::Static),
2872-
|this| this.visit_ty(ty),
2873+
|this| {
2874+
if is_type_const
2875+
&& !this.r.tcx.features().generic_const_parameter_types()
2876+
{
2877+
this.with_rib(TypeNS, RibKind::ConstParamTy, |this| {
2878+
this.with_rib(ValueNS, RibKind::ConstParamTy, |this| {
2879+
this.with_lifetime_rib(
2880+
LifetimeRibKind::ConstParamTy,
2881+
|this| this.visit_ty(ty),
2882+
)
2883+
})
2884+
});
2885+
} else {
2886+
this.visit_ty(ty);
2887+
}
2888+
},
28732889
);
28742890

28752891
if let Some(rhs) = rhs {
@@ -3209,6 +3225,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
32093225
define_opaque,
32103226
..
32113227
}) => {
3228+
let is_type_const = attr::contains_name(&item.attrs, sym::type_const);
32123229
self.with_generic_param_rib(
32133230
&generics.params,
32143231
RibKind::AssocItem,
@@ -3223,7 +3240,20 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
32233240
},
32243241
|this| {
32253242
this.visit_generics(generics);
3226-
this.visit_ty(ty);
3243+
if is_type_const
3244+
&& !this.r.tcx.features().generic_const_parameter_types()
3245+
{
3246+
this.with_rib(TypeNS, RibKind::ConstParamTy, |this| {
3247+
this.with_rib(ValueNS, RibKind::ConstParamTy, |this| {
3248+
this.with_lifetime_rib(
3249+
LifetimeRibKind::ConstParamTy,
3250+
|this| this.visit_ty(ty),
3251+
)
3252+
})
3253+
});
3254+
} else {
3255+
this.visit_ty(ty);
3256+
}
32273257

32283258
// Only impose the restrictions of `ConstRibKind` for an
32293259
// actual constant expression in a provided default.
@@ -3417,6 +3447,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
34173447
..
34183448
}) => {
34193449
debug!("resolve_implementation AssocItemKind::Const");
3450+
let is_type_const = attr::contains_name(&item.attrs, sym::type_const);
34203451
self.with_generic_param_rib(
34213452
&generics.params,
34223453
RibKind::AssocItem,
@@ -3453,7 +3484,28 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
34533484
);
34543485

34553486
this.visit_generics(generics);
3456-
this.visit_ty(ty);
3487+
if is_type_const
3488+
&& !this
3489+
.r
3490+
.tcx
3491+
.features()
3492+
.generic_const_parameter_types()
3493+
{
3494+
this.with_rib(TypeNS, RibKind::ConstParamTy, |this| {
3495+
this.with_rib(
3496+
ValueNS,
3497+
RibKind::ConstParamTy,
3498+
|this| {
3499+
this.with_lifetime_rib(
3500+
LifetimeRibKind::ConstParamTy,
3501+
|this| this.visit_ty(ty),
3502+
)
3503+
},
3504+
)
3505+
});
3506+
} else {
3507+
this.visit_ty(ty);
3508+
}
34573509
if let Some(rhs) = rhs {
34583510
// We allow arbitrary const expressions inside of associated consts,
34593511
// even if they are potentially not const evaluatable.

tests/ui/associated-consts/assoc-const-eq-bound-var-in-ty-not-wf.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
min_generic_const_args,
66
adt_const_params,
77
unsized_const_params,
8+
generic_const_parameter_types,
89
)]
910
#![allow(incomplete_features)]
1011

tests/ui/associated-consts/assoc-const-eq-bound-var-in-ty-not-wf.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error: higher-ranked subtype error
2-
--> $DIR/assoc-const-eq-bound-var-in-ty-not-wf.rs:21:13
2+
--> $DIR/assoc-const-eq-bound-var-in-ty-not-wf.rs:22:13
33
|
44
LL | K = const { () }
55
| ^^^^^^^^^^^^
66

77
error: higher-ranked subtype error
8-
--> $DIR/assoc-const-eq-bound-var-in-ty-not-wf.rs:21:13
8+
--> $DIR/assoc-const-eq-bound-var-in-ty-not-wf.rs:22:13
99
|
1010
LL | K = const { () }
1111
| ^^^^^^^^^^^^

tests/ui/associated-consts/assoc-const-eq-bound-var-in-ty.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
min_generic_const_args,
99
adt_const_params,
1010
unsized_const_params,
11+
generic_const_parameter_types,
1112
)]
1213
#![allow(incomplete_features)]
1314

tests/ui/associated-consts/assoc-const-eq-esc-bound-var-in-ty.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
// Detect and reject escaping late-bound generic params in
22
// the type of assoc consts used in an equality bound.
3-
#![feature(associated_const_equality, min_generic_const_args, unsized_const_params)]
3+
#![feature(
4+
associated_const_equality,
5+
min_generic_const_args,
6+
unsized_const_params,
7+
generic_const_parameter_types,
8+
)]
49
#![allow(incomplete_features)]
510

611
trait Trait<'a> {

tests/ui/associated-consts/assoc-const-eq-esc-bound-var-in-ty.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: the type of the associated constant `K` cannot capture late-bound generic parameters
2-
--> $DIR/assoc-const-eq-esc-bound-var-in-ty.rs:11:35
2+
--> $DIR/assoc-const-eq-esc-bound-var-in-ty.rs:16:35
33
|
44
LL | fn take(_: impl for<'r> Trait<'r, K = const { &() }>) {}
55
| -- ^ its type cannot capture the late-bound lifetime parameter `'r`

tests/ui/associated-consts/assoc-const-eq-param-in-ty.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
min_generic_const_args,
66
adt_const_params,
77
unsized_const_params,
8+
generic_const_parameter_types,
89
)]
910
#![allow(incomplete_features)]
1011

tests/ui/associated-consts/assoc-const-eq-param-in-ty.stderr

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: the type of the associated constant `K` must not depend on generic parameters
2-
--> $DIR/assoc-const-eq-param-in-ty.rs:22:29
2+
--> $DIR/assoc-const-eq-param-in-ty.rs:23:29
33
|
44
LL | fn take0<'r, A: 'r + ConstParamTy_, const Q: usize>(
55
| -- the lifetime parameter `'r` is defined here
@@ -10,7 +10,7 @@ LL | _: impl Trait<'r, A, Q, K = const { loop {} }>
1010
= note: `K` has type `&'r [A; Q]`
1111

1212
error: the type of the associated constant `K` must not depend on generic parameters
13-
--> $DIR/assoc-const-eq-param-in-ty.rs:22:29
13+
--> $DIR/assoc-const-eq-param-in-ty.rs:23:29
1414
|
1515
LL | fn take0<'r, A: 'r + ConstParamTy_, const Q: usize>(
1616
| - the type parameter `A` is defined here
@@ -21,7 +21,7 @@ LL | _: impl Trait<'r, A, Q, K = const { loop {} }>
2121
= note: `K` has type `&'r [A; Q]`
2222

2323
error: the type of the associated constant `K` must not depend on generic parameters
24-
--> $DIR/assoc-const-eq-param-in-ty.rs:22:29
24+
--> $DIR/assoc-const-eq-param-in-ty.rs:23:29
2525
|
2626
LL | fn take0<'r, A: 'r + ConstParamTy_, const Q: usize>(
2727
| - the const parameter `Q` is defined here
@@ -32,7 +32,7 @@ LL | _: impl Trait<'r, A, Q, K = const { loop {} }>
3232
= note: `K` has type `&'r [A; Q]`
3333

3434
error: the type of the associated constant `SELF` must not depend on `impl Trait`
35-
--> $DIR/assoc-const-eq-param-in-ty.rs:39:26
35+
--> $DIR/assoc-const-eq-param-in-ty.rs:40:26
3636
|
3737
LL | fn take1(_: impl Project<SELF = const {}>) {}
3838
| -------------^^^^------------
@@ -41,7 +41,7 @@ LL | fn take1(_: impl Project<SELF = const {}>) {}
4141
| the `impl Trait` is specified here
4242

4343
error: the type of the associated constant `SELF` must not depend on generic parameters
44-
--> $DIR/assoc-const-eq-param-in-ty.rs:44:21
44+
--> $DIR/assoc-const-eq-param-in-ty.rs:45:21
4545
|
4646
LL | fn take2<P: Project<SELF = const {}>>(_: P) {}
4747
| - ^^^^ its type must not depend on the type parameter `P`
@@ -51,7 +51,7 @@ LL | fn take2<P: Project<SELF = const {}>>(_: P) {}
5151
= note: `SELF` has type `P`
5252

5353
error: the type of the associated constant `K` must not depend on generic parameters
54-
--> $DIR/assoc-const-eq-param-in-ty.rs:53:52
54+
--> $DIR/assoc-const-eq-param-in-ty.rs:54:52
5555
|
5656
LL | trait Iface<'r>: ConstParamTy_ {
5757
| -- the lifetime parameter `'r` is defined here
@@ -62,15 +62,15 @@ LL | type Assoc<const Q: usize>: Trait<'r, Self, Q, K = const { loop {} }>
6262
= note: `K` has type `&'r [Self; Q]`
6363

6464
error: the type of the associated constant `K` must not depend on `Self`
65-
--> $DIR/assoc-const-eq-param-in-ty.rs:53:52
65+
--> $DIR/assoc-const-eq-param-in-ty.rs:54:52
6666
|
6767
LL | type Assoc<const Q: usize>: Trait<'r, Self, Q, K = const { loop {} }>
6868
| ^ its type must not depend on `Self`
6969
|
7070
= note: `K` has type `&'r [Self; Q]`
7171

7272
error: the type of the associated constant `K` must not depend on generic parameters
73-
--> $DIR/assoc-const-eq-param-in-ty.rs:53:52
73+
--> $DIR/assoc-const-eq-param-in-ty.rs:54:52
7474
|
7575
LL | type Assoc<const Q: usize>: Trait<'r, Self, Q, K = const { loop {} }>
7676
| - ^ its type must not depend on the const parameter `Q`
@@ -80,7 +80,7 @@ LL | type Assoc<const Q: usize>: Trait<'r, Self, Q, K = const { loop {} }>
8080
= note: `K` has type `&'r [Self; Q]`
8181

8282
error: the type of the associated constant `K` must not depend on generic parameters
83-
--> $DIR/assoc-const-eq-param-in-ty.rs:53:52
83+
--> $DIR/assoc-const-eq-param-in-ty.rs:54:52
8484
|
8585
LL | trait Iface<'r>: ConstParamTy_ {
8686
| -- the lifetime parameter `'r` is defined here
@@ -92,7 +92,7 @@ LL | type Assoc<const Q: usize>: Trait<'r, Self, Q, K = const { loop {} }>
9292
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
9393

9494
error: the type of the associated constant `K` must not depend on `Self`
95-
--> $DIR/assoc-const-eq-param-in-ty.rs:53:52
95+
--> $DIR/assoc-const-eq-param-in-ty.rs:54:52
9696
|
9797
LL | type Assoc<const Q: usize>: Trait<'r, Self, Q, K = const { loop {} }>
9898
| ^ its type must not depend on `Self`
@@ -101,7 +101,7 @@ LL | type Assoc<const Q: usize>: Trait<'r, Self, Q, K = const { loop {} }>
101101
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
102102

103103
error: the type of the associated constant `K` must not depend on generic parameters
104-
--> $DIR/assoc-const-eq-param-in-ty.rs:53:52
104+
--> $DIR/assoc-const-eq-param-in-ty.rs:54:52
105105
|
106106
LL | type Assoc<const Q: usize>: Trait<'r, Self, Q, K = const { loop {} }>
107107
| - ^ its type must not depend on the const parameter `Q`

tests/ui/associated-consts/assoc-const-eq-supertraits.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
min_generic_const_args,
99
adt_const_params,
1010
unsized_const_params,
11+
generic_const_parameter_types,
1112
)]
1213
#![allow(incomplete_features)]
1314

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
error: anonymous constants referencing generics are not yet supported
2+
--> $DIR/type_const-generic-param-in-type.rs:9:53
3+
|
4+
LL | const FOO<T: core::marker::ConstParamTy_>: [T; 0] = const { [] };
5+
| ^^^^^^^^^^^^
6+
7+
error: anonymous constants referencing generics are not yet supported
8+
--> $DIR/type_const-generic-param-in-type.rs:14:38
9+
|
10+
LL | const BAR<const N: usize>: [(); N] = const { [] };
11+
| ^^^^^^^^^^^^
12+
13+
error: anonymous constants with lifetimes in their type are not yet supported
14+
--> $DIR/type_const-generic-param-in-type.rs:19:30
15+
|
16+
LL | const BAZ<'a>: [&'a (); 0] = const { [] };
17+
| ^^^^^^^^^^^^
18+
19+
error: anonymous constants referencing generics are not yet supported
20+
--> $DIR/type_const-generic-param-in-type.rs:39:59
21+
|
22+
LL | const ASSOC<T: core::marker::ConstParamTy_>: [T; 0] = const { [] };
23+
| ^^^^^^^^^^^^
24+
25+
error: anonymous constants referencing generics are not yet supported
26+
--> $DIR/type_const-generic-param-in-type.rs:44:50
27+
|
28+
LL | const ASSOC_CONST<const N: usize>: [(); N] = const { [] };
29+
| ^^^^^^^^^^^^
30+
31+
error: anonymous constants with lifetimes in their type are not yet supported
32+
--> $DIR/type_const-generic-param-in-type.rs:49:39
33+
|
34+
LL | const ASSOC_LT<'a>: [&'a (); 0] = const { [] };
35+
| ^^^^^^^^^^^^
36+
37+
error: aborting due to 6 previous errors
38+

0 commit comments

Comments
 (0)