Skip to content

Commit a63f8c1

Browse files
authoredSep 16, 2020
Rollup merge of #76719 - hameerabbasi:min-const-generics-ty, r=lcnr
Change error message for ty param in const This PR introduces the following changes: * Change error message for type param in a const expression when using `min_const_generics` * Change `ParamInNonTrivialAnonConst` to contain an extra `bool` used for distinguishing whether the passed-in symbol is a type or a value. Fixes #76701
2 parents ece688b + 5f0b775 commit a63f8c1

9 files changed

+80
-14
lines changed
 

‎compiler/rustc_resolve/src/diagnostics.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ impl<'a> Resolver<'a> {
466466
);
467467
err
468468
}
469-
ResolutionError::ParamInNonTrivialAnonConst(name) => {
469+
ResolutionError::ParamInNonTrivialAnonConst { name, is_type } => {
470470
let mut err = self.session.struct_span_err(
471471
span,
472472
"generic parameters must not be used inside of non trivial constant values",
@@ -478,9 +478,17 @@ impl<'a> Resolver<'a> {
478478
name
479479
),
480480
);
481-
err.help(
482-
&format!("it is currently only allowed to use either `{0}` or `{{ {0} }}` as generic constants", name)
483-
);
481+
482+
if is_type {
483+
err.note("type parameters are currently not permitted in anonymous constants");
484+
} else {
485+
err.help(
486+
&format!("it is currently only allowed to use either `{0}` or `{{ {0} }}` as generic constants",
487+
name
488+
)
489+
);
490+
}
491+
484492
err
485493
}
486494
ResolutionError::SelfInTyParamDefault => {

‎compiler/rustc_resolve/src/lib.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ enum ResolutionError<'a> {
221221
/// generic parameters must not be used inside of non trivial constant values.
222222
///
223223
/// This error is only emitted when using `min_const_generics`.
224-
ParamInNonTrivialAnonConst(Symbol),
224+
ParamInNonTrivialAnonConst { name: Symbol, is_type: bool },
225225
/// Error E0735: type parameters with a default cannot use `Self`
226226
SelfInTyParamDefault,
227227
/// Error E0767: use of unreachable label
@@ -2638,9 +2638,10 @@ impl<'a> Resolver<'a> {
26382638
if record_used {
26392639
self.report_error(
26402640
span,
2641-
ResolutionError::ParamInNonTrivialAnonConst(
2642-
rib_ident.name,
2643-
),
2641+
ResolutionError::ParamInNonTrivialAnonConst {
2642+
name: rib_ident.name,
2643+
is_type: true,
2644+
},
26442645
);
26452646
}
26462647
return Res::Err;
@@ -2718,7 +2719,10 @@ impl<'a> Resolver<'a> {
27182719
if record_used {
27192720
self.report_error(
27202721
span,
2721-
ResolutionError::ParamInNonTrivialAnonConst(rib_ident.name),
2722+
ResolutionError::ParamInNonTrivialAnonConst {
2723+
name: rib_ident.name,
2724+
is_type: false,
2725+
},
27222726
);
27232727
}
27242728
return Res::Err;

‎src/test/ui/const-generics/issues/issue-64494.min.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ error: generic parameters must not be used inside of non trivial constant values
44
LL | impl<T: Foo> MyTrait for T where Is<{T::VAL == 5}>: True {}
55
| ^^^^^^ non-trivial anonymous constants must not depend on the parameter `T`
66
|
7-
= help: it is currently only allowed to use either `T` or `{ T }` as generic constants
7+
= note: type parameters are currently not permitted in anonymous constants
88

99
error: generic parameters must not be used inside of non trivial constant values
1010
--> $DIR/issue-64494.rs:19:38
1111
|
1212
LL | impl<T: Foo> MyTrait for T where Is<{T::VAL == 6}>: True {}
1313
| ^^^^^^ non-trivial anonymous constants must not depend on the parameter `T`
1414
|
15-
= help: it is currently only allowed to use either `T` or `{ T }` as generic constants
15+
= note: type parameters are currently not permitted in anonymous constants
1616

1717
error[E0119]: conflicting implementations of trait `MyTrait`:
1818
--> $DIR/issue-64494.rs:19:1

‎src/test/ui/const-generics/issues/issue-67739.min.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: generic parameters must not be used inside of non trivial constant values
44
LL | [0u8; mem::size_of::<Self::Associated>()];
55
| ^^^^^^^^^^^^^^^^ non-trivial anonymous constants must not depend on the parameter `Self`
66
|
7-
= help: it is currently only allowed to use either `Self` or `{ Self }` as generic constants
7+
= note: type parameters are currently not permitted in anonymous constants
88

99
error: aborting due to previous error
1010

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: constant expression depends on a generic parameter
2+
--> $DIR/issue-76701-ty-param-in-const.rs:6:21
3+
|
4+
LL | fn ty_param<T>() -> [u8; std::mem::size_of::<T>()] {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: this may fail depending on what value the parameter takes
8+
9+
error: constant expression depends on a generic parameter
10+
--> $DIR/issue-76701-ty-param-in-const.rs:12:37
11+
|
12+
LL | fn const_param<const N: usize>() -> [u8; N + 1] {
13+
| ^^^^^^^^^^^
14+
|
15+
= note: this may fail depending on what value the parameter takes
16+
17+
error: aborting due to 2 previous errors
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: generic parameters must not be used inside of non trivial constant values
2+
--> $DIR/issue-76701-ty-param-in-const.rs:6:46
3+
|
4+
LL | fn ty_param<T>() -> [u8; std::mem::size_of::<T>()] {
5+
| ^ non-trivial anonymous constants must not depend on the parameter `T`
6+
|
7+
= note: type parameters are currently not permitted in anonymous constants
8+
9+
error: generic parameters must not be used inside of non trivial constant values
10+
--> $DIR/issue-76701-ty-param-in-const.rs:12:42
11+
|
12+
LL | fn const_param<const N: usize>() -> [u8; N + 1] {
13+
| ^ non-trivial anonymous constants must not depend on the parameter `N`
14+
|
15+
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
16+
17+
error: aborting due to 2 previous errors
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// revisions: full min
2+
#![cfg_attr(full, feature(const_generics))]
3+
#![cfg_attr(full, allow(incomplete_features))]
4+
#![cfg_attr(min, feature(min_const_generics))]
5+
6+
fn ty_param<T>() -> [u8; std::mem::size_of::<T>()] {
7+
//[full]~^ ERROR constant expression depends on a generic parameter
8+
//[min]~^^ ERROR generic parameters must not be used inside of non trivial constant values
9+
todo!()
10+
}
11+
12+
fn const_param<const N: usize>() -> [u8; N + 1] {
13+
//[full]~^ ERROR constant expression depends on a generic parameter
14+
//[min]~^^ ERROR generic parameters must not be used inside of non trivial constant values
15+
todo!()
16+
}
17+
18+
fn main() {}

‎src/test/ui/const-generics/min_const_generics/self-ty-in-const-1.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: generic parameters must not be used inside of non trivial constant values
44
LL | fn t1() -> [u8; std::mem::size_of::<Self>()];
55
| ^^^^ non-trivial anonymous constants must not depend on the parameter `Self`
66
|
7-
= help: it is currently only allowed to use either `Self` or `{ Self }` as generic constants
7+
= note: type parameters are currently not permitted in anonymous constants
88

99
error: generic `Self` types are currently not permitted in anonymous constants
1010
--> $DIR/self-ty-in-const-1.rs:14:41

‎src/test/ui/const-generics/params-in-ct-in-ty-param-lazy-norm.min.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ error: generic parameters must not be used inside of non trivial constant values
1212
LL | struct Foo<T, U = [u8; std::mem::size_of::<T>()]>(T, U);
1313
| ^ non-trivial anonymous constants must not depend on the parameter `T`
1414
|
15-
= help: it is currently only allowed to use either `T` or `{ T }` as generic constants
15+
= note: type parameters are currently not permitted in anonymous constants
1616

1717
error: constant values inside of type parameter defaults must not depend on generic parameters
1818
--> $DIR/params-in-ct-in-ty-param-lazy-norm.rs:12:21

0 commit comments

Comments
 (0)
Please sign in to comment.