Add #[rustc_legacy_const_generics]

This commit is contained in:
Amanieu d'Antras 2021-02-23 15:12:28 +00:00
parent 9b471a3f5f
commit d87eec1bf6
11 changed files with 310 additions and 2 deletions

View file

@ -0,0 +1,6 @@
#![feature(rustc_attrs)]
#[rustc_legacy_const_generics(1)]
pub fn foo<const Y: usize>(x: usize, z: usize) -> [usize; 3] {
[x, Y, z]
}

View file

@ -0,0 +1,35 @@
#![feature(rustc_attrs)]
#[rustc_legacy_const_generics(0)] //~ ERROR index exceeds number of arguments
fn foo1() {}
#[rustc_legacy_const_generics(1)] //~ ERROR index exceeds number of arguments
fn foo2(_: u8) {}
#[rustc_legacy_const_generics(2)] //~ ERROR index exceeds number of arguments
fn foo3<const X: usize>(_: u8) {}
#[rustc_legacy_const_generics(a)] //~ ERROR arguments should be non-negative integers
fn foo4() {}
#[rustc_legacy_const_generics(1, a, 2, b)] //~ ERROR arguments should be non-negative integers
fn foo5(_: u8, _: u8, _: u8) {}
#[rustc_legacy_const_generics(0)] //~ ERROR attribute should be applied to a function
struct S;
#[rustc_legacy_const_generics(0usize)] //~ ERROR suffixed literals are not allowed in attributes
fn foo6(_: u8) {}
extern {
#[rustc_legacy_const_generics(1)] //~ ERROR index exceeds number of arguments
fn foo7(_: u8);
}
#[rustc_legacy_const_generics] //~ ERROR malformed `rustc_legacy_const_generics` attribute
fn bar1() {}
#[rustc_legacy_const_generics = 1] //~ ERROR malformed `rustc_legacy_const_generics` attribute
fn bar2() {}
fn main() {}

View file

@ -0,0 +1,66 @@
error: suffixed literals are not allowed in attributes
--> $DIR/invalid-rustc_legacy_const_generics-arguments.rs:21:31
|
LL | #[rustc_legacy_const_generics(0usize)]
| ^^^^^^
|
= help: instead of using a suffixed literal (`1u8`, `1.0f32`, etc.), use an unsuffixed version (`1`, `1.0`, etc.)
error: malformed `rustc_legacy_const_generics` attribute input
--> $DIR/invalid-rustc_legacy_const_generics-arguments.rs:29:1
|
LL | #[rustc_legacy_const_generics]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[rustc_legacy_const_generics(N)]`
error: malformed `rustc_legacy_const_generics` attribute input
--> $DIR/invalid-rustc_legacy_const_generics-arguments.rs:32:1
|
LL | #[rustc_legacy_const_generics = 1]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[rustc_legacy_const_generics(N)]`
error: index exceeds number of arguments
--> $DIR/invalid-rustc_legacy_const_generics-arguments.rs:3:31
|
LL | #[rustc_legacy_const_generics(0)]
| ^ there are only 0 arguments
error: index exceeds number of arguments
--> $DIR/invalid-rustc_legacy_const_generics-arguments.rs:6:31
|
LL | #[rustc_legacy_const_generics(1)]
| ^ there is only 1 argument
error: index exceeds number of arguments
--> $DIR/invalid-rustc_legacy_const_generics-arguments.rs:9:31
|
LL | #[rustc_legacy_const_generics(2)]
| ^ there are only 2 arguments
error: arguments should be non-negative integers
--> $DIR/invalid-rustc_legacy_const_generics-arguments.rs:12:31
|
LL | #[rustc_legacy_const_generics(a)]
| ^
error: arguments should be non-negative integers
--> $DIR/invalid-rustc_legacy_const_generics-arguments.rs:15:34
|
LL | #[rustc_legacy_const_generics(1, a, 2, b)]
| ^ ^
error: attribute should be applied to a function
--> $DIR/invalid-rustc_legacy_const_generics-arguments.rs:18:1
|
LL | #[rustc_legacy_const_generics(0)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | struct S;
| --------- not a function
error: index exceeds number of arguments
--> $DIR/invalid-rustc_legacy_const_generics-arguments.rs:25:35
|
LL | #[rustc_legacy_const_generics(1)]
| ^ there is only 1 argument
error: aborting due to 10 previous errors

View file

@ -0,0 +1,18 @@
// aux-build:legacy-const-generics.rs
// run-pass
#![feature(rustc_attrs)]
extern crate legacy_const_generics;
#[rustc_legacy_const_generics(1)]
pub fn bar<const Y: usize>(x: usize, z: usize) -> [usize; 3] {
[x, Y, z]
}
fn main() {
assert_eq!(legacy_const_generics::foo(0 + 0, 1 + 1, 2 + 2), [0, 2, 4]);
assert_eq!(legacy_const_generics::foo::<{1 + 1}>(0 + 0, 2 + 2), [0, 2, 4]);
// TODO: Only works cross-crate
//assert_eq!(bar(0, 1, 2), [0, 1, 2]);
}