diff options
Diffstat (limited to 'tests/ui/const-generics/early')
13 files changed, 390 insertions, 0 deletions
diff --git a/tests/ui/const-generics/early/closing-args-token.rs b/tests/ui/const-generics/early/closing-args-token.rs new file mode 100644 index 000000000..cb4d6299e --- /dev/null +++ b/tests/ui/const-generics/early/closing-args-token.rs @@ -0,0 +1,26 @@ +struct S<const X: u32>; +struct T<const X: bool>; + +fn bad_args_1() { + S::<5 + 2 >> 7>; + //~^ ERROR expressions must be enclosed in braces to be used as const generic arguments + //~| ERROR comparison operators cannot be chained +} + +fn bad_args_2() { + S::<{ 5 + 2 } >> 7>; + //~^ ERROR comparison operators cannot be chained +} + +fn bad_args_3() { + T::<0 >= 3>; + //~^ ERROR expected expression, found `;` +} + +fn bad_args_4() { + let mut x = 0; + T::<x >>= 2 > 0>; + //~^ ERROR comparison operators cannot be chained +} + +fn main() {} diff --git a/tests/ui/const-generics/early/closing-args-token.stderr b/tests/ui/const-generics/early/closing-args-token.stderr new file mode 100644 index 000000000..58fff3a85 --- /dev/null +++ b/tests/ui/const-generics/early/closing-args-token.stderr @@ -0,0 +1,52 @@ +error: expressions must be enclosed in braces to be used as const generic arguments + --> $DIR/closing-args-token.rs:5:9 + | +LL | S::<5 + 2 >> 7>; + | ^^^^^ + | +help: enclose the `const` expression in braces + | +LL | S::<{ 5 + 2 } >> 7>; + | + + + +error: comparison operators cannot be chained + --> $DIR/closing-args-token.rs:5:16 + | +LL | S::<5 + 2 >> 7>; + | ^ ^ + | +help: split the comparison into two + | +LL | S::<5 + 2 >> 7 && 7>; + | ++++ + +error: comparison operators cannot be chained + --> $DIR/closing-args-token.rs:11:20 + | +LL | S::<{ 5 + 2 } >> 7>; + | ^ ^ + | +help: split the comparison into two + | +LL | S::<{ 5 + 2 } >> 7 && 7>; + | ++++ + +error: expected expression, found `;` + --> $DIR/closing-args-token.rs:16:16 + | +LL | T::<0 >= 3>; + | ^ expected expression + +error: comparison operators cannot be chained + --> $DIR/closing-args-token.rs:22:12 + | +LL | T::<x >>= 2 > 0>; + | ^^ ^ + | +help: split the comparison into two + | +LL | T::<x >>= 2 && 2 > 0>; + | ++++ + +error: aborting due to 5 previous errors + diff --git a/tests/ui/const-generics/early/const-expression-parameter.rs b/tests/ui/const-generics/early/const-expression-parameter.rs new file mode 100644 index 000000000..4cf7ba869 --- /dev/null +++ b/tests/ui/const-generics/early/const-expression-parameter.rs @@ -0,0 +1,19 @@ +fn i32_identity<const X: i32>() -> i32 { + 5 +} + +fn foo_a() { + i32_identity::<-1>(); // ok +} + +fn foo_b() { + i32_identity::<1 + 2>(); //~ ERROR expressions must be enclosed in braces +} + +fn foo_c() { + i32_identity::< -1 >(); // ok +} + +fn main() { + i32_identity::<5>(); // ok +} diff --git a/tests/ui/const-generics/early/const-expression-parameter.stderr b/tests/ui/const-generics/early/const-expression-parameter.stderr new file mode 100644 index 000000000..4ce1be25e --- /dev/null +++ b/tests/ui/const-generics/early/const-expression-parameter.stderr @@ -0,0 +1,13 @@ +error: expressions must be enclosed in braces to be used as const generic arguments + --> $DIR/const-expression-parameter.rs:10:20 + | +LL | i32_identity::<1 + 2>(); + | ^^^^^ + | +help: enclose the `const` expression in braces + | +LL | i32_identity::<{ 1 + 2 }>(); + | + + + +error: aborting due to previous error + diff --git a/tests/ui/const-generics/early/const-param-from-outer-fn.rs b/tests/ui/const-generics/early/const-param-from-outer-fn.rs new file mode 100644 index 000000000..c3b418ee3 --- /dev/null +++ b/tests/ui/const-generics/early/const-param-from-outer-fn.rs @@ -0,0 +1,7 @@ +fn foo<const X: u32>() { + fn bar() -> u32 { + X //~ ERROR can't use generic parameters from outer function + } +} + +fn main() {} diff --git a/tests/ui/const-generics/early/const-param-from-outer-fn.stderr b/tests/ui/const-generics/early/const-param-from-outer-fn.stderr new file mode 100644 index 000000000..e3bf38b70 --- /dev/null +++ b/tests/ui/const-generics/early/const-param-from-outer-fn.stderr @@ -0,0 +1,13 @@ +error[E0401]: can't use generic parameters from outer function + --> $DIR/const-param-from-outer-fn.rs:3:9 + | +LL | fn foo<const X: u32>() { + | - const parameter from outer function +LL | fn bar() -> u32 { + | - help: try using a local generic parameter instead: `<X>` +LL | X + | ^ use of generic parameter from outer function + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0401`. diff --git a/tests/ui/const-generics/early/const-param-hygiene.rs b/tests/ui/const-generics/early/const-param-hygiene.rs new file mode 100644 index 000000000..fd4e5b409 --- /dev/null +++ b/tests/ui/const-generics/early/const-param-hygiene.rs @@ -0,0 +1,17 @@ +// run-pass + +macro_rules! bar { + ($($t:tt)*) => { impl<const N: usize> $($t)* }; +} + +macro_rules! baz { + ($t:tt) => { fn test<const M: usize>(&self) -> usize { $t } }; +} + +struct Foo<const N: usize>; + +bar!(Foo<N> { baz!{ M } }); + +fn main() { + assert_eq!(Foo::<7>.test::<3>(), 3); +} diff --git a/tests/ui/const-generics/early/const-param-shadowing.rs b/tests/ui/const-generics/early/const-param-shadowing.rs new file mode 100644 index 000000000..ddd15dbc4 --- /dev/null +++ b/tests/ui/const-generics/early/const-param-shadowing.rs @@ -0,0 +1,7 @@ +type N = u32; +struct Foo<const M: usize>; +fn test<const N: usize>() -> Foo<N> { //~ ERROR type provided when + Foo +} + +fn main() {} diff --git a/tests/ui/const-generics/early/const-param-shadowing.stderr b/tests/ui/const-generics/early/const-param-shadowing.stderr new file mode 100644 index 000000000..625338bd9 --- /dev/null +++ b/tests/ui/const-generics/early/const-param-shadowing.stderr @@ -0,0 +1,14 @@ +error[E0747]: type provided when a constant was expected + --> $DIR/const-param-shadowing.rs:3:34 + | +LL | fn test<const N: usize>() -> Foo<N> { + | ^ + | +help: if this generic argument was intended as a const parameter, surround it with braces + | +LL | fn test<const N: usize>() -> Foo<{ N }> { + | + + + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0747`. diff --git a/tests/ui/const-generics/early/invalid-const-arguments.rs b/tests/ui/const-generics/early/invalid-const-arguments.rs new file mode 100644 index 000000000..6619c9758 --- /dev/null +++ b/tests/ui/const-generics/early/invalid-const-arguments.rs @@ -0,0 +1,16 @@ +#![crate_type="lib"] + +struct A<const N: u8>; +trait Foo {} +impl Foo for A<N> {} +//~^ ERROR cannot find type +//~| unresolved item provided when a constant + +struct B<const N: u8>; +impl<N> Foo for B<N> {} +//~^ ERROR type provided when a constant + +struct C<const C: u8, const N: u8>; +impl<const N: u8> Foo for C<N, T> {} +//~^ ERROR cannot find type +//~| unresolved item provided when a constant diff --git a/tests/ui/const-generics/early/invalid-const-arguments.stderr b/tests/ui/const-generics/early/invalid-const-arguments.stderr new file mode 100644 index 000000000..b46e7e24f --- /dev/null +++ b/tests/ui/const-generics/early/invalid-const-arguments.stderr @@ -0,0 +1,73 @@ +error[E0412]: cannot find type `N` in this scope + --> $DIR/invalid-const-arguments.rs:5:16 + | +LL | struct A<const N: u8>; + | ---------------------- similarly named struct `A` defined here +LL | trait Foo {} +LL | impl Foo for A<N> {} + | ^ + | +help: a struct with a similar name exists + | +LL | impl Foo for A<A> {} + | ~ +help: you might be missing a type parameter + | +LL | impl<N> Foo for A<N> {} + | +++ + +error[E0412]: cannot find type `T` in this scope + --> $DIR/invalid-const-arguments.rs:14:32 + | +LL | struct A<const N: u8>; + | ---------------------- similarly named struct `A` defined here +... +LL | impl<const N: u8> Foo for C<N, T> {} + | ^ + | +help: a struct with a similar name exists + | +LL | impl<const N: u8> Foo for C<N, A> {} + | ~ +help: you might be missing a type parameter + | +LL | impl<const N: u8, T> Foo for C<N, T> {} + | +++ + +error[E0747]: unresolved item provided when a constant was expected + --> $DIR/invalid-const-arguments.rs:5:16 + | +LL | impl Foo for A<N> {} + | ^ + | +help: if this generic argument was intended as a const parameter, surround it with braces + | +LL | impl Foo for A<{ N }> {} + | + + + +error[E0747]: type provided when a constant was expected + --> $DIR/invalid-const-arguments.rs:10:19 + | +LL | impl<N> Foo for B<N> {} + | ^ + | +help: consider changing this type parameter to be a `const` generic + | +LL | impl<const N: u8> Foo for B<N> {} + | ~~~~~~~~~~~ + +error[E0747]: unresolved item provided when a constant was expected + --> $DIR/invalid-const-arguments.rs:14:32 + | +LL | impl<const N: u8> Foo for C<N, T> {} + | ^ + | +help: if this generic argument was intended as a const parameter, surround it with braces + | +LL | impl<const N: u8> Foo for C<N, { T }> {} + | + + + +error: aborting due to 5 previous errors + +Some errors have detailed explanations: E0412, E0747. +For more information about an error, try `rustc --explain E0412`. diff --git a/tests/ui/const-generics/early/macro_rules-braces.rs b/tests/ui/const-generics/early/macro_rules-braces.rs new file mode 100644 index 000000000..0ae914635 --- /dev/null +++ b/tests/ui/const-generics/early/macro_rules-braces.rs @@ -0,0 +1,73 @@ +mod m { + pub const P: usize = 0; +} + +const Q: usize = 0; + +fn test<const N: usize>() { + struct Foo<const M: usize>; + macro_rules! foo { + ($x:expr) => { + [u8; $x] + } + } + macro_rules! bar { + ($x:expr) => { + [u8; { $x }] + } + } + macro_rules! baz { + ( $x:expr) => { + Foo<$x> + } + } + macro_rules! biz { + ($x:expr) => { + Foo<{ $x }> + }; + } + + let _: foo!(N); + let _: foo!({ N }); + let _: foo!({{ N }}); //~ ERROR generic parameters may not + let _: foo!(Q); + let _: foo!(m::P); + let _: bar!(N); + let _: bar!({ N }); //~ ERROR generic parameters may not + let _: bar!(Q); + let _: bar!(m::P); + let _: baz!(N); + let _: baz!({ N }); + let _: baz!({{ N }}); //~ ERROR generic parameters may not + let _: baz!(Q); + let _: baz!({ m::P }); + let _: baz!(m::P); //~ ERROR expressions must be enclosed in braces + let _: biz!(N); + let _: biz!({ N }); //~ ERROR generic parameters may not + let _: biz!(Q); + let _: biz!(m::P); + let _: foo!(3); + let _: foo!({ 3 }); + let _: foo!({{ 3 }}); + let _: bar!(3); + let _: bar!({ 3 }); + let _: baz!(3); + let _: baz!({ 3 }); + let _: baz!({{ 3 }}); + let _: biz!(3); + let _: biz!({ 3 }); + let _: foo!(10 + 7); + let _: foo!({ 10 + 7 }); + let _: foo!({{ 10 + 7 }}); + let _: bar!(10 + 7); + let _: bar!({ 10 + 7 }); + let _: baz!(10 + 7); //~ ERROR expressions must be enclosed in braces + let _: baz!({ 10 + 7 }); + let _: baz!({{ 10 + 7 }}); + let _: biz!(10 + 7); + let _: biz!({ 10 + 7 }); +} + +fn main() { + test::<3>(); +} diff --git a/tests/ui/const-generics/early/macro_rules-braces.stderr b/tests/ui/const-generics/early/macro_rules-braces.stderr new file mode 100644 index 000000000..49382dbf0 --- /dev/null +++ b/tests/ui/const-generics/early/macro_rules-braces.stderr @@ -0,0 +1,60 @@ +error: expressions must be enclosed in braces to be used as const generic arguments + --> $DIR/macro_rules-braces.rs:44:17 + | +LL | let _: baz!(m::P); + | ^^^^ + | +help: enclose the `const` expression in braces + | +LL | let _: baz!({ m::P }); + | + + + +error: expressions must be enclosed in braces to be used as const generic arguments + --> $DIR/macro_rules-braces.rs:64:17 + | +LL | let _: baz!(10 + 7); + | ^^^^^^ + | +help: enclose the `const` expression in braces + | +LL | let _: baz!({ 10 + 7 }); + | + + + +error: generic parameters may not be used in const operations + --> $DIR/macro_rules-braces.rs:32:20 + | +LL | let _: foo!({{ N }}); + | ^ cannot perform const operation using `N` + | + = help: const parameters may only be used as standalone arguments, i.e. `N` + = help: use `#![feature(generic_const_exprs)]` to allow generic const expressions + +error: generic parameters may not be used in const operations + --> $DIR/macro_rules-braces.rs:36:19 + | +LL | let _: bar!({ N }); + | ^ cannot perform const operation using `N` + | + = help: const parameters may only be used as standalone arguments, i.e. `N` + = help: use `#![feature(generic_const_exprs)]` to allow generic const expressions + +error: generic parameters may not be used in const operations + --> $DIR/macro_rules-braces.rs:41:20 + | +LL | let _: baz!({{ N }}); + | ^ cannot perform const operation using `N` + | + = help: const parameters may only be used as standalone arguments, i.e. `N` + = help: use `#![feature(generic_const_exprs)]` to allow generic const expressions + +error: generic parameters may not be used in const operations + --> $DIR/macro_rules-braces.rs:46:19 + | +LL | let _: biz!({ N }); + | ^ cannot perform const operation using `N` + | + = help: const parameters may only be used as standalone arguments, i.e. `N` + = help: use `#![feature(generic_const_exprs)]` to allow generic const expressions + +error: aborting due to 6 previous errors + |