Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement Literal::byte_character #112711

Merged
merged 1 commit into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading