Skip to content

Commit

Permalink
Rollup merge of #76719 - hameerabbasi:min-const-generics-ty, r=lcnr
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tmandry authored Sep 16, 2020
2 parents ece688b + 5f0b775 commit a63f8c1
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 14 deletions.
16 changes: 12 additions & 4 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ impl<'a> Resolver<'a> {
);
err
}
ResolutionError::ParamInNonTrivialAnonConst(name) => {
ResolutionError::ParamInNonTrivialAnonConst { name, is_type } => {
let mut err = self.session.struct_span_err(
span,
"generic parameters must not be used inside of non trivial constant values",
Expand All @@ -478,9 +478,17 @@ impl<'a> Resolver<'a> {
name
),
);
err.help(
&format!("it is currently only allowed to use either `{0}` or `{{ {0} }}` as generic constants", name)
);

if is_type {
err.note("type parameters are currently not permitted in anonymous constants");
} else {
err.help(
&format!("it is currently only allowed to use either `{0}` or `{{ {0} }}` as generic constants",
name
)
);
}

err
}
ResolutionError::SelfInTyParamDefault => {
Expand Down
14 changes: 9 additions & 5 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ enum ResolutionError<'a> {
/// generic parameters must not be used inside of non trivial constant values.
///
/// This error is only emitted when using `min_const_generics`.
ParamInNonTrivialAnonConst(Symbol),
ParamInNonTrivialAnonConst { name: Symbol, is_type: bool },
/// Error E0735: type parameters with a default cannot use `Self`
SelfInTyParamDefault,
/// Error E0767: use of unreachable label
Expand Down Expand Up @@ -2638,9 +2638,10 @@ impl<'a> Resolver<'a> {
if record_used {
self.report_error(
span,
ResolutionError::ParamInNonTrivialAnonConst(
rib_ident.name,
),
ResolutionError::ParamInNonTrivialAnonConst {
name: rib_ident.name,
is_type: true,
},
);
}
return Res::Err;
Expand Down Expand Up @@ -2718,7 +2719,10 @@ impl<'a> Resolver<'a> {
if record_used {
self.report_error(
span,
ResolutionError::ParamInNonTrivialAnonConst(rib_ident.name),
ResolutionError::ParamInNonTrivialAnonConst {
name: rib_ident.name,
is_type: false,
},
);
}
return Res::Err;
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/const-generics/issues/issue-64494.min.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ error: generic parameters must not be used inside of non trivial constant values
LL | impl<T: Foo> MyTrait for T where Is<{T::VAL == 5}>: True {}
| ^^^^^^ non-trivial anonymous constants must not depend on the parameter `T`
|
= help: it is currently only allowed to use either `T` or `{ T }` as generic constants
= note: type parameters are currently not permitted in anonymous constants

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

error[E0119]: conflicting implementations of trait `MyTrait`:
--> $DIR/issue-64494.rs:19:1
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/const-generics/issues/issue-67739.min.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error: generic parameters must not be used inside of non trivial constant values
LL | [0u8; mem::size_of::<Self::Associated>()];
| ^^^^^^^^^^^^^^^^ non-trivial anonymous constants must not depend on the parameter `Self`
|
= help: it is currently only allowed to use either `Self` or `{ Self }` as generic constants
= note: type parameters are currently not permitted in anonymous constants

error: aborting due to previous error

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error: constant expression depends on a generic parameter
--> $DIR/issue-76701-ty-param-in-const.rs:6:21
|
LL | fn ty_param<T>() -> [u8; std::mem::size_of::<T>()] {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this may fail depending on what value the parameter takes

error: constant expression depends on a generic parameter
--> $DIR/issue-76701-ty-param-in-const.rs:12:37
|
LL | fn const_param<const N: usize>() -> [u8; N + 1] {
| ^^^^^^^^^^^
|
= note: this may fail depending on what value the parameter takes

error: aborting due to 2 previous errors

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error: generic parameters must not be used inside of non trivial constant values
--> $DIR/issue-76701-ty-param-in-const.rs:6:46
|
LL | fn ty_param<T>() -> [u8; std::mem::size_of::<T>()] {
| ^ non-trivial anonymous constants must not depend on the parameter `T`
|
= note: type parameters are currently not permitted in anonymous constants

error: generic parameters must not be used inside of non trivial constant values
--> $DIR/issue-76701-ty-param-in-const.rs:12:42
|
LL | fn const_param<const N: usize>() -> [u8; N + 1] {
| ^ non-trivial anonymous constants must not depend on the parameter `N`
|
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants

error: aborting due to 2 previous errors

18 changes: 18 additions & 0 deletions src/test/ui/const-generics/issues/issue-76701-ty-param-in-const.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// revisions: full min
#![cfg_attr(full, feature(const_generics))]
#![cfg_attr(full, allow(incomplete_features))]
#![cfg_attr(min, feature(min_const_generics))]

fn ty_param<T>() -> [u8; std::mem::size_of::<T>()] {
//[full]~^ ERROR constant expression depends on a generic parameter
//[min]~^^ ERROR generic parameters must not be used inside of non trivial constant values
todo!()
}

fn const_param<const N: usize>() -> [u8; N + 1] {
//[full]~^ ERROR constant expression depends on a generic parameter
//[min]~^^ ERROR generic parameters must not be used inside of non trivial constant values
todo!()
}

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

error: generic `Self` types are currently not permitted in anonymous constants
--> $DIR/self-ty-in-const-1.rs:14:41
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ error: generic parameters must not be used inside of non trivial constant values
LL | struct Foo<T, U = [u8; std::mem::size_of::<T>()]>(T, U);
| ^ non-trivial anonymous constants must not depend on the parameter `T`
|
= help: it is currently only allowed to use either `T` or `{ T }` as generic constants
= note: type parameters are currently not permitted in anonymous constants

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

0 comments on commit a63f8c1

Please sign in to comment.