From 74f52613454714cd033165cc635a407ef61f04c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20Gardstr=C3=B6m?= Date: Sat, 23 Sep 2023 20:48:00 +0200 Subject: [PATCH] implement Literal::byte_character without this, the only way to create a `LitKind::Byte` is by doing `"b'a'".parse::()`, this solves that by enabling `Literal::byte_character(b'a')` --- library/proc_macro/src/lib.rs | 7 +++++++ .../feature-gate-proc_macro_byte_character.rs | 10 ++++++++++ .../feature-gate-proc_macro_byte_character.stderr | 12 ++++++++++++ tests/ui/proc-macro/auxiliary/api/mod.rs | 1 + tests/ui/proc-macro/auxiliary/api/parse.rs | 4 ++++ 5 files changed, 34 insertions(+) create mode 100644 tests/ui/feature-gates/feature-gate-proc_macro_byte_character.rs create mode 100644 tests/ui/feature-gates/feature-gate-proc_macro_byte_character.stderr diff --git a/library/proc_macro/src/lib.rs b/library/proc_macro/src/lib.rs index d382fec93521a..d322d882cc189 100644 --- a/library/proc_macro/src/lib.rs +++ b/library/proc_macro/src/lib.rs @@ -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 { diff --git a/tests/ui/feature-gates/feature-gate-proc_macro_byte_character.rs b/tests/ui/feature-gates/feature-gate-proc_macro_byte_character.rs new file mode 100644 index 0000000000000..0648ce0ee20e8 --- /dev/null +++ b/tests/ui/feature-gates/feature-gate-proc_macro_byte_character.rs @@ -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' +} diff --git a/tests/ui/feature-gates/feature-gate-proc_macro_byte_character.stderr b/tests/ui/feature-gates/feature-gate-proc_macro_byte_character.stderr new file mode 100644 index 0000000000000..0069153893886 --- /dev/null +++ b/tests/ui/feature-gates/feature-gate-proc_macro_byte_character.stderr @@ -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 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`. diff --git a/tests/ui/proc-macro/auxiliary/api/mod.rs b/tests/ui/proc-macro/auxiliary/api/mod.rs index 739c25132e77b..3bea5d75779aa 100644 --- a/tests/ui/proc-macro/auxiliary/api/mod.rs +++ b/tests/ui/proc-macro/auxiliary/api/mod.rs @@ -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; diff --git a/tests/ui/proc-macro/auxiliary/api/parse.rs b/tests/ui/proc-macro/auxiliary/api/parse.rs index 27391f8311176..07c9f464961ae 100644 --- a/tests/ui/proc-macro/auxiliary/api/parse.rs +++ b/tests/ui/proc-macro/auxiliary/api/parse.rs @@ -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::().unwrap().to_string(), "1"); assert_eq!("1.0".parse::().unwrap().to_string(), "1.0"); assert_eq!("'a'".parse::().unwrap().to_string(), "'a'"); + assert_eq!("b'a'".parse::().unwrap().to_string(), "b'a'"); assert_eq!("\"\n\"".parse::().unwrap().to_string(), "\"\n\""); assert_eq!("b\"\"".parse::().unwrap().to_string(), "b\"\""); assert_eq!("r##\"\"##".parse::().unwrap().to_string(), "r##\"\"##");