Skip to content

Commit 7e2175e

Browse files
committed
Forbid generic parameters in types of #[type_const] items
1 parent 50d5940 commit 7e2175e

11 files changed

+228
-17
lines changed

compiler/rustc_resolve/src/late.rs

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2855,6 +2855,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
28552855
ref define_opaque,
28562856
..
28572857
}) => {
2858+
let is_type_const = attr::contains_name(&item.attrs, sym::type_const);
28582859
self.with_generic_param_rib(
28592860
&generics.params,
28602861
RibKind::Item(
@@ -2873,7 +2874,22 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
28732874

28742875
this.with_lifetime_rib(
28752876
LifetimeRibKind::Elided(LifetimeRes::Static),
2876-
|this| this.visit_ty(ty),
2877+
|this| {
2878+
if is_type_const
2879+
&& !this.r.tcx.features().generic_const_parameter_types()
2880+
{
2881+
this.with_rib(TypeNS, RibKind::ConstParamTy, |this| {
2882+
this.with_rib(ValueNS, RibKind::ConstParamTy, |this| {
2883+
this.with_lifetime_rib(
2884+
LifetimeRibKind::ConstParamTy,
2885+
|this| this.visit_ty(ty),
2886+
)
2887+
})
2888+
});
2889+
} else {
2890+
this.visit_ty(ty);
2891+
}
2892+
},
28772893
);
28782894

28792895
if let Some(rhs) = rhs {
@@ -3213,6 +3229,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
32133229
define_opaque,
32143230
..
32153231
}) => {
3232+
let is_type_const = attr::contains_name(&item.attrs, sym::type_const);
32163233
self.with_generic_param_rib(
32173234
&generics.params,
32183235
RibKind::AssocItem,
@@ -3227,7 +3244,20 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
32273244
},
32283245
|this| {
32293246
this.visit_generics(generics);
3230-
this.visit_ty(ty);
3247+
if is_type_const
3248+
&& !this.r.tcx.features().generic_const_parameter_types()
3249+
{
3250+
this.with_rib(TypeNS, RibKind::ConstParamTy, |this| {
3251+
this.with_rib(ValueNS, RibKind::ConstParamTy, |this| {
3252+
this.with_lifetime_rib(
3253+
LifetimeRibKind::ConstParamTy,
3254+
|this| this.visit_ty(ty),
3255+
)
3256+
})
3257+
});
3258+
} else {
3259+
this.visit_ty(ty);
3260+
}
32313261

32323262
// Only impose the restrictions of `ConstRibKind` for an
32333263
// actual constant expression in a provided default.
@@ -3422,6 +3452,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
34223452
..
34233453
}) => {
34243454
debug!("resolve_implementation AssocItemKind::Const");
3455+
let is_type_const = attr::contains_name(&item.attrs, sym::type_const);
34253456
self.with_generic_param_rib(
34263457
&generics.params,
34273458
RibKind::AssocItem,
@@ -3458,7 +3489,28 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
34583489
);
34593490

34603491
this.visit_generics(generics);
3461-
this.visit_ty(ty);
3492+
if is_type_const
3493+
&& !this
3494+
.r
3495+
.tcx
3496+
.features()
3497+
.generic_const_parameter_types()
3498+
{
3499+
this.with_rib(TypeNS, RibKind::ConstParamTy, |this| {
3500+
this.with_rib(
3501+
ValueNS,
3502+
RibKind::ConstParamTy,
3503+
|this| {
3504+
this.with_lifetime_rib(
3505+
LifetimeRibKind::ConstParamTy,
3506+
|this| this.visit_ty(ty),
3507+
)
3508+
},
3509+
)
3510+
});
3511+
} else {
3512+
this.visit_ty(ty);
3513+
}
34623514
if let Some(rhs) = rhs {
34633515
// We allow arbitrary const expressions inside of associated consts,
34643516
// 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-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:10: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:15: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:20: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:40: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:45: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:50:39
33+
|
34+
LL | const ASSOC_LT<'a>: [&'a (); 0] = const { [] };
35+
| ^^^^^^^^^^^^
36+
37+
error: aborting due to 6 previous errors
38+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
error[E0770]: the type of const parameters must not depend on other generic parameters
2+
--> $DIR/type_const-generic-param-in-type.rs:10:45
3+
|
4+
LL | const FOO<T: core::marker::ConstParamTy_>: [T; 0] = const { [] };
5+
| ^ the type must not depend on the parameter `T`
6+
7+
error[E0770]: the type of const parameters must not depend on other generic parameters
8+
--> $DIR/type_const-generic-param-in-type.rs:15:33
9+
|
10+
LL | const BAR<const N: usize>: [(); N] = const { [] };
11+
| ^ the type must not depend on the parameter `N`
12+
13+
error[E0770]: the type of const parameters must not depend on other generic parameters
14+
--> $DIR/type_const-generic-param-in-type.rs:20:18
15+
|
16+
LL | const BAZ<'a>: [&'a (); 0] = const { [] };
17+
| ^^ the type must not depend on the parameter `'a`
18+
19+
error[E0770]: the type of const parameters must not depend on other generic parameters
20+
--> $DIR/type_const-generic-param-in-type.rs:26:51
21+
|
22+
LL | const ASSOC<T: core::marker::ConstParamTy_>: [T; 0];
23+
| ^ the type must not depend on the parameter `T`
24+
25+
error[E0770]: the type of const parameters must not depend on other generic parameters
26+
--> $DIR/type_const-generic-param-in-type.rs:30:45
27+
|
28+
LL | const ASSOC_CONST<const N: usize>: [(); N];
29+
| ^ the type must not depend on the parameter `N`
30+
31+
error[E0770]: the type of const parameters must not depend on other generic parameters
32+
--> $DIR/type_const-generic-param-in-type.rs:34:27
33+
|
34+
LL | const ASSOC_LT<'a>: [&'a (); 0];
35+
| ^^ the type must not depend on the parameter `'a`
36+
37+
error[E0770]: the type of const parameters must not depend on other generic parameters
38+
--> $DIR/type_const-generic-param-in-type.rs:40:51
39+
|
40+
LL | const ASSOC<T: core::marker::ConstParamTy_>: [T; 0] = const { [] };
41+
| ^ the type must not depend on the parameter `T`
42+
43+
error[E0770]: the type of const parameters must not depend on other generic parameters
44+
--> $DIR/type_const-generic-param-in-type.rs:45:45
45+
|
46+
LL | const ASSOC_CONST<const N: usize>: [(); N] = const { [] };
47+
| ^ the type must not depend on the parameter `N`
48+
49+
error[E0770]: the type of const parameters must not depend on other generic parameters
50+
--> $DIR/type_const-generic-param-in-type.rs:50:27
51+
|
52+
LL | const ASSOC_LT<'a>: [&'a (); 0] = const { [] };
53+
| ^^ the type must not depend on the parameter `'a`
54+
55+
error: aborting due to 9 previous errors
56+
57+
For more information about this error, try `rustc --explain E0770`.

0 commit comments

Comments
 (0)