Skip to content

Commit f45fe57

Browse files
committed
Auto merge of rust-lang#119651 - novafacing:proc_macro_c_str_literals, r=Amanieu
proc_macro: Add Literal::c_string constructor Adds a constructor for C string literals, hopefully starts addressing rust-lang#118560. Tracking issue: rust-lang#119750
2 parents 6bf600b + ee007ab commit f45fe57

File tree

6 files changed

+38
-4
lines changed

6 files changed

+38
-4
lines changed

Diff for: library/proc_macro/src/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ mod diagnostic;
4545
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
4646
pub use diagnostic::{Diagnostic, Level, MultiSpan};
4747

48+
use std::ffi::CStr;
4849
use std::ops::{Range, RangeBounds};
4950
use std::path::PathBuf;
5051
use std::str::FromStr;
@@ -1351,6 +1352,13 @@ impl Literal {
13511352
Literal::new(bridge::LitKind::ByteStr, &string, None)
13521353
}
13531354

1355+
/// C string literal.
1356+
#[unstable(feature = "proc_macro_c_str_literals", issue = "119750")]
1357+
pub fn c_string(string: &CStr) -> Literal {
1358+
let string = string.to_bytes().escape_ascii().to_string();
1359+
Literal::new(bridge::LitKind::CStr, &string, None)
1360+
}
1361+
13541362
/// Returns the span encompassing this literal.
13551363
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
13561364
pub fn span(&self) -> Span {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// edition: 2021
2+
// force-host
3+
#![crate_type = "proc-macro"]
4+
5+
extern crate proc_macro;
6+
7+
use proc_macro::Literal;
8+
9+
fn test() {
10+
Literal::c_string(c"a"); //~ ERROR use of unstable library feature 'proc_macro_c_str_literals'
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0658]: use of unstable library feature 'proc_macro_c_str_literals'
2+
--> $DIR/feature-gate-proc_macro_c_str_literals.rs:10:5
3+
|
4+
LL | Literal::c_string(c"a");
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #119750 <https://github.com/rust-lang/rust/issues/119750> for more information
8+
= help: add `#![feature(proc_macro_c_str_literals)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
11+
error: aborting due to 1 previous error
12+
13+
For more information about this error, try `rustc --explain E0658`.

Diff for: tests/ui/proc-macro/auxiliary/api/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
// force-host
22
// no-prefer-dynamic
3+
// edition: 2021
34

45
#![crate_type = "proc-macro"]
56
#![crate_name = "proc_macro_api_tests"]
67
#![feature(proc_macro_span)]
78
#![feature(proc_macro_byte_character)]
9+
#![feature(proc_macro_c_str_literals)]
810
#![deny(dead_code)] // catch if a test function is never called
911

1012
extern crate proc_macro;

Diff for: tests/ui/proc-macro/auxiliary/api/parse.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ fn test_display_literal() {
1919
"10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0",
2020
);
2121

22-
assert_eq!(
23-
Literal::string("a \t ❤ ' \" \u{1}").to_string(),
24-
"\"a \\t ❤ ' \\\" \\u{1}\"",
25-
);
22+
assert_eq!(Literal::string("a \t ❤ ' \" \u{1}").to_string(), "\"a \\t ❤ ' \\\" \\u{1}\"",);
23+
assert_eq!(Literal::c_string(c"\'\"\x7f\u{7fff}").to_string(), r#"c"\'\"\x7f\xe7\xbf\xbf""#);
2624
assert_eq!(Literal::character('a').to_string(), "'a'");
2725
assert_eq!(Literal::character('\t').to_string(), "'\\t'");
2826
assert_eq!(Literal::character('❤').to_string(), "'❤'");
@@ -41,6 +39,7 @@ fn test_parse_literal() {
4139
assert_eq!("b'a'".parse::<Literal>().unwrap().to_string(), "b'a'");
4240
assert_eq!("\"\n\"".parse::<Literal>().unwrap().to_string(), "\"\n\"");
4341
assert_eq!("b\"\"".parse::<Literal>().unwrap().to_string(), "b\"\"");
42+
assert_eq!("c\"\"".parse::<Literal>().unwrap().to_string(), "c\"\"");
4443
assert_eq!("r##\"\"##".parse::<Literal>().unwrap().to_string(), "r##\"\"##");
4544
assert_eq!("10ulong".parse::<Literal>().unwrap().to_string(), "10ulong");
4645
assert_eq!("-10ulong".parse::<Literal>().unwrap().to_string(), "-10ulong");

Diff for: tests/ui/proc-macro/test.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// check-pass
22
// aux-build:api/mod.rs
3+
// edition: 2021
34

45
//! This is for everything that *would* be a #[test] inside of libproc_macro,
56
//! except for the fact that proc_macro objects are not capable of existing

0 commit comments

Comments
 (0)