Skip to content

Commit

Permalink
Rollup merge of rust-lang#77825 - ethanboxx:min_const_generics_diagno…
Browse files Browse the repository at this point in the history
…stic, r=lcnr

`min_const_generics` diagnostics improvements

As disscussed in [zulip/project-const-generics/non-trivial anonymous constant](https://rust-lang.zulipchat.com/#narrow/stream/260443-project-const-generics/topic/non-trivial.20anonymous.20constants).

This is my first PR on the compiler.

@lcnr is mentoring me on this PR.

Related to rust-lang#60551.
  • Loading branch information
JohnTitor authored Oct 13, 2020
2 parents f6d9e32 + 79351b1 commit 0a83cb9
Show file tree
Hide file tree
Showing 56 changed files with 185 additions and 192 deletions.
21 changes: 7 additions & 14 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,24 +469,17 @@ impl<'a> Resolver<'a> {
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",
);
err.span_label(
span,
&format!(
"non-trivial anonymous constants must not depend on the parameter `{}`",
name
),
"generic parameters may not be used in const operations",
);
err.span_label(span, &format!("cannot perform const operation using `{}`", name));

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

err
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ enum ResolutionError<'a> {
ParamInTyOfConstParam(Symbol),
/// constant values inside of type parameter defaults must not depend on generic parameters.
ParamInAnonConstInTyDefault(Symbol),
/// generic parameters must not be used inside of non-trivial constant values.
/// generic parameters must not be used inside const evaluations.
///
/// This error is only emitted when using `min_const_generics`.
ParamInNonTrivialAnonConst { name: Symbol, is_type: bool },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
error: generic parameters must not be used inside of non-trivial constant values
error: generic parameters may not be used in const operations
--> $DIR/array-size-in-generic-struct-param.rs:9:48
|
LL | struct ArithArrayLen<const N: usize>([u32; 0 + N]);
| ^ non-trivial anonymous constants must not depend on the parameter `N`
| ^ cannot perform const operation using `N`
|
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
= help: const parameters may only be used as standalone arguments, i.e. `N`

error: generic parameters must not be used inside of non-trivial constant values
error: generic parameters may not be used in const operations
--> $DIR/array-size-in-generic-struct-param.rs:20:15
|
LL | arr: [u8; CFG.arr_size],
| ^^^ non-trivial anonymous constants must not depend on the parameter `CFG`
| ^^^ cannot perform const operation using `CFG`
|
= help: it is currently only allowed to use either `CFG` or `{ CFG }` as generic constants
= help: const parameters may only be used as standalone arguments, i.e. `CFG`

error: `Config` is forbidden as the type of a const generic parameter
--> $DIR/array-size-in-generic-struct-param.rs:18:21
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#[allow(dead_code)]
struct ArithArrayLen<const N: usize>([u32; 0 + N]);
//[full]~^ ERROR constant expression depends on a generic parameter
//[min]~^^ ERROR generic parameters must not be used inside of non-trivial constant values
//[min]~^^ ERROR generic parameters may not be used in const operations

#[derive(PartialEq, Eq)]
struct Config {
Expand All @@ -19,7 +19,7 @@ struct B<const CFG: Config> {
//[min]~^ ERROR `Config` is forbidden
arr: [u8; CFG.arr_size],
//[full]~^ ERROR constant expression depends on a generic parameter
//[min]~^^ ERROR generic parameters must not be used inside of non-trivial
//[min]~^^ ERROR generic parameters may not be used in const operations
}

const C: Config = Config { arr_size: 5 };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error: generic parameters must not be used inside of non-trivial constant values
error: generic parameters may not be used in const operations
--> $DIR/const-argument-if-length.rs:19:24
|
LL | pad: [u8; is_zst::<T>()],
| ^ non-trivial anonymous constants must not depend on the parameter `T`
| ^ cannot perform const operation using `T`
|
= note: type parameters are currently not permitted in anonymous constants
= note: type parameters may not be used in const expressions

error[E0277]: the size for values of type `T` cannot be known at compilation time
--> $DIR/const-argument-if-length.rs:17:12
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/const-generics/const-argument-if-length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct AtLeastByte<T: ?Sized> {
value: T,
//~^ ERROR the size for values of type `T` cannot be known at compilation time
pad: [u8; is_zst::<T>()],
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
//[min]~^ ERROR generic parameters may not be used in const operations
//[full]~^^ ERROR evaluation of constant value failed
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error: generic parameters must not be used inside of non-trivial constant values
error: generic parameters may not be used in const operations
--> $DIR/feature-gate-const_evaluatable_checked.rs:6:33
|
LL | type Arr<const N: usize> = [u8; N - 1];
| ^ non-trivial anonymous constants must not depend on the parameter `N`
| ^ cannot perform const operation using `N`
|
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
= help: const parameters may only be used as standalone arguments, i.e. `N`

error: aborting due to previous error

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#![cfg_attr(min, feature(min_const_generics))]

type Arr<const N: usize> = [u8; N - 1];
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
//[min]~^ ERROR generic parameters may not be used in const operations

fn test<const N: usize>() -> Arr<N> where Arr<N>: Default {
//[full]~^ ERROR constant expression depends
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
error: generic parameters must not be used inside of non-trivial constant values
error: generic parameters may not be used in const operations
--> $DIR/simple.rs:8:53
|
LL | fn test<const N: usize>() -> [u8; N - 1] where [u8; N - 1]: Default {
| ^ non-trivial anonymous constants must not depend on the parameter `N`
| ^ cannot perform const operation using `N`
|
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
= help: const parameters may only be used as standalone arguments, i.e. `N`

error: generic parameters must not be used inside of non-trivial constant values
error: generic parameters may not be used in const operations
--> $DIR/simple.rs:8:35
|
LL | fn test<const N: usize>() -> [u8; N - 1] where [u8; N - 1]: Default {
| ^ non-trivial anonymous constants must not depend on the parameter `N`
| ^ cannot perform const operation using `N`
|
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
= help: const parameters may only be used as standalone arguments, i.e. `N`

error: aborting due to 2 previous errors

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error: generic parameters must not be used inside of non-trivial constant values
error: generic parameters may not be used in const operations
--> $DIR/simple_fail.rs:7:33
|
LL | type Arr<const N: usize> = [u8; N - 1];
| ^ non-trivial anonymous constants must not depend on the parameter `N`
| ^ cannot perform const operation using `N`
|
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
= help: const parameters may only be used as standalone arguments, i.e. `N`

error: aborting due to previous error

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#![allow(incomplete_features)]

type Arr<const N: usize> = [u8; N - 1]; //[full]~ ERROR evaluation of constant
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
//[min]~^ ERROR generic parameters may not be used in const operations

fn test<const N: usize>() -> Arr<N> where Arr<N>: Sized {
todo!()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
error: generic parameters must not be used inside of non-trivial constant values
error: generic parameters may not be used in const operations
--> $DIR/generic-function-call-in-array-length.rs:9:39
|
LL | fn bar<const N: usize>() -> [u32; foo(N)] {
| ^ non-trivial anonymous constants must not depend on the parameter `N`
| ^ cannot perform const operation using `N`
|
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
= help: const parameters may only be used as standalone arguments, i.e. `N`

error: generic parameters must not be used inside of non-trivial constant values
error: generic parameters may not be used in const operations
--> $DIR/generic-function-call-in-array-length.rs:12:13
|
LL | [0; foo(N)]
| ^ non-trivial anonymous constants must not depend on the parameter `N`
| ^ cannot perform const operation using `N`
|
= help: it is currently only allowed to use either `N` or `{ N }` as generic constants
= help: const parameters may only be used as standalone arguments, i.e. `N`

error: aborting due to 2 previous errors

Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
const fn foo(n: usize) -> usize { n * 2 }

fn bar<const N: usize>() -> [u32; foo(N)] {
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
//[min]~^ ERROR generic parameters may not be used in const operations
//[full]~^^ ERROR constant expression depends on a generic parameter
[0; foo(N)]
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
//[min]~^ ERROR generic parameters may not be used in const operations
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
error: generic parameters must not be used inside of non-trivial constant values
error: generic parameters may not be used in const operations
--> $DIR/generic-sum-in-array-length.rs:7:53
|
LL | fn foo<const A: usize, const B: usize>(bar: [usize; A + B]) {}
| ^ non-trivial anonymous constants must not depend on the parameter `A`
| ^ cannot perform const operation using `A`
|
= help: it is currently only allowed to use either `A` or `{ A }` as generic constants
= help: const parameters may only be used as standalone arguments, i.e. `A`

error: generic parameters must not be used inside of non-trivial constant values
error: generic parameters may not be used in const operations
--> $DIR/generic-sum-in-array-length.rs:7:57
|
LL | fn foo<const A: usize, const B: usize>(bar: [usize; A + B]) {}
| ^ non-trivial anonymous constants must not depend on the parameter `B`
| ^ cannot perform const operation using `B`
|
= help: it is currently only allowed to use either `B` or `{ B }` as generic constants
= help: const parameters may only be used as standalone arguments, i.e. `B`

error: aborting due to 2 previous errors

4 changes: 2 additions & 2 deletions src/test/ui/const-generics/generic-sum-in-array-length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#![cfg_attr(min, feature(min_const_generics))]

fn foo<const A: usize, const B: usize>(bar: [usize; A + B]) {}
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
//[min]~| ERROR generic parameters must not be used inside of non-trivial constant values
//[min]~^ ERROR generic parameters may not be used in const operations
//[min]~| ERROR generic parameters may not be used in const operations
//[full]~^^^ ERROR constant expression depends on a generic parameter

fn main() {}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error: generic parameters must not be used inside of non-trivial constant values
error: generic parameters may not be used in const operations
--> $DIR/intrinsics-type_name-as-const-argument.rs:15:44
|
LL | T: Trait<{std::intrinsics::type_name::<T>()}>
| ^ non-trivial anonymous constants must not depend on the parameter `T`
| ^ cannot perform const operation using `T`
|
= note: type parameters are currently not permitted in anonymous constants
= note: type parameters may not be used in const expressions

error: `&'static str` is forbidden as the type of a const generic parameter
--> $DIR/intrinsics-type_name-as-const-argument.rs:10:22
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trait Trait<const S: &'static str> {}
struct Bug<T>
where
T: Trait<{std::intrinsics::type_name::<T>()}>
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
//[min]~^ ERROR generic parameters may not be used in const operations
//[full]~^^ ERROR constant expression depends on a generic parameter
{
t: T
Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/const-generics/issue-61522-array-len-succ.min.stderr
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
error: generic parameters must not be used inside of non-trivial constant values
error: generic parameters may not be used in const operations
--> $DIR/issue-61522-array-len-succ.rs:7:45
|
LL | pub struct MyArray<const COUNT: usize>([u8; COUNT + 1]);
| ^^^^^ non-trivial anonymous constants must not depend on the parameter `COUNT`
| ^^^^^ cannot perform const operation using `COUNT`
|
= help: it is currently only allowed to use either `COUNT` or `{ COUNT }` as generic constants
= help: const parameters may only be used as standalone arguments, i.e. `COUNT`

error: generic parameters must not be used inside of non-trivial constant values
error: generic parameters may not be used in const operations
--> $DIR/issue-61522-array-len-succ.rs:12:30
|
LL | fn inner(&self) -> &[u8; COUNT + 1] {
| ^^^^^ non-trivial anonymous constants must not depend on the parameter `COUNT`
| ^^^^^ cannot perform const operation using `COUNT`
|
= help: it is currently only allowed to use either `COUNT` or `{ COUNT }` as generic constants
= help: const parameters may only be used as standalone arguments, i.e. `COUNT`

error: aborting due to 2 previous errors

4 changes: 2 additions & 2 deletions src/test/ui/const-generics/issue-61522-array-len-succ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

pub struct MyArray<const COUNT: usize>([u8; COUNT + 1]);
//[full]~^ ERROR constant expression depends on a generic parameter
//[min]~^^ ERROR generic parameters must not be used
//[min]~^^ ERROR generic parameters may not be used

impl<const COUNT: usize> MyArray<COUNT> {
fn inner(&self) -> &[u8; COUNT + 1] {
//[full]~^ ERROR constant expression depends on a generic parameter
//[min]~^^ ERROR generic parameters must not be used
//[min]~^^ ERROR generic parameters may not be used
&self.0
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/const-generics/issue-67375.min.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error: generic parameters must not be used inside of non-trivial constant values
error: generic parameters may not be used in const operations
--> $DIR/issue-67375.rs:9:25
|
LL | inner: [(); { [|_: &T| {}; 0].len() }],
| ^ non-trivial anonymous constants must not depend on the parameter `T`
| ^ cannot perform const operation using `T`
|
= note: type parameters are currently not permitted in anonymous constants
= note: type parameters may not be used in const expressions

error[E0392]: parameter `T` is never used
--> $DIR/issue-67375.rs:7:12
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/const-generics/issue-67375.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
struct Bug<T> {
//~^ ERROR parameter `T` is never used
inner: [(); { [|_: &T| {}; 0].len() }],
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
//[min]~^ ERROR generic parameters may not be used in const operations
//[full]~^^ WARN cannot use constants which depend on generic parameters in types
//[full]~^^^ WARN this was previously accepted by the compiler
}
Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/const-generics/issue-67945-1.min.stderr
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
error: generic parameters must not be used inside of non-trivial constant values
error: generic parameters may not be used in const operations
--> $DIR/issue-67945-1.rs:14:16
|
LL | let x: S = MaybeUninit::uninit();
| ^ non-trivial anonymous constants must not depend on the parameter `S`
| ^ cannot perform const operation using `S`
|
= note: type parameters are currently not permitted in anonymous constants
= note: type parameters may not be used in const expressions

error: generic parameters must not be used inside of non-trivial constant values
error: generic parameters may not be used in const operations
--> $DIR/issue-67945-1.rs:17:45
|
LL | let b = &*(&x as *const _ as *const S);
| ^ non-trivial anonymous constants must not depend on the parameter `S`
| ^ cannot perform const operation using `S`
|
= note: type parameters are currently not permitted in anonymous constants
= note: type parameters may not be used in const expressions

error[E0392]: parameter `S` is never used
--> $DIR/issue-67945-1.rs:11:12
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/const-generics/issue-67945-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ struct Bug<S> {
//~^ ERROR parameter `S` is never used
A: [(); {
let x: S = MaybeUninit::uninit();
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
//[min]~^ ERROR generic parameters may not be used in const operations
//[full]~^^ ERROR mismatched types
let b = &*(&x as *const _ as *const S);
//[min]~^ ERROR generic parameters must not be used inside of non-trivial constant values
//[min]~^ ERROR generic parameters may not be used in const operations
0
}],
}
Expand Down
Loading

0 comments on commit 0a83cb9

Please sign in to comment.