Remove usage of rustc_lexer::unescape in rust-analyzer

This commit is contained in:
Guillaume Gomez 2025-04-04 14:59:14 +02:00
parent 7671097ff6
commit f2ac210f0c
7 changed files with 21 additions and 18 deletions

8
Cargo.lock generated
View file

@ -1270,6 +1270,7 @@ dependencies = [
"edition", "edition",
"expect-test", "expect-test",
"ra-ap-rustc_lexer", "ra-ap-rustc_lexer",
"rustc-literal-escaper",
"stdx", "stdx",
"tracing", "tracing",
] ]
@ -1743,6 +1744,12 @@ version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "583034fd73374156e66797ed8e5b0d5690409c9226b22d87cb7f19821c05d152" checksum = "583034fd73374156e66797ed8e5b0d5690409c9226b22d87cb7f19821c05d152"
[[package]]
name = "rustc-literal-escaper"
version = "0.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cfdd0fcb1409d38cb2d940400497e2384a4a04b8685ee92a0a7a8986ccd72115"
[[package]] [[package]]
name = "rustc-stable-hash" name = "rustc-stable-hash"
version = "0.1.1" version = "0.1.1"
@ -1982,6 +1989,7 @@ dependencies = [
"rayon", "rayon",
"rowan", "rowan",
"rustc-hash 2.0.0", "rustc-hash 2.0.0",
"rustc-literal-escaper",
"rustc_apfloat", "rustc_apfloat",
"smol_str", "smol_str",
"stdx", "stdx",

View file

@ -14,6 +14,7 @@ rust-version.workspace = true
[dependencies] [dependencies]
drop_bomb = "0.1.5" drop_bomb = "0.1.5"
ra-ap-rustc_lexer.workspace = true ra-ap-rustc_lexer.workspace = true
rustc-literal-escaper = "0.0.1"
tracing = { workspace = true, optional = true } tracing = { workspace = true, optional = true }
edition.workspace = true edition.workspace = true

View file

@ -10,7 +10,7 @@
use std::ops; use std::ops;
use rustc_lexer::unescape::{EscapeError, Mode}; use rustc_literal_escaper::{EscapeError, Mode, unescape_byte, unescape_char, unescape_mixed, unescape_unicode};
use crate::{ use crate::{
Edition, Edition,
@ -282,7 +282,7 @@ impl<'a> Converter<'a> {
let text = &self.res.text[self.offset + 1..][..len - 1]; let text = &self.res.text[self.offset + 1..][..len - 1];
let i = text.rfind('\'').unwrap(); let i = text.rfind('\'').unwrap();
let text = &text[..i]; let text = &text[..i];
if let Err(e) = rustc_lexer::unescape::unescape_char(text) { if let Err(e) = unescape_char(text) {
err = error_to_diagnostic_message(e, Mode::Char); err = error_to_diagnostic_message(e, Mode::Char);
} }
} }
@ -295,7 +295,7 @@ impl<'a> Converter<'a> {
let text = &self.res.text[self.offset + 2..][..len - 2]; let text = &self.res.text[self.offset + 2..][..len - 2];
let i = text.rfind('\'').unwrap(); let i = text.rfind('\'').unwrap();
let text = &text[..i]; let text = &text[..i];
if let Err(e) = rustc_lexer::unescape::unescape_byte(text) { if let Err(e) = unescape_byte(text) {
err = error_to_diagnostic_message(e, Mode::Byte); err = error_to_diagnostic_message(e, Mode::Byte);
} }
} }
@ -402,14 +402,14 @@ fn unescape_string_error_message(text: &str, mode: Mode) -> &'static str {
let mut error_message = ""; let mut error_message = "";
match mode { match mode {
Mode::CStr => { Mode::CStr => {
rustc_lexer::unescape::unescape_mixed(text, mode, &mut |_, res| { unescape_mixed(text, mode, &mut |_, res| {
if let Err(e) = res { if let Err(e) = res {
error_message = error_to_diagnostic_message(e, mode); error_message = error_to_diagnostic_message(e, mode);
} }
}); });
} }
Mode::ByteStr | Mode::Str => { Mode::ByteStr | Mode::Str => {
rustc_lexer::unescape::unescape_unicode(text, mode, &mut |_, res| { unescape_unicode(text, mode, &mut |_, res| {
if let Err(e) = res { if let Err(e) = res {
error_message = error_to_diagnostic_message(e, mode); error_message = error_to_diagnostic_message(e, mode);
} }

View file

@ -17,6 +17,7 @@ either.workspace = true
itertools.workspace = true itertools.workspace = true
rowan = "=0.15.15" rowan = "=0.15.15"
rustc-hash.workspace = true rustc-hash.workspace = true
rustc-literal-escaper = "0.0.1"
indexmap.workspace = true indexmap.workspace = true
smol_str.workspace = true smol_str.workspace = true
triomphe.workspace = true triomphe.workspace = true

View file

@ -2,7 +2,7 @@
use std::{borrow::Cow, num::ParseIntError}; use std::{borrow::Cow, num::ParseIntError};
use rustc_lexer::unescape::{ use rustc_literal_escaper::{
unescape_byte, unescape_char, unescape_mixed, unescape_unicode, EscapeError, MixedUnit, Mode, unescape_byte, unescape_char, unescape_mixed, unescape_unicode, EscapeError, MixedUnit, Mode,
}; };
use stdx::always; use stdx::always;

View file

@ -19,13 +19,6 @@
//! [RFC]: <https://github.com/rust-lang/rfcs/pull/2256> //! [RFC]: <https://github.com/rust-lang/rfcs/pull/2256>
//! [Swift]: <https://github.com/apple/swift/blob/13d593df6f359d0cb2fc81cfaac273297c539455/lib/Syntax/README.md> //! [Swift]: <https://github.com/apple/swift/blob/13d593df6f359d0cb2fc81cfaac273297c539455/lib/Syntax/README.md>
#![cfg_attr(feature = "in-rust-tree", feature(rustc_private))]
#[cfg(not(feature = "in-rust-tree"))]
extern crate ra_ap_rustc_lexer as rustc_lexer;
#[cfg(feature = "in-rust-tree")]
extern crate rustc_lexer;
mod parsing; mod parsing;
mod ptr; mod ptr;
mod syntax_error; mod syntax_error;
@ -64,7 +57,7 @@ pub use rowan::{
api::Preorder, Direction, GreenNode, NodeOrToken, SyntaxText, TextRange, TextSize, api::Preorder, Direction, GreenNode, NodeOrToken, SyntaxText, TextRange, TextSize,
TokenAtOffset, WalkEvent, TokenAtOffset, WalkEvent,
}; };
pub use rustc_lexer::unescape; pub use rustc_literal_escaper as unescape;
pub use smol_str::{format_smolstr, SmolStr, SmolStrBuilder, ToSmolStr}; pub use smol_str::{format_smolstr, SmolStr, SmolStrBuilder, ToSmolStr};
/// `Parse` is the result of the parsing: a syntax tree and a collection of /// `Parse` is the result of the parsing: a syntax tree and a collection of

View file

@ -5,7 +5,7 @@
mod block; mod block;
use rowan::Direction; use rowan::Direction;
use rustc_lexer::unescape::{self, unescape_mixed, unescape_unicode, Mode}; use rustc_literal_escaper::{unescape_mixed, unescape_unicode, EscapeError, Mode};
use crate::{ use crate::{
algo, algo,
@ -44,8 +44,8 @@ pub(crate) fn validate(root: &SyntaxNode, errors: &mut Vec<SyntaxError>) {
} }
} }
fn rustc_unescape_error_to_string(err: unescape::EscapeError) -> (&'static str, bool) { fn rustc_unescape_error_to_string(err: EscapeError) -> (&'static str, bool) {
use unescape::EscapeError as EE; use rustc_literal_escaper::EscapeError as EE;
#[rustfmt::skip] #[rustfmt::skip]
let err_message = match err { let err_message = match err {
@ -127,7 +127,7 @@ fn validate_literal(literal: ast::Literal, acc: &mut Vec<SyntaxError>) {
let text = token.text(); let text = token.text();
// FIXME: lift this lambda refactor to `fn` (https://github.com/rust-lang/rust-analyzer/pull/2834#discussion_r366199205) // FIXME: lift this lambda refactor to `fn` (https://github.com/rust-lang/rust-analyzer/pull/2834#discussion_r366199205)
let mut push_err = |prefix_len, off, err: unescape::EscapeError| { let mut push_err = |prefix_len, off, err: EscapeError| {
let off = token.text_range().start() + TextSize::try_from(off + prefix_len).unwrap(); let off = token.text_range().start() + TextSize::try_from(off + prefix_len).unwrap();
let (message, is_err) = rustc_unescape_error_to_string(err); let (message, is_err) = rustc_unescape_error_to_string(err);
// FIXME: Emit lexer warnings // FIXME: Emit lexer warnings