Skip to content

Commit 7d887ff

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 f320f42 commit 7d887ff

File tree

7 files changed

+36
-0
lines changed

7 files changed

+36
-0
lines changed

compiler/rustc_feature/src/active.rs

+2
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,8 @@ declare_features! (
530530
(active, plugin, "1.0.0", Some(29597), None),
531531
/// Allows exhaustive integer pattern matching on `usize` and `isize`.
532532
(active, precise_pointer_size_matching, "1.32.0", Some(56354), None),
533+
/// Allows creating a `b'b'` `Literal`
534+
(active, proc_macro_byte_character, "CURRENT_RUSTC_VERSION", Some(71358), None),
533535
/// Allows macro attributes on expressions, statements and non-inline modules.
534536
(active, proc_macro_hygiene, "1.30.0", Some(54727), None),
535537
/// Allows `&raw const $place_expr` and `&raw mut $place_expr` expressions.

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,7 @@ symbols! {
11591159
proc_dash_macro: "proc-macro",
11601160
proc_macro,
11611161
proc_macro_attribute,
1162+
proc_macro_byte_character,
11621163
proc_macro_derive,
11631164
proc_macro_expr,
11641165
proc_macro_gen,

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 = "71358")]
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,9 @@
1+
#![crate_type = "proc-macro"]
2+
3+
extern crate proc_macro;
4+
5+
use proc_macro::Literal;
6+
7+
fn test() {
8+
Literal::byte_character(b'a'); //~ ERROR use of unstable library feature 'proc_macro_byte_character'
9+
}
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:8:5
3+
|
4+
LL | Literal::byte_character(b'a');
5+
| ^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #71358 <https://github.com/rust-lang/rust/issues/71358> 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`.

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;

tests/ui/proc-macro/auxiliary/api/parse.rs

+4
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@ 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'");
3234
}
3335

3436
fn test_parse_literal() {
3537
assert_eq!("1".parse::<Literal>().unwrap().to_string(), "1");
3638
assert_eq!("1.0".parse::<Literal>().unwrap().to_string(), "1.0");
3739
assert_eq!("'a'".parse::<Literal>().unwrap().to_string(), "'a'");
40+
assert_eq!("b'a'".parse::<Literal>().unwrap().to_string(), "b'a'");
3841
assert_eq!("\"\n\"".parse::<Literal>().unwrap().to_string(), "\"\n\"");
3942
assert_eq!("b\"\"".parse::<Literal>().unwrap().to_string(), "b\"\"");
4043
assert_eq!("r##\"\"##".parse::<Literal>().unwrap().to_string(), "r##\"\"##");
@@ -45,6 +48,7 @@ fn test_parse_literal() {
4548
assert!(".8".parse::<Literal>().is_err());
4649
assert!("0 1".parse::<Literal>().is_err());
4750
assert!("'a".parse::<Literal>().is_err());
51+
assert!("b'❤'".parse::<Literal>().is_err());
4852
assert!(" 0".parse::<Literal>().is_err());
4953
assert!("0 ".parse::<Literal>().is_err());
5054
assert!("/* comment */0".parse::<Literal>().is_err());

0 commit comments

Comments
 (0)