Rollup merge of #87599 - Smittyvb:concat_bytes, r=Mark-Simulacrum

Implement concat_bytes!

This implements the unstable `concat_bytes!` macro, which has tracking issue #87555. It can be used like:
```rust
#![feature(concat_bytes)]

fn main() {
    assert_eq!(concat_bytes!(), &[]);
    assert_eq!(concat_bytes!(b'A', b"BC", [68, b'E', 70]), b"ABCDEF");
}
```
If strings or characters are used where byte strings or byte characters are required, it suggests adding a `b` prefix. If a number is used outside of an array it suggests arrayifying it. If a boolean is used it suggests replacing it with the numeric value of that number. Doubly nested arrays of bytes are disallowed.
This commit is contained in:
Matthias Krüger 2021-12-09 05:08:30 +01:00 committed by GitHub
commit 3fc5bd7abc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 421 additions and 0 deletions

View file

@ -0,0 +1,4 @@
fn main() {
let a = concat_bytes!(b'A', b"BC"); //~ ERROR use of unstable library feature 'concat_bytes'
assert_eq!(a, &[65, 66, 67]);
}

View file

@ -0,0 +1,12 @@
error[E0658]: use of unstable library feature 'concat_bytes'
--> $DIR/feature-gate-concat_bytes.rs:2:13
|
LL | let a = concat_bytes!(b'A', b"BC");
| ^^^^^^^^^^^^
|
= note: see issue #87555 <https://github.com/rust-lang/rust/issues/87555> for more information
= help: add `#![feature(concat_bytes)]` to the crate attributes to enable
error: aborting due to previous error
For more information about this error, try `rustc --explain E0658`.

View file

@ -0,0 +1,42 @@
#![feature(concat_bytes)]
fn main() {
concat_bytes!(pie); //~ ERROR expected a byte literal
concat_bytes!(pie, pie); //~ ERROR expected a byte literal
concat_bytes!("tnrsi", "tnri"); //~ ERROR cannot concatenate string literals
concat_bytes!(2.8); //~ ERROR cannot concatenate float literals
concat_bytes!(300); //~ ERROR cannot concatenate numeric literals
concat_bytes!('a'); //~ ERROR cannot concatenate character literals
concat_bytes!(true, false); //~ ERROR cannot concatenate boolean literals
concat_bytes!(42, b"va", b'l'); //~ ERROR cannot concatenate numeric literals
concat_bytes!(42, b"va", b'l', [1, 2]); //~ ERROR cannot concatenate numeric literals
concat_bytes!([
"hi", //~ ERROR cannot concatenate string literals
]);
concat_bytes!([
'a', //~ ERROR cannot concatenate character literals
]);
concat_bytes!([
true, //~ ERROR cannot concatenate boolean literals
]);
concat_bytes!([
false, //~ ERROR cannot concatenate boolean literals
]);
concat_bytes!([
2.6, //~ ERROR cannot concatenate float literals
]);
concat_bytes!([
265, //~ ERROR numeric literal is out of bounds
]);
concat_bytes!([
-33, //~ ERROR expected a byte literal
]);
concat_bytes!([
b"hi!", //~ ERROR cannot concatenate doubly nested array
]);
concat_bytes!([
[5, 6, 7], //~ ERROR cannot concatenate doubly nested array
]);
concat_bytes!(5u16); //~ ERROR cannot concatenate numeric literals
concat_bytes!([5u16]); //~ ERROR numeric literal is not a `u8`
}

View file

@ -0,0 +1,131 @@
error: expected a byte literal
--> $DIR/concat-bytes-error.rs:4:19
|
LL | concat_bytes!(pie);
| ^^^
|
= note: only byte literals (like `b"foo"`, `b's'`, and `[3, 4, 5]`) can be passed to `concat_bytes!()`
error: expected a byte literal
--> $DIR/concat-bytes-error.rs:5:19
|
LL | concat_bytes!(pie, pie);
| ^^^ ^^^
|
= note: only byte literals (like `b"foo"`, `b's'`, and `[3, 4, 5]`) can be passed to `concat_bytes!()`
error: cannot concatenate string literals
--> $DIR/concat-bytes-error.rs:6:19
|
LL | concat_bytes!("tnrsi", "tnri");
| ^^^^^^^ help: try using a byte string: `b"tnrsi"`
error: cannot concatenate float literals
--> $DIR/concat-bytes-error.rs:7:19
|
LL | concat_bytes!(2.8);
| ^^^
error: cannot concatenate numeric literals
--> $DIR/concat-bytes-error.rs:8:19
|
LL | concat_bytes!(300);
| ^^^ help: try wrapping the number in an array: `[300]`
error: cannot concatenate character literals
--> $DIR/concat-bytes-error.rs:9:19
|
LL | concat_bytes!('a');
| ^^^ help: try using a byte character: `b'a'`
error: cannot concatenate boolean literals
--> $DIR/concat-bytes-error.rs:10:19
|
LL | concat_bytes!(true, false);
| ^^^^
error: cannot concatenate numeric literals
--> $DIR/concat-bytes-error.rs:11:19
|
LL | concat_bytes!(42, b"va", b'l');
| ^^ help: try wrapping the number in an array: `[42]`
error: cannot concatenate numeric literals
--> $DIR/concat-bytes-error.rs:12:19
|
LL | concat_bytes!(42, b"va", b'l', [1, 2]);
| ^^ help: try wrapping the number in an array: `[42]`
error: cannot concatenate string literals
--> $DIR/concat-bytes-error.rs:14:9
|
LL | "hi",
| ^^^^
error: cannot concatenate character literals
--> $DIR/concat-bytes-error.rs:17:9
|
LL | 'a',
| ^^^ help: try using a byte character: `b'a'`
error: cannot concatenate boolean literals
--> $DIR/concat-bytes-error.rs:20:9
|
LL | true,
| ^^^^
error: cannot concatenate boolean literals
--> $DIR/concat-bytes-error.rs:23:9
|
LL | false,
| ^^^^^
error: cannot concatenate float literals
--> $DIR/concat-bytes-error.rs:26:9
|
LL | 2.6,
| ^^^
error: numeric literal is out of bounds
--> $DIR/concat-bytes-error.rs:29:9
|
LL | 265,
| ^^^
error: expected a byte literal
--> $DIR/concat-bytes-error.rs:32:9
|
LL | -33,
| ^^^
|
= note: only byte literals (like `b"foo"`, `b's'`, and `[3, 4, 5]`) can be passed to `concat_bytes!()`
error: cannot concatenate doubly nested array
--> $DIR/concat-bytes-error.rs:35:9
|
LL | b"hi!",
| ^^^^^^
|
= note: byte strings are treated as arrays of bytes
= help: try flattening the array
error: cannot concatenate doubly nested array
--> $DIR/concat-bytes-error.rs:38:9
|
LL | [5, 6, 7],
| ^^^^^^^^^
error: cannot concatenate numeric literals
--> $DIR/concat-bytes-error.rs:40:19
|
LL | concat_bytes!(5u16);
| ^^^^ help: try wrapping the number in an array: `[5u16]`
error: numeric literal is not a `u8`
--> $DIR/concat-bytes-error.rs:41:20
|
LL | concat_bytes!([5u16]);
| ^^^^
error: aborting due to 20 previous errors

View file

@ -0,0 +1,7 @@
// run-pass
#![feature(concat_bytes)]
fn main() {
assert_eq!(concat_bytes!(), &[]);
assert_eq!(concat_bytes!(b'A', b"BC", [68, b'E', 70]), b"ABCDEF");
}