Skip to content

Commit c5fc669

Browse files
committed
Auto merge of rust-lang#17559 - Veykril:tokentree, r=Veykril
Encode ident rawness and literal kind separately in tt::Leaf
2 parents c1d08b5 + 39eb8c6 commit c5fc669

File tree

36 files changed

+910
-431
lines changed

36 files changed

+910
-431
lines changed

src/tools/rust-analyzer/Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,7 @@ dependencies = [
10461046
"arrayvec",
10471047
"cov-mark",
10481048
"parser",
1049+
"ra-ap-rustc_lexer",
10491050
"rustc-hash",
10501051
"smallvec",
10511052
"span",

src/tools/rust-analyzer/crates/hir-def/src/attr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ impl<'attr> AttrQuery<'attr> {
605605
.nth(2);
606606

607607
match name {
608-
Some(tt::TokenTree::Leaf(tt::Leaf::Literal(tt::Literal{ ref text, ..}))) => Some(text),
608+
Some(tt::TokenTree::Leaf(tt::Leaf::Literal(tt::Literal{ text, kind: tt::LitKind::Str | tt::LitKind::StrRaw(_) , ..}))) => Some(text),
609609
_ => None
610610
}
611611
})

src/tools/rust-analyzer/crates/hir-def/src/hir/format_args.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ pub(crate) fn parse(
250250
}
251251
}
252252
ArgRef::Name(name, span) => {
253-
let name = Name::new(name, call_ctx);
253+
let name = Name::new(name, tt::IdentIsRaw::No, call_ctx);
254254
if let Some((index, _)) = args.by_name(&name) {
255255
record_usage(name, span);
256256
// Name found in `args`, so we resolve it to its index.

src/tools/rust-analyzer/crates/hir-def/src/macro_expansion_tests/builtin_fn_macro.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ fn main() { file!(); }
154154
#[rustc_builtin_macro]
155155
macro_rules! file {() => {}}
156156
157-
fn main() { ""; }
157+
fn main() { "file"; }
158158
"##]],
159159
);
160160
}
@@ -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
}

src/tools/rust-analyzer/crates/hir-def/src/macro_expansion_tests/mbe/regression.rs

+2-2
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
"#]],

src/tools/rust-analyzer/crates/hir-def/src/nameres/collector.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, def_map: DefMap, tree_id: TreeI
8282
.iter()
8383
.enumerate()
8484
.map(|(idx, it)| {
85-
let name = Name::new(&it.name, ctx);
85+
let name = Name::new(&it.name, tt::IdentIsRaw::No, ctx);
8686
(
8787
name,
8888
if !db.expand_proc_attr_macros() {
@@ -2144,7 +2144,7 @@ impl ModCollector<'_, '_> {
21442144
let name;
21452145
let name = match attrs.by_key("rustc_builtin_macro").string_value_with_span() {
21462146
Some((it, span)) => {
2147-
name = Name::new(it, span.ctx);
2147+
name = Name::new(it, tt::IdentIsRaw::No, span.ctx);
21482148
&name
21492149
}
21502150
None => {

src/tools/rust-analyzer/crates/hir-expand/src/attrs.rs

+21-23
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::{sym, 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::name::Name;
@@ -53,11 +56,15 @@ impl RawAttrs {
5356
}
5457
Either::Right(comment) => comment.doc_comment().map(|doc| {
5558
let span = span_map.span_for_range(comment.syntax().text_range());
59+
let (text, kind) =
60+
desugar_doc_comment_text(doc, DocCommentDesugarMode::ProcMacro);
5661
Attr {
5762
id,
5863
input: Some(Box::new(AttrInput::Literal(tt::Literal {
59-
text: SmolStr::new(format_smolstr!("\"{}\"", Self::escape_chars(doc))),
64+
text,
6065
span,
66+
kind,
67+
suffix: None,
6168
}))),
6269
path: Interned::new(ModPath::from(Name::new_symbol(
6370
sym::doc.clone(),
@@ -78,10 +85,6 @@ impl RawAttrs {
7885
RawAttrs { entries }
7986
}
8087

81-
fn escape_chars(s: &str) -> String {
82-
s.replace('\\', r#"\\"#).replace('"', r#"\""#)
83-
}
84-
8588
pub fn from_attrs_owner(
8689
db: &dyn ExpandDatabase,
8790
owner: InFile<&dyn ast::HasAttrs>,
@@ -238,10 +241,8 @@ impl Attr {
238241
})?);
239242
let span = span_map.span_for_range(range);
240243
let input = if let Some(ast::Expr::Literal(lit)) = ast.expr() {
241-
Some(Box::new(AttrInput::Literal(tt::Literal {
242-
text: lit.token().text().into(),
243-
span,
244-
})))
244+
let token = lit.token();
245+
Some(Box::new(AttrInput::Literal(token_to_literal(token.text().into(), span))))
245246
} else if let Some(tt) = ast.token_tree() {
246247
let tree = syntax_node_to_token_tree(
247248
tt.syntax(),
@@ -310,12 +311,11 @@ impl Attr {
310311
/// #[path = "string"]
311312
pub fn string_value(&self) -> Option<&str> {
312313
match self.input.as_deref()? {
313-
AttrInput::Literal(it) => match it.text.strip_prefix('r') {
314-
Some(it) => it.trim_matches('#'),
315-
None => it.text.as_str(),
316-
}
317-
.strip_prefix('"')?
318-
.strip_suffix('"'),
314+
AttrInput::Literal(tt::Literal {
315+
text,
316+
kind: tt::LitKind::Str | tt::LitKind::StrRaw(_),
317+
..
318+
}) => Some(text),
319319
_ => None,
320320
}
321321
}
@@ -336,12 +336,10 @@ impl Attr {
336336

337337
pub fn string_value_unescape(&self) -> Option<Cow<'_, str>> {
338338
match self.input.as_deref()? {
339-
AttrInput::Literal(it) => match it.text.strip_prefix('r') {
340-
Some(it) => {
341-
it.trim_matches('#').strip_prefix('"')?.strip_suffix('"').map(Cow::Borrowed)
342-
}
343-
None => it.text.strip_prefix('"')?.strip_suffix('"').and_then(unescape),
344-
},
339+
AttrInput::Literal(tt::Literal { text, kind: tt::LitKind::StrRaw(_), .. }) => {
340+
Some(Cow::Borrowed(text))
341+
}
342+
AttrInput::Literal(tt::Literal { text, kind: tt::LitKind::Str, .. }) => unescape(text),
345343
_ => None,
346344
}
347345
}

src/tools/rust-analyzer/crates/hir-expand/src/builtin_derive_macro.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,8 @@ fn name_to_token(
370370
ExpandError::other("missing name")
371371
})?;
372372
let span = token_map.span_at(name.syntax().text_range().start());
373-
let name_token = tt::Ident { span, text: name.text().into() };
373+
374+
let name_token = tt::Ident::new(name.text().as_ref(), span);
374375
Ok(name_token)
375376
}
376377

0 commit comments

Comments
 (0)