about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-12-01 13:33:55 +0000
committerbors <bors@rust-lang.org>2023-12-01 13:33:55 +0000
commit3a760909fa8a5dbafcb89e409ab61768a206c8f7 (patch)
tree3ec982bc04093e64d373f001c6c5e7676ae0c066 /tests
parent071f8f610dbf9d288c0b3e37597351abe17b7aed (diff)
parente3a1555648f7e0471a4d1620e950644c2f725aa6 (diff)
downloadrust-3a760909fa8a5dbafcb89e409ab61768a206c8f7.tar.gz
rust-3a760909fa8a5dbafcb89e409ab61768a206c8f7.zip
Auto merge of #117472 - jmillikin:stable-c-str-literals, r=Nilstrieb
Stabilize C string literals

RFC: https://rust-lang.github.io/rfcs/3348-c-str-literal.html

Tracking issue: https://github.com/rust-lang/rust/issues/105723

Documentation PR (reference manual): https://github.com/rust-lang/reference/pull/1423

# Stabilization report

Stabilizes C string and raw C string literals (`c"..."` and `cr#"..."#`), which are expressions of type [`&CStr`](https://doc.rust-lang.org/stable/core/ffi/struct.CStr.html). Both new literals require Rust edition 2021 or later.

```rust
const HELLO: &core::ffi::CStr = c"Hello, world!";
```

C strings may contain any byte other than `NUL` (`b'\x00'`), and their in-memory representation is guaranteed to end with `NUL`.

## Implementation

Originally implemented by PR https://github.com/rust-lang/rust/pull/108801, which was reverted due to unintentional changes to lexer behavior in Rust editions < 2021.

The current implementation landed in PR https://github.com/rust-lang/rust/pull/113476, which restricts C string literals to Rust edition >= 2021.

## Resolutions to open questions from the RFC

* Adding C character literals (`c'.'`) of type `c_char` is not part of this feature.
  * Support for `c"..."` literals does not prevent `c'.'` literals from being added in the future.
* C string literals should not be blocked on making `&CStr` a thin pointer.
  * It's possible to declare constant expressions of type `&'static CStr` in stable Rust (as of v1.59), so C string literals are not adding additional coupling on the internal representation of `CStr`.
* The unstable `concat_bytes!` macro should not accept `c"..."` literals.
  * C strings have two equally valid `&[u8]` representations (with or without terminal `NUL`), so allowing them to be used in `concat_bytes!` would be ambiguous.
* Adding a type to represent C strings containing valid UTF-8 is not part of this feature.
  * Support for a hypothetical `&Utf8CStr` may be explored in the future, should such a type be added to Rust.
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/needless_raw_string.fixed1
-rw-r--r--tests/ui/needless_raw_string.rs1
-rw-r--r--tests/ui/needless_raw_string.stderr14
-rw-r--r--tests/ui/needless_raw_string_hashes.fixed1
-rw-r--r--tests/ui/needless_raw_string_hashes.rs1
-rw-r--r--tests/ui/needless_raw_string_hashes.stderr30
6 files changed, 22 insertions, 26 deletions
diff --git a/tests/ui/needless_raw_string.fixed b/tests/ui/needless_raw_string.fixed
index 4ea711fd67a..1a9c601c462 100644
--- a/tests/ui/needless_raw_string.fixed
+++ b/tests/ui/needless_raw_string.fixed
@@ -1,6 +1,5 @@
 #![allow(clippy::needless_raw_string_hashes, clippy::no_effect, unused)]
 #![warn(clippy::needless_raw_strings)]
-#![feature(c_str_literals)]
 
 fn main() {
     "aaa";
diff --git a/tests/ui/needless_raw_string.rs b/tests/ui/needless_raw_string.rs
index b6239f9b1f0..1126ea5aa30 100644
--- a/tests/ui/needless_raw_string.rs
+++ b/tests/ui/needless_raw_string.rs
@@ -1,6 +1,5 @@
 #![allow(clippy::needless_raw_string_hashes, clippy::no_effect, unused)]
 #![warn(clippy::needless_raw_strings)]
-#![feature(c_str_literals)]
 
 fn main() {
     r#"aaa"#;
diff --git a/tests/ui/needless_raw_string.stderr b/tests/ui/needless_raw_string.stderr
index 276bc84c6c3..b2188698564 100644
--- a/tests/ui/needless_raw_string.stderr
+++ b/tests/ui/needless_raw_string.stderr
@@ -1,5 +1,5 @@
 error: unnecessary raw string literal
-  --> $DIR/needless_raw_string.rs:6:5
+  --> $DIR/needless_raw_string.rs:5:5
    |
 LL |     r#"aaa"#;
    |     ^^^^^^^^
@@ -13,7 +13,7 @@ LL +     "aaa";
    |
 
 error: unnecessary raw string literal
-  --> $DIR/needless_raw_string.rs:9:5
+  --> $DIR/needless_raw_string.rs:8:5
    |
 LL |     br#"aaa"#;
    |     ^^^^^^^^^
@@ -25,7 +25,7 @@ LL +     b"aaa";
    |
 
 error: unnecessary raw string literal
-  --> $DIR/needless_raw_string.rs:12:5
+  --> $DIR/needless_raw_string.rs:11:5
    |
 LL |     cr#"aaa"#;
    |     ^^^^^^^^^
@@ -37,7 +37,7 @@ LL +     c"aaa";
    |
 
 error: unnecessary raw string literal
-  --> $DIR/needless_raw_string.rs:16:5
+  --> $DIR/needless_raw_string.rs:15:5
    |
 LL | /     r#"
 LL | |         a
@@ -56,7 +56,7 @@ LL ~     ";
    |
 
 error: unnecessary raw string literal
-  --> $DIR/needless_raw_string.rs:22:5
+  --> $DIR/needless_raw_string.rs:21:5
    |
 LL |     r"no hashes";
    |     ^^^^^^^^^^^^
@@ -68,7 +68,7 @@ LL +     "no hashes";
    |
 
 error: unnecessary raw string literal
-  --> $DIR/needless_raw_string.rs:23:5
+  --> $DIR/needless_raw_string.rs:22:5
    |
 LL |     br"no hashes";
    |     ^^^^^^^^^^^^^
@@ -80,7 +80,7 @@ LL +     b"no hashes";
    |
 
 error: unnecessary raw string literal
-  --> $DIR/needless_raw_string.rs:24:5
+  --> $DIR/needless_raw_string.rs:23:5
    |
 LL |     cr"no hashes";
    |     ^^^^^^^^^^^^^
diff --git a/tests/ui/needless_raw_string_hashes.fixed b/tests/ui/needless_raw_string_hashes.fixed
index c99c2f46532..b2ad657d6b2 100644
--- a/tests/ui/needless_raw_string_hashes.fixed
+++ b/tests/ui/needless_raw_string_hashes.fixed
@@ -1,6 +1,5 @@
 #![allow(clippy::no_effect, unused)]
 #![warn(clippy::needless_raw_string_hashes)]
-#![feature(c_str_literals)]
 
 fn main() {
     r"\aaa";
diff --git a/tests/ui/needless_raw_string_hashes.rs b/tests/ui/needless_raw_string_hashes.rs
index dcc2af69f4e..54d8ed76d47 100644
--- a/tests/ui/needless_raw_string_hashes.rs
+++ b/tests/ui/needless_raw_string_hashes.rs
@@ -1,6 +1,5 @@
 #![allow(clippy::no_effect, unused)]
 #![warn(clippy::needless_raw_string_hashes)]
-#![feature(c_str_literals)]
 
 fn main() {
     r#"\aaa"#;
diff --git a/tests/ui/needless_raw_string_hashes.stderr b/tests/ui/needless_raw_string_hashes.stderr
index 017160b1a42..c74eff8161e 100644
--- a/tests/ui/needless_raw_string_hashes.stderr
+++ b/tests/ui/needless_raw_string_hashes.stderr
@@ -1,5 +1,5 @@
 error: unnecessary hashes around raw string literal
-  --> $DIR/needless_raw_string_hashes.rs:6:5
+  --> $DIR/needless_raw_string_hashes.rs:5:5
    |
 LL |     r#"\aaa"#;
    |     ^^^^^^^^^
@@ -13,7 +13,7 @@ LL +     r"\aaa";
    |
 
 error: unnecessary hashes around raw string literal
-  --> $DIR/needless_raw_string_hashes.rs:7:5
+  --> $DIR/needless_raw_string_hashes.rs:6:5
    |
 LL |     r##"Hello "world"!"##;
    |     ^^^^^^^^^^^^^^^^^^^^^
@@ -25,7 +25,7 @@ LL +     r#"Hello "world"!"#;
    |
 
 error: unnecessary hashes around raw string literal
-  --> $DIR/needless_raw_string_hashes.rs:8:5
+  --> $DIR/needless_raw_string_hashes.rs:7:5
    |
 LL |     r######" "### "## "# "######;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -37,7 +37,7 @@ LL +     r####" "### "## "# "####;
    |
 
 error: unnecessary hashes around raw string literal
-  --> $DIR/needless_raw_string_hashes.rs:9:5
+  --> $DIR/needless_raw_string_hashes.rs:8:5
    |
 LL |     r######" "aa" "# "## "######;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -49,7 +49,7 @@ LL +     r###" "aa" "# "## "###;
    |
 
 error: unnecessary hashes around raw string literal
-  --> $DIR/needless_raw_string_hashes.rs:10:5
+  --> $DIR/needless_raw_string_hashes.rs:9:5
    |
 LL |     br#"\aaa"#;
    |     ^^^^^^^^^^
@@ -61,7 +61,7 @@ LL +     br"\aaa";
    |
 
 error: unnecessary hashes around raw string literal
-  --> $DIR/needless_raw_string_hashes.rs:11:5
+  --> $DIR/needless_raw_string_hashes.rs:10:5
    |
 LL |     br##"Hello "world"!"##;
    |     ^^^^^^^^^^^^^^^^^^^^^^
@@ -73,7 +73,7 @@ LL +     br#"Hello "world"!"#;
    |
 
 error: unnecessary hashes around raw string literal
-  --> $DIR/needless_raw_string_hashes.rs:12:5
+  --> $DIR/needless_raw_string_hashes.rs:11:5
    |
 LL |     br######" "### "## "# "######;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -85,7 +85,7 @@ LL +     br####" "### "## "# "####;
    |
 
 error: unnecessary hashes around raw string literal
-  --> $DIR/needless_raw_string_hashes.rs:13:5
+  --> $DIR/needless_raw_string_hashes.rs:12:5
    |
 LL |     br######" "aa" "# "## "######;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -97,7 +97,7 @@ LL +     br###" "aa" "# "## "###;
    |
 
 error: unnecessary hashes around raw string literal
-  --> $DIR/needless_raw_string_hashes.rs:14:5
+  --> $DIR/needless_raw_string_hashes.rs:13:5
    |
 LL |     cr#"\aaa"#;
    |     ^^^^^^^^^^
@@ -109,7 +109,7 @@ LL +     cr"\aaa";
    |
 
 error: unnecessary hashes around raw string literal
-  --> $DIR/needless_raw_string_hashes.rs:15:5
+  --> $DIR/needless_raw_string_hashes.rs:14:5
    |
 LL |     cr##"Hello "world"!"##;
    |     ^^^^^^^^^^^^^^^^^^^^^^
@@ -121,7 +121,7 @@ LL +     cr#"Hello "world"!"#;
    |
 
 error: unnecessary hashes around raw string literal
-  --> $DIR/needless_raw_string_hashes.rs:16:5
+  --> $DIR/needless_raw_string_hashes.rs:15:5
    |
 LL |     cr######" "### "## "# "######;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -133,7 +133,7 @@ LL +     cr####" "### "## "# "####;
    |
 
 error: unnecessary hashes around raw string literal
-  --> $DIR/needless_raw_string_hashes.rs:17:5
+  --> $DIR/needless_raw_string_hashes.rs:16:5
    |
 LL |     cr######" "aa" "# "## "######;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -145,7 +145,7 @@ LL +     cr###" "aa" "# "## "###;
    |
 
 error: unnecessary hashes around raw string literal
-  --> $DIR/needless_raw_string_hashes.rs:19:5
+  --> $DIR/needless_raw_string_hashes.rs:18:5
    |
 LL | /     r#"
 LL | |         \a
@@ -164,7 +164,7 @@ LL ~     ";
    |
 
 error: unnecessary hashes around raw string literal
-  --> $DIR/needless_raw_string_hashes.rs:25:5
+  --> $DIR/needless_raw_string_hashes.rs:24:5
    |
 LL |     r###"rust"###;
    |     ^^^^^^^^^^^^^
@@ -176,7 +176,7 @@ LL +     r"rust";
    |
 
 error: unnecessary hashes around raw string literal
-  --> $DIR/needless_raw_string_hashes.rs:26:5
+  --> $DIR/needless_raw_string_hashes.rs:25:5
    |
 LL |     r#"hello world"#;
    |     ^^^^^^^^^^^^^^^^