Skip to content

Commit 98e15d4

Browse files
committed
Encode ident rawness and literal kind separately in tt::Leaf
1 parent 35aa238 commit 98e15d4

33 files changed

+841
-413
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/hir-def/src/attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ impl<'attr> AttrQuery<'attr> {
596596
.nth(2);
597597

598598
match name {
599-
Some(tt::TokenTree::Leaf(tt::Leaf::Literal(tt::Literal{ ref text, ..}))) => Some(text),
599+
Some(tt::TokenTree::Leaf(tt::Leaf::Literal(tt::Literal{ text, kind: tt::LitKind::Str | tt::LitKind::StrRaw(_) , ..}))) => Some(text),
600600
_ => None
601601
}
602602
})

crates/hir-def/src/hir/format_args.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ pub(crate) fn parse(
248248
}
249249
}
250250
ArgRef::Name(name, span) => {
251-
let name = Name::new_text_dont_use(SmolStr::new(name));
251+
let name = Name::new_text_dont_use(SmolStr::new(name), tt::IdentIsRaw::No);
252252
if let Some((index, _)) = args.by_name(&name) {
253253
record_usage(name, span);
254254
// Name found in `args`, so we resolve it to its index.

crates/hir-def/src/macro_expansion_tests/builtin_fn_macro.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -460,13 +460,13 @@ fn test_concat_expand() {
460460
#[rustc_builtin_macro]
461461
macro_rules! concat {}
462462
463-
fn main() { concat!("fo", "o", 0, r#"bar"#, "\n", false, '"', '\0'); }
463+
fn main() { concat!("fo", "o", 0, r#""bar""#, "\n", false, '"', '\0'); }
464464
"##,
465465
expect![[r##"
466466
#[rustc_builtin_macro]
467467
macro_rules! concat {}
468468
469-
fn main() { "foo0bar\nfalse\"\u{0}"; }
469+
fn main() { "foo0\"bar\"\nfalse\"\u{0}"; }
470470
"##]],
471471
);
472472
}
@@ -478,13 +478,13 @@ fn test_concat_bytes_expand() {
478478
#[rustc_builtin_macro]
479479
macro_rules! concat_bytes {}
480480
481-
fn main() { concat_bytes!(b'A', b"BC", [68, b'E', 70]); }
481+
fn main() { concat_bytes!(b'A', b"BC\"", [68, b'E', 70], br#"G""#,b'\0'); }
482482
"##,
483483
expect![[r#"
484484
#[rustc_builtin_macro]
485485
macro_rules! concat_bytes {}
486486
487-
fn main() { [b'A', 66, 67, 68, b'E', 70]; }
487+
fn main() { b"ABC\"DEFG\"\x00"; }
488488
"#]],
489489
);
490490
}

crates/hir-def/src/macro_expansion_tests/mbe/regression.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ macro_rules! concat {}
10581058
macro_rules! line {}
10591059
10601060
fn main() {
1061-
"event 0u32";
1061+
"event 0";
10621062
}
10631063
10641064
"##]],
@@ -1084,7 +1084,7 @@ fn main() {
10841084
macro_rules! concat_bytes {}
10851085
10861086
fn main() {
1087-
let x = /* error: unexpected token in input */[];
1087+
let x = /* error: unexpected token in input */b"";
10881088
}
10891089
10901090
"#]],

crates/hir-def/src/nameres/collector.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, def_map: DefMap, tree_id: TreeI
8080
.iter()
8181
.enumerate()
8282
.map(|(idx, it)| {
83-
let name = Name::new_text_dont_use(it.name.clone());
83+
let name = Name::new_text_dont_use(it.name.clone(), tt::IdentIsRaw::No);
8484
(
8585
name,
8686
if !db.expand_proc_attr_macros() {
@@ -2138,7 +2138,7 @@ impl ModCollector<'_, '_> {
21382138
let name;
21392139
let name = match attrs.by_key("rustc_builtin_macro").string_value() {
21402140
Some(it) => {
2141-
name = Name::new_text_dont_use(it.into());
2141+
name = Name::new_text_dont_use(it.into(), tt::IdentIsRaw::No);
21422142
&name
21432143
}
21442144
None => {

crates/hir-expand/src/attrs.rs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ use base_db::CrateId;
55
use cfg::CfgExpr;
66
use either::Either;
77
use intern::Interned;
8-
use mbe::{syntax_node_to_token_tree, DelimiterKind, DocCommentDesugarMode, Punct};
8+
use mbe::{
9+
desugar_doc_comment_text, syntax_node_to_token_tree, token_to_literal, DelimiterKind,
10+
DocCommentDesugarMode, Punct,
11+
};
912
use smallvec::{smallvec, SmallVec};
1013
use span::{Span, SyntaxContextId};
1114
use syntax::unescape;
12-
use syntax::{ast, format_smolstr, match_ast, AstNode, AstToken, SmolStr, SyntaxNode};
15+
use syntax::{ast, match_ast, AstNode, AstToken, SyntaxNode};
1316
use triomphe::ThinArc;
1417

1518
use crate::{
@@ -52,11 +55,15 @@ impl RawAttrs {
5255
}
5356
Either::Right(comment) => comment.doc_comment().map(|doc| {
5457
let span = span_map.span_for_range(comment.syntax().text_range());
58+
let (text, kind) =
59+
desugar_doc_comment_text(doc, DocCommentDesugarMode::ProcMacro);
5560
Attr {
5661
id,
5762
input: Some(Box::new(AttrInput::Literal(tt::Literal {
58-
text: SmolStr::new(format_smolstr!("\"{}\"", Self::escape_chars(doc))),
63+
text,
5964
span,
65+
kind,
66+
suffix: None,
6067
}))),
6168
path: Interned::new(ModPath::from(crate::name!(doc))),
6269
ctxt: span.ctx,
@@ -74,10 +81,6 @@ impl RawAttrs {
7481
RawAttrs { entries }
7582
}
7683

77-
fn escape_chars(s: &str) -> String {
78-
s.replace('\\', r#"\\"#).replace('"', r#"\""#)
79-
}
80-
8184
pub fn from_attrs_owner(
8285
db: &dyn ExpandDatabase,
8386
owner: InFile<&dyn ast::HasAttrs>,
@@ -234,10 +237,8 @@ impl Attr {
234237
})?);
235238
let span = span_map.span_for_range(range);
236239
let input = if let Some(ast::Expr::Literal(lit)) = ast.expr() {
237-
Some(Box::new(AttrInput::Literal(tt::Literal {
238-
text: lit.token().text().into(),
239-
span,
240-
})))
240+
let token = lit.token();
241+
Some(Box::new(AttrInput::Literal(token_to_literal(token.text().into(), span))))
241242
} else if let Some(tt) = ast.token_tree() {
242243
let tree = syntax_node_to_token_tree(
243244
tt.syntax(),
@@ -306,24 +307,21 @@ impl Attr {
306307
/// #[path = "string"]
307308
pub fn string_value(&self) -> Option<&str> {
308309
match self.input.as_deref()? {
309-
AttrInput::Literal(it) => match it.text.strip_prefix('r') {
310-
Some(it) => it.trim_matches('#'),
311-
None => it.text.as_str(),
312-
}
313-
.strip_prefix('"')?
314-
.strip_suffix('"'),
310+
AttrInput::Literal(tt::Literal {
311+
text,
312+
kind: tt::LitKind::Str | tt::LitKind::StrRaw(_),
313+
..
314+
}) => Some(text),
315315
_ => None,
316316
}
317317
}
318318

319319
pub fn string_value_unescape(&self) -> Option<Cow<'_, str>> {
320320
match self.input.as_deref()? {
321-
AttrInput::Literal(it) => match it.text.strip_prefix('r') {
322-
Some(it) => {
323-
it.trim_matches('#').strip_prefix('"')?.strip_suffix('"').map(Cow::Borrowed)
324-
}
325-
None => it.text.strip_prefix('"')?.strip_suffix('"').and_then(unescape),
326-
},
321+
AttrInput::Literal(tt::Literal { text, kind: tt::LitKind::StrRaw(_), .. }) => {
322+
Some(Cow::Borrowed(text))
323+
}
324+
AttrInput::Literal(tt::Literal { text, kind: tt::LitKind::Str, .. }) => unescape(text),
327325
_ => None,
328326
}
329327
}

crates/hir-expand/src/builtin_derive_macro.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,8 @@ fn name_to_token(
369369
ExpandError::other("missing name")
370370
})?;
371371
let span = token_map.span_at(name.syntax().text_range().start());
372-
let name_token = tt::Ident { span, text: name.text().into() };
372+
373+
let name_token = tt::Ident::new(name.text().as_ref(), span);
373374
Ok(name_token)
374375
}
375376

0 commit comments

Comments
 (0)