Skip to content

Commit

Permalink
Rollup merge of rust-lang#119651 - novafacing:proc_macro_c_str_litera…
Browse files Browse the repository at this point in the history
…ls, 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
  • Loading branch information
compiler-errors authored Jan 17, 2024
2 parents cf3bedb + ee007ab commit dbf960a
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 4 deletions.
8 changes: 8 additions & 0 deletions library/proc_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ mod diagnostic;
#[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
pub use diagnostic::{Diagnostic, Level, MultiSpan};

use std::ffi::CStr;
use std::ops::{Range, RangeBounds};
use std::path::PathBuf;
use std::str::FromStr;
Expand Down Expand Up @@ -1351,6 +1352,13 @@ impl Literal {
Literal::new(bridge::LitKind::ByteStr, &string, None)
}

/// C string literal.
#[unstable(feature = "proc_macro_c_str_literals", issue = "119750")]
pub fn c_string(string: &CStr) -> Literal {
let string = string.to_bytes().escape_ascii().to_string();
Literal::new(bridge::LitKind::CStr, &string, None)
}

/// Returns the span encompassing this literal.
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
pub fn span(&self) -> Span {
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/feature-gates/feature-gate-proc_macro_c_str_literals.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// edition: 2021
// force-host
#![crate_type = "proc-macro"]

extern crate proc_macro;

use proc_macro::Literal;

fn test() {
Literal::c_string(c"a"); //~ ERROR use of unstable library feature 'proc_macro_c_str_literals'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error[E0658]: use of unstable library feature 'proc_macro_c_str_literals'
--> $DIR/feature-gate-proc_macro_c_str_literals.rs:10:5
|
LL | Literal::c_string(c"a");
| ^^^^^^^^^^^^^^^^^
|
= note: see issue #119750 <https://github.com/rust-lang/rust/issues/119750> for more information
= help: add `#![feature(proc_macro_c_str_literals)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0658`.
2 changes: 2 additions & 0 deletions tests/ui/proc-macro/auxiliary/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// force-host
// no-prefer-dynamic
// edition: 2021

#![crate_type = "proc-macro"]
#![crate_name = "proc_macro_api_tests"]
#![feature(proc_macro_span)]
#![feature(proc_macro_byte_character)]
#![feature(proc_macro_c_str_literals)]
#![deny(dead_code)] // catch if a test function is never called

extern crate proc_macro;
Expand Down
7 changes: 3 additions & 4 deletions tests/ui/proc-macro/auxiliary/api/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ fn test_display_literal() {
"10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0",
);

assert_eq!(
Literal::string("a \t ❤ ' \" \u{1}").to_string(),
"\"a \\t ❤ ' \\\" \\u{1}\"",
);
assert_eq!(Literal::string("a \t ❤ ' \" \u{1}").to_string(), "\"a \\t ❤ ' \\\" \\u{1}\"",);
assert_eq!(Literal::c_string(c"\'\"\x7f\u{7fff}").to_string(), r#"c"\'\"\x7f\xe7\xbf\xbf""#);
assert_eq!(Literal::character('a').to_string(), "'a'");
assert_eq!(Literal::character('\t').to_string(), "'\\t'");
assert_eq!(Literal::character('❤').to_string(), "'❤'");
Expand All @@ -41,6 +39,7 @@ fn test_parse_literal() {
assert_eq!("b'a'".parse::<Literal>().unwrap().to_string(), "b'a'");
assert_eq!("\"\n\"".parse::<Literal>().unwrap().to_string(), "\"\n\"");
assert_eq!("b\"\"".parse::<Literal>().unwrap().to_string(), "b\"\"");
assert_eq!("c\"\"".parse::<Literal>().unwrap().to_string(), "c\"\"");
assert_eq!("r##\"\"##".parse::<Literal>().unwrap().to_string(), "r##\"\"##");
assert_eq!("10ulong".parse::<Literal>().unwrap().to_string(), "10ulong");
assert_eq!("-10ulong".parse::<Literal>().unwrap().to_string(), "-10ulong");
Expand Down
1 change: 1 addition & 0 deletions tests/ui/proc-macro/test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// check-pass
// aux-build:api/mod.rs
// edition: 2021

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

0 comments on commit dbf960a

Please sign in to comment.