Skip to content

Commit

Permalink
Auto merge of #112711 - Emilgardis:lit_byte_char, r=dtolnay
Browse files Browse the repository at this point in the history
implement Literal::byte_character

without this, the only way to create a `LitKind::Byte` is by
doing `"b'a'".parse::<Literal>()`, this solves that by enabling
`Literal::byte_character(b'a')`

cc #71358

The tracking issue is #115268
  • Loading branch information
bors committed Sep 23, 2023
2 parents 13e6f24 + 74f5261 commit acfb46d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 0 deletions.
7 changes: 7 additions & 0 deletions library/proc_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,13 @@ impl Literal {
Literal::new(bridge::LitKind::Char, symbol, None)
}

/// Byte character literal.
#[unstable(feature = "proc_macro_byte_character", issue = "115268")]
pub fn byte_character(byte: u8) -> Literal {
let string = [byte].escape_ascii().to_string();
Literal::new(bridge::LitKind::Byte, &string, None)
}

/// Byte string literal.
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
pub fn byte_string(bytes: &[u8]) -> Literal {
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/feature-gates/feature-gate-proc_macro_byte_character.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// force-host
#![crate_type = "proc-macro"]

extern crate proc_macro;

use proc_macro::Literal;

fn test() {
Literal::byte_character(b'a'); //~ ERROR use of unstable library feature 'proc_macro_byte_character'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0658]: use of unstable library feature 'proc_macro_byte_character'
--> $DIR/feature-gate-proc_macro_byte_character.rs:9:5
|
LL | Literal::byte_character(b'a');
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #115268 <https://github.com/rust-lang/rust/issues/115268> for more information
= help: add `#![feature(proc_macro_byte_character)]` to the crate attributes to enable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0658`.
1 change: 1 addition & 0 deletions tests/ui/proc-macro/auxiliary/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#![crate_type = "proc-macro"]
#![crate_name = "proc_macro_api_tests"]
#![feature(proc_macro_span)]
#![feature(proc_macro_byte_character)]
#![deny(dead_code)] // catch if a test function is never called

extern crate proc_macro;
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/proc-macro/auxiliary/api/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ fn test_display_literal() {
assert_eq!(Literal::character('\'').to_string(), "'\\''");
assert_eq!(Literal::character('"').to_string(), "'\"'");
assert_eq!(Literal::character('\u{1}').to_string(), "'\\u{1}'");

assert_eq!(Literal::byte_character(b'a').to_string(), "b'a'");
assert_eq!(Literal::byte_character(0).to_string(), "b'\\x00'");
}

fn test_parse_literal() {
assert_eq!("1".parse::<Literal>().unwrap().to_string(), "1");
assert_eq!("1.0".parse::<Literal>().unwrap().to_string(), "1.0");
assert_eq!("'a'".parse::<Literal>().unwrap().to_string(), "'a'");
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!("r##\"\"##".parse::<Literal>().unwrap().to_string(), "r##\"\"##");
Expand Down

0 comments on commit acfb46d

Please sign in to comment.