Skip to content

Commit 11d6212

Browse files
authored
Merge pull request #21362 from ChayimFriedman2/compress-spans-v2
internal: Preparations for span compression
2 parents be6975f + 819cdf0 commit 11d6212

27 files changed

Lines changed: 524 additions & 495 deletions

File tree

crates/cfg/src/cfg_expr.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ fn next_cfg_expr_from_ast(
157157
},
158158
ctx: span::SyntaxContext::root(span::Edition::Edition2015),
159159
};
160-
let literal = tt::token_to_literal(literal.text(), dummy_span).symbol;
160+
let literal =
161+
Symbol::intern(tt::token_to_literal(literal.text(), dummy_span).text());
161162
it.next();
162163
CfgAtom::KeyValue { key: name, value: literal.clone() }.into()
163164
} else {
@@ -197,20 +198,21 @@ fn next_cfg_expr(it: &mut tt::iter::TtIter<'_>) -> Option<CfgExpr> {
197198
Some(_) => return Some(CfgExpr::Invalid),
198199
};
199200

200-
let ret = match it.peek() {
201+
let mut it_clone = it.clone();
202+
let ret = match it_clone.next() {
201203
Some(TtElement::Leaf(tt::Leaf::Punct(punct)))
202204
// Don't consume on e.g. `=>`.
203205
if punct.char == '='
204206
&& (punct.spacing == tt::Spacing::Alone
205-
|| it.remaining().flat_tokens().get(1).is_none_or(|peek2| {
206-
!matches!(peek2, tt::TokenTree::Leaf(tt::Leaf::Punct(_)))
207+
|| it_clone.peek().is_none_or(|peek2| {
208+
!matches!(peek2, tt::TtElement::Leaf(tt::Leaf::Punct(_)))
207209
})) =>
208210
{
209-
match it.remaining().flat_tokens().get(1) {
210-
Some(tt::TokenTree::Leaf(tt::Leaf::Literal(literal))) => {
211+
match it_clone.next() {
212+
Some(tt::TtElement::Leaf(tt::Leaf::Literal(literal))) => {
211213
it.next();
212214
it.next();
213-
CfgAtom::KeyValue { key: name, value: literal.symbol.clone() }.into()
215+
CfgAtom::KeyValue { key: name, value: Symbol::intern(literal.text()) }.into()
214216
}
215217
_ => return Some(CfgExpr::Invalid),
216218
}

crates/hir-def/src/item_tree/attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ impl<'attr> AttrQuery<'attr> {
226226
}
227227

228228
#[inline]
229-
pub(crate) fn string_value_with_span(self) -> Option<(&'attr Symbol, span::Span)> {
229+
pub(crate) fn string_value_with_span(self) -> Option<(&'attr str, span::Span)> {
230230
self.attrs().find_map(|attr| attr.string_value_with_span())
231231
}
232232

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ pub(super) fn attr_macro_as_call_id(
114114
let arg = match macro_attr.input.as_deref() {
115115
Some(AttrInput::TokenTree(tt)) => {
116116
let mut tt = tt.clone();
117-
tt.top_subtree_delimiter_mut().kind = tt::DelimiterKind::Invisible;
117+
tt.set_top_subtree_delimiter_kind(tt::DelimiterKind::Invisible);
118118
Some(tt)
119119
}
120120

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use hir_expand::{
1717
name::{AsName, Name},
1818
proc_macro::CustomProcMacroExpander,
1919
};
20-
use intern::{Interned, sym};
20+
use intern::{Interned, Symbol, sym};
2121
use itertools::izip;
2222
use la_arena::Idx;
2323
use rustc_hash::{FxHashMap, FxHashSet};
@@ -292,13 +292,13 @@ impl<'db> DefCollector<'db> {
292292
match () {
293293
() if *attr_name == sym::recursion_limit => {
294294
if let Some(limit) = attr.string_value()
295-
&& let Ok(limit) = limit.as_str().parse()
295+
&& let Ok(limit) = limit.parse()
296296
{
297297
crate_data.recursion_limit = Some(limit);
298298
}
299299
}
300300
() if *attr_name == sym::crate_type => {
301-
if attr.string_value() == Some(&sym::proc_dash_macro) {
301+
if attr.string_value() == Some("proc-macro") {
302302
self.is_proc_macro = true;
303303
}
304304
}
@@ -2460,14 +2460,14 @@ impl ModCollector<'_, '_> {
24602460
let name;
24612461
let name = match attrs.by_key(sym::rustc_builtin_macro).string_value_with_span() {
24622462
Some((it, span)) => {
2463-
name = Name::new_symbol(it.clone(), span.ctx);
2463+
name = Name::new_symbol(Symbol::intern(it), span.ctx);
24642464
&name
24652465
}
24662466
None => {
24672467
let explicit_name =
24682468
attrs.by_key(sym::rustc_builtin_macro).tt_values().next().and_then(|tt| {
2469-
match tt.token_trees().flat_tokens().first() {
2470-
Some(tt::TokenTree::Leaf(tt::Leaf::Ident(name))) => Some(name),
2469+
match tt.token_trees().iter().next() {
2470+
Some(tt::TtElement::Leaf(tt::Leaf::Ident(name))) => Some(name),
24712471
_ => None,
24722472
}
24732473
});

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

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
33
use hir_expand::name::{AsName, Name};
44
use intern::sym;
5+
use itertools::Itertools;
56

67
use crate::{
78
item_tree::Attrs,
8-
tt::{Leaf, TokenTree, TopSubtree, TtElement},
9+
tt::{Leaf, TopSubtree, TtElement},
910
};
1011

1112
#[derive(Debug, PartialEq, Eq)]
@@ -61,35 +62,35 @@ impl Attrs<'_> {
6162

6263
// This fn is intended for `#[proc_macro_derive(..)]` and `#[rustc_builtin_macro(..)]`, which have
6364
// the same structure.
64-
#[rustfmt::skip]
6565
pub(crate) fn parse_macro_name_and_helper_attrs(tt: &TopSubtree) -> Option<(Name, Box<[Name]>)> {
66-
match tt.token_trees().flat_tokens() {
66+
if let Some([TtElement::Leaf(Leaf::Ident(trait_name))]) =
67+
tt.token_trees().iter().collect_array()
68+
{
6769
// `#[proc_macro_derive(Trait)]`
6870
// `#[rustc_builtin_macro(Trait)]`
69-
[TokenTree::Leaf(Leaf::Ident(trait_name))] => Some((trait_name.as_name(), Box::new([]))),
70-
71+
Some((trait_name.as_name(), Box::new([])))
72+
} else if let Some(
73+
[
74+
TtElement::Leaf(Leaf::Ident(trait_name)),
75+
TtElement::Leaf(Leaf::Punct(comma)),
76+
TtElement::Leaf(Leaf::Ident(attributes)),
77+
TtElement::Subtree(_, helpers),
78+
],
79+
) = tt.token_trees().iter().collect_array()
80+
&& comma.char == ','
81+
&& attributes.sym == sym::attributes
82+
{
7183
// `#[proc_macro_derive(Trait, attributes(helper1, helper2, ...))]`
7284
// `#[rustc_builtin_macro(Trait, attributes(helper1, helper2, ...))]`
73-
[
74-
TokenTree::Leaf(Leaf::Ident(trait_name)),
75-
TokenTree::Leaf(Leaf::Punct(comma)),
76-
TokenTree::Leaf(Leaf::Ident(attributes)),
77-
TokenTree::Subtree(_),
78-
..
79-
] if comma.char == ',' && attributes.sym == sym::attributes =>
80-
{
81-
let helpers = tt::TokenTreesView::new(&tt.token_trees().flat_tokens()[3..]).try_into_subtree()?;
82-
let helpers = helpers
83-
.iter()
84-
.filter_map(|tt| match tt {
85-
TtElement::Leaf(Leaf::Ident(helper)) => Some(helper.as_name()),
86-
_ => None,
87-
})
88-
.collect::<Box<[_]>>();
89-
90-
Some((trait_name.as_name(), helpers))
91-
}
85+
let helpers = helpers
86+
.filter_map(|tt| match tt {
87+
TtElement::Leaf(Leaf::Ident(helper)) => Some(helper.as_name()),
88+
_ => None,
89+
})
90+
.collect::<Box<[_]>>();
9291

93-
_ => None,
92+
Some((trait_name.as_name(), helpers))
93+
} else {
94+
None
9495
}
9596
}

crates/hir-expand/src/attrs.rs

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ use arrayvec::ArrayVec;
3535
use base_db::Crate;
3636
use cfg::{CfgExpr, CfgOptions};
3737
use either::Either;
38-
use intern::{Interned, Symbol};
38+
use intern::Interned;
39+
use itertools::Itertools;
3940
use mbe::{DelimiterKind, Punct};
4041
use parser::T;
4142
use smallvec::SmallVec;
@@ -416,47 +417,42 @@ impl fmt::Display for AttrInput {
416417

417418
impl Attr {
418419
/// #[path = "string"]
419-
pub fn string_value(&self) -> Option<&Symbol> {
420+
pub fn string_value(&self) -> Option<&str> {
420421
match self.input.as_deref()? {
421-
AttrInput::Literal(tt::Literal {
422-
symbol: text,
423-
kind: tt::LitKind::Str | tt::LitKind::StrRaw(_),
424-
..
425-
}) => Some(text),
422+
AttrInput::Literal(
423+
lit @ tt::Literal { kind: tt::LitKind::Str | tt::LitKind::StrRaw(_), .. },
424+
) => Some(lit.text()),
426425
_ => None,
427426
}
428427
}
429428

430429
/// #[path = "string"]
431-
pub fn string_value_with_span(&self) -> Option<(&Symbol, span::Span)> {
430+
pub fn string_value_with_span(&self) -> Option<(&str, span::Span)> {
432431
match self.input.as_deref()? {
433-
AttrInput::Literal(tt::Literal {
434-
symbol: text,
435-
kind: tt::LitKind::Str | tt::LitKind::StrRaw(_),
436-
span,
437-
suffix: _,
438-
}) => Some((text, *span)),
432+
AttrInput::Literal(
433+
lit @ tt::Literal { kind: tt::LitKind::Str | tt::LitKind::StrRaw(_), span, .. },
434+
) => Some((lit.text(), *span)),
439435
_ => None,
440436
}
441437
}
442438

443439
pub fn string_value_unescape(&self) -> Option<Cow<'_, str>> {
444440
match self.input.as_deref()? {
445-
AttrInput::Literal(tt::Literal {
446-
symbol: text, kind: tt::LitKind::StrRaw(_), ..
447-
}) => Some(Cow::Borrowed(text.as_str())),
448-
AttrInput::Literal(tt::Literal { symbol: text, kind: tt::LitKind::Str, .. }) => {
449-
unescape(text.as_str())
441+
AttrInput::Literal(lit @ tt::Literal { kind: tt::LitKind::StrRaw(_), .. }) => {
442+
Some(Cow::Borrowed(lit.text()))
443+
}
444+
AttrInput::Literal(lit @ tt::Literal { kind: tt::LitKind::Str, .. }) => {
445+
unescape(lit.text())
450446
}
451447
_ => None,
452448
}
453449
}
454450

455451
/// #[path(ident)]
456-
pub fn single_ident_value(&self) -> Option<&tt::Ident> {
452+
pub fn single_ident_value(&self) -> Option<tt::Ident> {
457453
match self.input.as_deref()? {
458-
AttrInput::TokenTree(tt) => match tt.token_trees().flat_tokens() {
459-
[tt::TokenTree::Leaf(tt::Leaf::Ident(ident))] => Some(ident),
454+
AttrInput::TokenTree(tt) => match tt.token_trees().iter().collect_array() {
455+
Some([tt::TtElement::Leaf(tt::Leaf::Ident(ident))]) => Some(ident),
460456
_ => None,
461457
},
462458
_ => None,
@@ -492,7 +488,7 @@ fn parse_path_comma_token_tree<'a>(
492488
args.token_trees()
493489
.split(|tt| matches!(tt, tt::TtElement::Leaf(tt::Leaf::Punct(Punct { char: ',', .. }))))
494490
.filter_map(move |tts| {
495-
let span = tts.flat_tokens().first()?.first_span();
491+
let span = tts.first_span()?;
496492
Some((ModPath::from_tt(db, tts)?, span, tts))
497493
})
498494
}
@@ -611,16 +607,12 @@ impl AttrId {
611607
else {
612608
return derive_attr_range;
613609
};
614-
let (Some(first_tt), Some(last_tt)) =
615-
(derive_tts.flat_tokens().first(), derive_tts.flat_tokens().last())
610+
let (Some(first_span), Some(last_span)) = (derive_tts.first_span(), derive_tts.last_span())
616611
else {
617612
return derive_attr_range;
618613
};
619-
let start = first_tt.first_span().range.start();
620-
let end = match last_tt {
621-
tt::TokenTree::Leaf(it) => it.span().range.end(),
622-
tt::TokenTree::Subtree(it) => it.delimiter.close.range.end(),
623-
};
614+
let start = first_span.range.start();
615+
let end = last_span.range.end();
624616
TextRange::new(start, end)
625617
}
626618
}

0 commit comments

Comments
 (0)