Skip to content

Commit 74f5261

Browse files
committed
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')`
1 parent 19c6502 commit 74f5261

File tree

5 files changed

+34
-0
lines changed

5 files changed

+34
-0
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,13 @@ impl Literal {
13371337
Literal::new(bridge::LitKind::Char, symbol, None)
13381338
}
13391339

1340+
/// Byte character literal.
1341+
#[unstable(feature = "proc_macro_byte_character", issue = "115268")]
1342+
pub fn byte_character(byte: u8) -> Literal {
1343+
let string = [byte].escape_ascii().to_string();
1344+
Literal::new(bridge::LitKind::Byte, &string, None)
1345+
}
1346+
13401347
/// Byte string literal.
13411348
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
13421349
pub fn byte_string(bytes: &[u8]) -> Literal {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// force-host
2+
#![crate_type = "proc-macro"]
3+
4+
extern crate proc_macro;
5+
6+
use proc_macro::Literal;
7+
8+
fn test() {
9+
Literal::byte_character(b'a'); //~ ERROR use of unstable library feature 'proc_macro_byte_character'
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0658]: use of unstable library feature 'proc_macro_byte_character'
2+
--> $DIR/feature-gate-proc_macro_byte_character.rs:9:5
3+
|
4+
LL | Literal::byte_character(b'a');
5+
| ^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #115268 <https://github.com./rust-lang/rust/issues/115268> for more information
8+
= help: add `#![feature(proc_macro_byte_character)]` to the crate attributes to enable
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0658`.

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

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![crate_type = "proc-macro"]
55
#![crate_name = "proc_macro_api_tests"]
66
#![feature(proc_macro_span)]
7+
#![feature(proc_macro_byte_character)]
78
#![deny(dead_code)] // catch if a test function is never called
89

910
extern crate proc_macro;

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

+4
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,16 @@ fn test_display_literal() {
2929
assert_eq!(Literal::character('\'').to_string(), "'\\''");
3030
assert_eq!(Literal::character('"').to_string(), "'\"'");
3131
assert_eq!(Literal::character('\u{1}').to_string(), "'\\u{1}'");
32+
33+
assert_eq!(Literal::byte_character(b'a').to_string(), "b'a'");
34+
assert_eq!(Literal::byte_character(0).to_string(), "b'\\x00'");
3235
}
3336

3437
fn test_parse_literal() {
3538
assert_eq!("1".parse::<Literal>().unwrap().to_string(), "1");
3639
assert_eq!("1.0".parse::<Literal>().unwrap().to_string(), "1.0");
3740
assert_eq!("'a'".parse::<Literal>().unwrap().to_string(), "'a'");
41+
assert_eq!("b'a'".parse::<Literal>().unwrap().to_string(), "b'a'");
3842
assert_eq!("\"\n\"".parse::<Literal>().unwrap().to_string(), "\"\n\"");
3943
assert_eq!("b\"\"".parse::<Literal>().unwrap().to_string(), "b\"\"");
4044
assert_eq!("r##\"\"##".parse::<Literal>().unwrap().to_string(), "r##\"\"##");

0 commit comments

Comments
 (0)