Skip to content

Commit 6af4734

Browse files
Rollup merge of #139367 - GuillaumeGomez:proc-macro-values, r=Urgau
Add `*_value` methods to proc_macro lib This is the (re-)implementation of rust-lang/libs-team#459. It allows to get the actual value (unescaped) of the different string literals. It was originally done in rust-lang/rust#136355 but it broke the artifacts build so we decided to move the crate to crates.io to go around this limitation. Part of rust-lang/rust#136652. Considering this is a copy-paste of the originally approved PR, no need to go through the whole process again. \o/ r? `@Urgau`
2 parents 4693662 + 2135a01 commit 6af4734

File tree

8 files changed

+22
-21
lines changed

8 files changed

+22
-21
lines changed

Cargo.lock

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ pulldown-cmark-to-cmark = "10.0.4"
136136
pulldown-cmark = { version = "0.9.0", default-features = false }
137137
rayon = "1.8.0"
138138
rustc-hash = "2.0.0"
139+
rustc-literal-escaper = "0.0.2"
139140
semver = "1.0.14"
140141
serde = { version = "1.0.192" }
141142
serde_derive = { version = "1.0.192" }

crates/parser/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ rust-version.workspace = true
1414
[dependencies]
1515
drop_bomb = "0.1.5"
1616
ra-ap-rustc_lexer.workspace = true
17+
rustc-literal-escaper.workspace = true
1718
tracing = { workspace = true, optional = true }
1819

1920
edition.workspace = true

crates/parser/src/lexed_str.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
1111
use std::ops;
1212

13-
use rustc_lexer::unescape::{EscapeError, Mode};
13+
use rustc_literal_escaper::{EscapeError, Mode, unescape_byte, unescape_char, unescape_mixed, unescape_unicode};
1414

1515
use crate::{
1616
Edition,
@@ -282,7 +282,7 @@ impl<'a> Converter<'a> {
282282
let text = &self.res.text[self.offset + 1..][..len - 1];
283283
let i = text.rfind('\'').unwrap();
284284
let text = &text[..i];
285-
if let Err(e) = rustc_lexer::unescape::unescape_char(text) {
285+
if let Err(e) = unescape_char(text) {
286286
err = error_to_diagnostic_message(e, Mode::Char);
287287
}
288288
}
@@ -295,7 +295,7 @@ impl<'a> Converter<'a> {
295295
let text = &self.res.text[self.offset + 2..][..len - 2];
296296
let i = text.rfind('\'').unwrap();
297297
let text = &text[..i];
298-
if let Err(e) = rustc_lexer::unescape::unescape_byte(text) {
298+
if let Err(e) = unescape_byte(text) {
299299
err = error_to_diagnostic_message(e, Mode::Byte);
300300
}
301301
}
@@ -402,14 +402,14 @@ fn unescape_string_error_message(text: &str, mode: Mode) -> &'static str {
402402
let mut error_message = "";
403403
match mode {
404404
Mode::CStr => {
405-
rustc_lexer::unescape::unescape_mixed(text, mode, &mut |_, res| {
405+
unescape_mixed(text, mode, &mut |_, res| {
406406
if let Err(e) = res {
407407
error_message = error_to_diagnostic_message(e, mode);
408408
}
409409
});
410410
}
411411
Mode::ByteStr | Mode::Str => {
412-
rustc_lexer::unescape::unescape_unicode(text, mode, &mut |_, res| {
412+
unescape_unicode(text, mode, &mut |_, res| {
413413
if let Err(e) = res {
414414
error_message = error_to_diagnostic_message(e, mode);
415415
}

crates/syntax/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ either.workspace = true
1717
itertools.workspace = true
1818
rowan = "=0.15.15"
1919
rustc-hash.workspace = true
20+
rustc-literal-escaper.workspace = true
2021
indexmap.workspace = true
2122
smol_str.workspace = true
2223
triomphe.workspace = true
2324
tracing.workspace = true
2425

25-
ra-ap-rustc_lexer.workspace = true
26-
2726
parser.workspace = true
2827
stdx.workspace = true
2928

crates/syntax/src/ast/token_ext.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::{borrow::Cow, num::ParseIntError};
44

5-
use rustc_lexer::unescape::{
5+
use rustc_literal_escaper::{
66
unescape_byte, unescape_char, unescape_mixed, unescape_unicode, EscapeError, MixedUnit, Mode,
77
};
88
use stdx::always;

crates/syntax/src/lib.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,6 @@
1919
//! [RFC]: <https://github.com/rust-lang/rfcs/pull/2256>
2020
//! [Swift]: <https://github.com/apple/swift/blob/13d593df6f359d0cb2fc81cfaac273297c539455/lib/Syntax/README.md>
2121
22-
#![cfg_attr(feature = "in-rust-tree", feature(rustc_private))]
23-
24-
#[cfg(not(feature = "in-rust-tree"))]
25-
extern crate ra_ap_rustc_lexer as rustc_lexer;
26-
#[cfg(feature = "in-rust-tree")]
27-
extern crate rustc_lexer;
28-
2922
mod parsing;
3023
mod ptr;
3124
mod syntax_error;
@@ -64,7 +57,7 @@ pub use rowan::{
6457
api::Preorder, Direction, GreenNode, NodeOrToken, SyntaxText, TextRange, TextSize,
6558
TokenAtOffset, WalkEvent,
6659
};
67-
pub use rustc_lexer::unescape;
60+
pub use rustc_literal_escaper as unescape;
6861
pub use smol_str::{format_smolstr, SmolStr, SmolStrBuilder, ToSmolStr};
6962

7063
/// `Parse` is the result of the parsing: a syntax tree and a collection of

crates/syntax/src/validation.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
mod block;
66

77
use rowan::Direction;
8-
use rustc_lexer::unescape::{self, unescape_mixed, unescape_unicode, Mode};
8+
use rustc_literal_escaper::{unescape_mixed, unescape_unicode, EscapeError, Mode};
99

1010
use crate::{
1111
algo,
@@ -44,8 +44,8 @@ pub(crate) fn validate(root: &SyntaxNode, errors: &mut Vec<SyntaxError>) {
4444
}
4545
}
4646

47-
fn rustc_unescape_error_to_string(err: unescape::EscapeError) -> (&'static str, bool) {
48-
use unescape::EscapeError as EE;
47+
fn rustc_unescape_error_to_string(err: EscapeError) -> (&'static str, bool) {
48+
use rustc_literal_escaper::EscapeError as EE;
4949

5050
#[rustfmt::skip]
5151
let err_message = match err {
@@ -127,7 +127,7 @@ fn validate_literal(literal: ast::Literal, acc: &mut Vec<SyntaxError>) {
127127
let text = token.text();
128128

129129
// FIXME: lift this lambda refactor to `fn` (https://github.com/rust-lang/rust-analyzer/pull/2834#discussion_r366199205)
130-
let mut push_err = |prefix_len, off, err: unescape::EscapeError| {
130+
let mut push_err = |prefix_len, off, err: EscapeError| {
131131
let off = token.text_range().start() + TextSize::try_from(off + prefix_len).unwrap();
132132
let (message, is_err) = rustc_unescape_error_to_string(err);
133133
// FIXME: Emit lexer warnings

0 commit comments

Comments
 (0)