Skip to content

Commit 5fb8c0d

Browse files
committed
Remove MappedSubtree
1 parent 177c701 commit 5fb8c0d

File tree

7 files changed

+21
-41
lines changed

7 files changed

+21
-41
lines changed

crates/hir_def/src/attr.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use either::Either;
1414
use hir_expand::{hygiene::Hygiene, name::AsName, AstId, InFile};
1515
use itertools::Itertools;
1616
use la_arena::ArenaMap;
17-
use mbe::{syntax_node_to_token_tree, DelimiterKind, MappedSubTree};
17+
use mbe::{syntax_node_to_token_tree, DelimiterKind};
1818
use smallvec::{smallvec, SmallVec};
1919
use syntax::{
2020
ast::{self, AstNode, AttrsOwner},
@@ -160,18 +160,18 @@ impl RawAttrs {
160160
}
161161

162162
let subtree = match attr.input.as_deref() {
163-
Some(AttrInput::TokenTree(it)) => it,
163+
Some(AttrInput::TokenTree(it, _)) => it,
164164
_ => return smallvec![attr.clone()],
165165
};
166166

167167
// Input subtree is: `(cfg, $(attr),+)`
168168
// Split it up into a `cfg` subtree and the `attr` subtrees.
169169
// FIXME: There should be a common API for this.
170-
let mut parts = subtree.tree.token_trees.split(
170+
let mut parts = subtree.token_trees.split(
171171
|tt| matches!(tt, tt::TokenTree::Leaf(tt::Leaf::Punct(p)) if p.char == ','),
172172
);
173173
let cfg = parts.next().unwrap();
174-
let cfg = Subtree { delimiter: subtree.tree.delimiter, token_trees: cfg.to_vec() };
174+
let cfg = Subtree { delimiter: subtree.delimiter, token_trees: cfg.to_vec() };
175175
let cfg = CfgExpr::parse(&cfg);
176176
let index = attr.id;
177177
let attrs = parts.filter(|a| !a.is_empty()).filter_map(|attr| {
@@ -260,7 +260,7 @@ impl Attrs {
260260
pub fn docs(&self) -> Option<Documentation> {
261261
let docs = self.by_key("doc").attrs().flat_map(|attr| match attr.input.as_deref()? {
262262
AttrInput::Literal(s) => Some(s),
263-
AttrInput::TokenTree(_) => None,
263+
AttrInput::TokenTree(..) => None,
264264
});
265265
let indent = docs
266266
.clone()
@@ -465,7 +465,7 @@ impl AttrsWithOwner {
465465
// FIXME: code duplication in `docs` above
466466
let docs = self.by_key("doc").attrs().flat_map(|attr| match attr.input.as_deref()? {
467467
AttrInput::Literal(s) => Some((s, attr.id)),
468-
AttrInput::TokenTree(_) => None,
468+
AttrInput::TokenTree(..) => None,
469469
});
470470
let indent = docs
471471
.clone()
@@ -654,14 +654,14 @@ pub enum AttrInput {
654654
/// `#[attr = "string"]`
655655
Literal(SmolStr),
656656
/// `#[attr(subtree)]`
657-
TokenTree(mbe::MappedSubTree),
657+
TokenTree(tt::Subtree, mbe::TokenMap),
658658
}
659659

660660
impl fmt::Display for AttrInput {
661661
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
662662
match self {
663663
AttrInput::Literal(lit) => write!(f, " = \"{}\"", lit.escape_debug()),
664-
AttrInput::TokenTree(subtree) => subtree.tree.fmt(f),
664+
AttrInput::TokenTree(subtree, _) => subtree.fmt(f),
665665
}
666666
}
667667
}
@@ -682,7 +682,7 @@ impl Attr {
682682
Some(Interned::new(AttrInput::Literal(value)))
683683
} else if let Some(tt) = ast.token_tree() {
684684
let (tree, map) = syntax_node_to_token_tree(tt.syntax());
685-
Some(Interned::new(AttrInput::TokenTree(MappedSubTree { tree, map })))
685+
Some(Interned::new(AttrInput::TokenTree(tree, map)))
686686
} else {
687687
None
688688
};
@@ -712,10 +712,9 @@ impl Attr {
712712
}
713713

714714
match self.input.as_deref() {
715-
Some(AttrInput::TokenTree(args)) => {
715+
Some(AttrInput::TokenTree(args, _)) => {
716716
let mut counter = 0;
717717
let paths = args
718-
.tree
719718
.token_trees
720719
.iter()
721720
.group_by(move |tt| {
@@ -760,7 +759,7 @@ pub struct AttrQuery<'a> {
760759
impl<'a> AttrQuery<'a> {
761760
pub fn tt_values(self) -> impl Iterator<Item = &'a Subtree> {
762761
self.attrs().filter_map(|attr| match attr.input.as_deref()? {
763-
AttrInput::TokenTree(it) => Some(&it.tree),
762+
AttrInput::TokenTree(it, _) => Some(it),
764763
_ => None,
765764
})
766765
}

crates/hir_def/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -787,12 +787,12 @@ fn attr_macro_as_call_id(
787787
let mut arg = match &macro_attr.input {
788788
Some(input) => match &**input {
789789
attr::AttrInput::Literal(_) => Default::default(),
790-
attr::AttrInput::TokenTree(tt) => tt.clone(),
790+
attr::AttrInput::TokenTree(tt, map) => (tt.clone(), map.clone()),
791791
},
792792
None => Default::default(),
793793
};
794794
// The parentheses are always disposed here.
795-
arg.tree.delimiter = None;
795+
arg.0.delimiter = None;
796796

797797
let res = def.as_lazy_macro(
798798
db.upcast(),

crates/hir_def/src/nameres/collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ impl DefCollector<'_> {
289289
|| *attr_name == hir_expand::name![register_tool]
290290
{
291291
match attr.input.as_deref() {
292-
Some(AttrInput::TokenTree(subtree)) => match &*subtree.tree.token_trees {
292+
Some(AttrInput::TokenTree(subtree, _)) => match &*subtree.token_trees {
293293
[tt::TokenTree::Leaf(tt::Leaf::Ident(name))] => name.as_name(),
294294
_ => continue,
295295
},

crates/hir_expand/src/db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ fn expand_proc_macro(db: &dyn AstDatabase, id: MacroCallId) -> ExpandResult<tt::
387387

388388
let attr_arg = match &loc.kind {
389389
MacroCallKind::Attr { attr_args, .. } => {
390-
let mut attr_args = attr_args.tree.clone();
390+
let mut attr_args = attr_args.0.clone();
391391
mbe::Shift::new(&macro_arg.0).shift_all(&mut attr_args);
392392
Some(attr_args)
393393
}

crates/hir_expand/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ pub enum MacroCallKind {
283283
Attr {
284284
ast_id: AstId<ast::Item>,
285285
attr_name: String,
286-
attr_args: mbe::MappedSubTree,
286+
attr_args: (tt::Subtree, mbe::TokenMap),
287287
/// Syntactical index of the invoking `#[attribute]`.
288288
///
289289
/// Outer attributes are counted first, then inner attributes. This does not support
@@ -390,7 +390,7 @@ impl ExpansionInfo {
390390
token_tree.left_delimiter_token()?.text_range().start();
391391
let range = token.value.text_range().checked_sub(attr_input_start)?;
392392
let token_id =
393-
self.macro_arg_shift.shift(attr_args.map.token_by_range(range)?);
393+
self.macro_arg_shift.shift(attr_args.1.token_by_range(range)?);
394394
Some(token_id)
395395
}
396396
_ => None,
@@ -437,7 +437,7 @@ impl ExpansionInfo {
437437
MacroCallKind::Attr { attr_args, .. } => match self.macro_arg_shift.unshift(token_id) {
438438
Some(unshifted) => {
439439
token_id = unshifted;
440-
(&attr_args.map, self.attr_input_or_mac_def.clone()?.syntax().cloned())
440+
(&attr_args.1, self.attr_input_or_mac_def.clone()?.syntax().cloned())
441441
}
442442
None => (&self.macro_arg.1, self.arg.clone()),
443443
},

crates/mbe/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub use crate::{
6969
parse_exprs_with_sep, parse_to_token_tree, syntax_node_to_token_tree,
7070
token_tree_to_syntax_node,
7171
},
72-
token_map::{MappedSubTree, TokenMap},
72+
token_map::TokenMap,
7373
};
7474

7575
/// This struct contains AST for a single `macro_rules` definition. What might

crates/mbe/src/token_map.rs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::hash::Hash;
55
use parser::{SyntaxKind, T};
66
use syntax::{TextRange, TextSize};
77

8-
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
8+
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
99
enum TokenTextRange {
1010
Token(TextRange),
1111
Delimiter(TextRange),
@@ -26,27 +26,8 @@ impl TokenTextRange {
2626
}
2727
}
2828

29-
#[derive(Debug, Clone, Default)]
30-
pub struct MappedSubTree {
31-
pub tree: tt::Subtree,
32-
pub map: TokenMap,
33-
}
34-
35-
impl Eq for MappedSubTree {}
36-
impl PartialEq for MappedSubTree {
37-
fn eq(&self, other: &Self) -> bool {
38-
self.tree == other.tree && self.map == other.map
39-
}
40-
}
41-
42-
impl Hash for MappedSubTree {
43-
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
44-
self.tree.hash(state);
45-
}
46-
}
47-
4829
/// Maps `tt::TokenId` to the relative range of the original token.
49-
#[derive(Debug, PartialEq, Eq, Clone, Default)]
30+
#[derive(Debug, PartialEq, Eq, Clone, Default, Hash)]
5031
pub struct TokenMap {
5132
/// Maps `tt::TokenId` to the *relative* source range.
5233
entries: Vec<(tt::TokenId, TokenTextRange)>,

0 commit comments

Comments
 (0)