summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorJana Dönszelmann <jonathan@donsz.nl>2025-06-21 15:32:06 +0200
committerGitHub <noreply@github.com>2025-06-21 15:32:06 +0200
commitc693bc268acd4826faa2d1fa7908aaafc36a7e02 (patch)
treea2878d9eb8695942e89b47c8e9fdb76ce59e5edc /tests
parent6ed5d48cecd90072dd25c52d102a3d76f9afc5bb (diff)
parent044c99df78da9464e2ad0a90494af1e584123ab4 (diff)
downloadrust-c693bc268acd4826faa2d1fa7908aaafc36a7e02.tar.gz
rust-c693bc268acd4826faa2d1fa7908aaafc36a7e02.zip
Rollup merge of #142698 - tgross35:concat-byte-cstr-diag, r=petrochenkov
Improve diagnostics for `concat_bytes!` with C string  literals

Use the same error as other invalid types for `concat_bytes!`, rather
than using `ConcatCStrLit` from `concat!`. Also add more information
with a note about why this doesn't work, and a suggestion to use a
null-terminated byte string instead.
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/macros/concat-bytes-error.rs25
-rw-r--r--tests/ui/macros/concat-bytes-error.stderr99
2 files changed, 95 insertions, 29 deletions
diff --git a/tests/ui/macros/concat-bytes-error.rs b/tests/ui/macros/concat-bytes-error.rs
index db5d3cab0bd..8130fc54d8d 100644
--- a/tests/ui/macros/concat-bytes-error.rs
+++ b/tests/ui/macros/concat-bytes-error.rs
@@ -1,20 +1,44 @@
+//@ edition: 2021
+// 2021 edition for C string literals
+
 #![feature(concat_bytes)]
 
 fn main() {
+    // Identifiers
     concat_bytes!(pie); //~ ERROR expected a byte literal
     concat_bytes!(pie, pie); //~ ERROR expected a byte literal
+
+    // String literals
     concat_bytes!("tnrsi", "tnri"); //~ ERROR cannot concatenate string literals
+    //~^ SUGGESTION b"tnrsi"
+    concat_bytes!(r"tnrsi", r"tnri"); //~ ERROR cannot concatenate string literals
+    //~^ SUGGESTION br"tnrsi"
+    concat_bytes!(r#"tnrsi"#, r###"tnri"###); //~ ERROR cannot concatenate string literals
+    //~^ SUGGESTION br#"tnrsi"#
+    concat_bytes!(c"tnrsi", c"tnri"); //~ ERROR cannot concatenate C string literals
+    //~^ SUGGESTION b"tnrsi\0"
+    concat_bytes!(cr"tnrsi", cr"tnri"); //~ ERROR cannot concatenate C string literals
+    concat_bytes!(cr#"tnrsi"#, cr###"tnri"###); //~ ERROR cannot concatenate C string literals
+
+    // Other literals
     concat_bytes!(2.8); //~ ERROR cannot concatenate float literals
     concat_bytes!(300); //~ ERROR cannot concatenate numeric literals
+    //~^ SUGGESTION [300]
     concat_bytes!('a'); //~ ERROR cannot concatenate character literals
+    //~^ SUGGESTION b'a'
     concat_bytes!(true, false); //~ ERROR cannot concatenate boolean literals
     concat_bytes!(42, b"va", b'l'); //~ ERROR cannot concatenate numeric literals
+    //~^ SUGGESTION [42]
     concat_bytes!(42, b"va", b'l', [1, 2]); //~ ERROR cannot concatenate numeric literals
+    //~^ SUGGESTION [42]
+
+    // Nested items
     concat_bytes!([
         "hi", //~ ERROR cannot concatenate string literals
     ]);
     concat_bytes!([
         'a', //~ ERROR cannot concatenate character literals
+        //~^ SUGGESTION b'a'
     ]);
     concat_bytes!([
         true, //~ ERROR cannot concatenate boolean literals
@@ -38,6 +62,7 @@ fn main() {
         [5, 6, 7], //~ ERROR cannot concatenate doubly nested array
     ]);
     concat_bytes!(5u16); //~ ERROR cannot concatenate numeric literals
+    //~^ SUGGESTION [5u16]
     concat_bytes!([5u16]); //~ ERROR numeric literal is not a `u8`
     concat_bytes!([3; ()]); //~ ERROR repeat count is not a positive number
     concat_bytes!([3; -2]); //~ ERROR repeat count is not a positive number
diff --git a/tests/ui/macros/concat-bytes-error.stderr b/tests/ui/macros/concat-bytes-error.stderr
index 3f2c64922e3..447d7a663fd 100644
--- a/tests/ui/macros/concat-bytes-error.stderr
+++ b/tests/ui/macros/concat-bytes-error.stderr
@@ -1,5 +1,5 @@
 error: expected a byte literal
-  --> $DIR/concat-bytes-error.rs:4:19
+  --> $DIR/concat-bytes-error.rs:8:19
    |
 LL |     concat_bytes!(pie);
    |                   ^^^
@@ -7,7 +7,7 @@ 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
+  --> $DIR/concat-bytes-error.rs:9:19
    |
 LL |     concat_bytes!(pie, pie);
    |                   ^^^  ^^^
@@ -15,85 +15,126 @@ 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
+  --> $DIR/concat-bytes-error.rs:12:19
    |
 LL |     concat_bytes!("tnrsi", "tnri");
    |                   ^^^^^^^ help: try using a byte string: `b"tnrsi"`
 
+error: cannot concatenate string literals
+  --> $DIR/concat-bytes-error.rs:14:19
+   |
+LL |     concat_bytes!(r"tnrsi", r"tnri");
+   |                   ^^^^^^^^ help: try using a byte string: `br"tnrsi"`
+
+error: cannot concatenate string literals
+  --> $DIR/concat-bytes-error.rs:16:19
+   |
+LL |     concat_bytes!(r#"tnrsi"#, r###"tnri"###);
+   |                   ^^^^^^^^^^ help: try using a byte string: `br#"tnrsi"#`
+
+error: cannot concatenate C string literals
+  --> $DIR/concat-bytes-error.rs:18:19
+   |
+LL |     concat_bytes!(c"tnrsi", c"tnri");
+   |                   ^^^^^^^^ help: try using a null-terminated byte string: `b"tnrsi\0"`
+   |
+note: concatenating C strings is ambiguous about including the '\0'
+  --> $DIR/concat-bytes-error.rs:18:19
+   |
+LL |     concat_bytes!(c"tnrsi", c"tnri");
+   |                   ^^^^^^^^
+   = note: concatenating C strings is ambiguous about including the '\0'
+
+error: cannot concatenate C string literals
+  --> $DIR/concat-bytes-error.rs:20:19
+   |
+LL |     concat_bytes!(cr"tnrsi", cr"tnri");
+   |                   ^^^^^^^^^
+   |
+   = note: concatenating C strings is ambiguous about including the '\0'
+
+error: cannot concatenate C string literals
+  --> $DIR/concat-bytes-error.rs:21:19
+   |
+LL |     concat_bytes!(cr#"tnrsi"#, cr###"tnri"###);
+   |                   ^^^^^^^^^^^
+   |
+   = note: concatenating C strings is ambiguous about including the '\0'
+
 error: cannot concatenate float literals
-  --> $DIR/concat-bytes-error.rs:7:19
+  --> $DIR/concat-bytes-error.rs:24:19
    |
 LL |     concat_bytes!(2.8);
    |                   ^^^
 
 error: cannot concatenate numeric literals
-  --> $DIR/concat-bytes-error.rs:8:19
+  --> $DIR/concat-bytes-error.rs:25: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
+  --> $DIR/concat-bytes-error.rs:27: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
+  --> $DIR/concat-bytes-error.rs:29:19
    |
 LL |     concat_bytes!(true, false);
    |                   ^^^^
 
 error: cannot concatenate numeric literals
-  --> $DIR/concat-bytes-error.rs:11:19
+  --> $DIR/concat-bytes-error.rs:30: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
+  --> $DIR/concat-bytes-error.rs:32: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
+  --> $DIR/concat-bytes-error.rs:37:9
    |
 LL |         "hi",
    |         ^^^^
 
 error: cannot concatenate character literals
-  --> $DIR/concat-bytes-error.rs:17:9
+  --> $DIR/concat-bytes-error.rs:40:9
    |
 LL |         'a',
    |         ^^^ help: try using a byte character: `b'a'`
 
 error: cannot concatenate boolean literals
-  --> $DIR/concat-bytes-error.rs:20:9
+  --> $DIR/concat-bytes-error.rs:44:9
    |
 LL |         true,
    |         ^^^^
 
 error: cannot concatenate boolean literals
-  --> $DIR/concat-bytes-error.rs:23:9
+  --> $DIR/concat-bytes-error.rs:47:9
    |
 LL |         false,
    |         ^^^^^
 
 error: cannot concatenate float literals
-  --> $DIR/concat-bytes-error.rs:26:9
+  --> $DIR/concat-bytes-error.rs:50:9
    |
 LL |         2.6,
    |         ^^^
 
 error: numeric literal is out of bounds
-  --> $DIR/concat-bytes-error.rs:29:9
+  --> $DIR/concat-bytes-error.rs:53:9
    |
 LL |         265,
    |         ^^^
 
 error: expected a byte literal
-  --> $DIR/concat-bytes-error.rs:32:9
+  --> $DIR/concat-bytes-error.rs:56:9
    |
 LL |         -33,
    |         ^^^
@@ -101,7 +142,7 @@ 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
+  --> $DIR/concat-bytes-error.rs:59:9
    |
 LL |         b"hi!",
    |         ^^^^^^
@@ -110,43 +151,43 @@ LL |         b"hi!",
    = help: try flattening the array
 
 error: cannot concatenate doubly nested array
-  --> $DIR/concat-bytes-error.rs:38:9
+  --> $DIR/concat-bytes-error.rs:62:9
    |
 LL |         [5, 6, 7],
    |         ^^^^^^^^^
 
 error: cannot concatenate numeric literals
-  --> $DIR/concat-bytes-error.rs:40:19
+  --> $DIR/concat-bytes-error.rs:64: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
+  --> $DIR/concat-bytes-error.rs:66:20
    |
 LL |     concat_bytes!([5u16]);
    |                    ^^^^
 
 error: repeat count is not a positive number
-  --> $DIR/concat-bytes-error.rs:42:23
+  --> $DIR/concat-bytes-error.rs:67:23
    |
 LL |     concat_bytes!([3; ()]);
    |                       ^^
 
 error: repeat count is not a positive number
-  --> $DIR/concat-bytes-error.rs:43:23
+  --> $DIR/concat-bytes-error.rs:68:23
    |
 LL |     concat_bytes!([3; -2]);
    |                       ^^
 
 error: repeat count is not a positive number
-  --> $DIR/concat-bytes-error.rs:44:25
+  --> $DIR/concat-bytes-error.rs:69:25
    |
 LL |     concat_bytes!([pie; -2]);
    |                         ^^
 
 error: expected a byte literal
-  --> $DIR/concat-bytes-error.rs:45:20
+  --> $DIR/concat-bytes-error.rs:70:20
    |
 LL |     concat_bytes!([pie; 2]);
    |                    ^^^
@@ -154,28 +195,28 @@ LL |     concat_bytes!([pie; 2]);
    = note: only byte literals (like `b"foo"`, `b's'` and `[3, 4, 5]`) can be passed to `concat_bytes!()`
 
 error: cannot concatenate float literals
-  --> $DIR/concat-bytes-error.rs:46:20
+  --> $DIR/concat-bytes-error.rs:71:20
    |
 LL |     concat_bytes!([2.2; 0]);
    |                    ^^^
 
 error: repeat count is not a positive number
-  --> $DIR/concat-bytes-error.rs:47:25
+  --> $DIR/concat-bytes-error.rs:72:25
    |
 LL |     concat_bytes!([5.5; ()]);
    |                         ^^
 
 error: cannot concatenate doubly nested array
-  --> $DIR/concat-bytes-error.rs:48:20
+  --> $DIR/concat-bytes-error.rs:73:20
    |
 LL |     concat_bytes!([[1, 2, 3]; 3]);
    |                    ^^^^^^^^^
 
 error: cannot concatenate doubly nested array
-  --> $DIR/concat-bytes-error.rs:49:20
+  --> $DIR/concat-bytes-error.rs:74:20
    |
 LL |     concat_bytes!([[42; 2]; 3]);
    |                    ^^^^^^^
 
-error: aborting due to 28 previous errors
+error: aborting due to 33 previous errors