Skip to content

Commit 9aa6be7

Browse files
committed
internal: remove useless helpers
We generally avoid "syntax only" helper wrappers, which don't do much: they make code easier to write, but harder to read. They also make investigations harder, as "find_usages" needs to be invoked both for the wrapped and unwrapped APIs
1 parent 977fef7 commit 9aa6be7

File tree

12 files changed

+32
-36
lines changed

12 files changed

+32
-36
lines changed

crates/cfg/src/tests.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use expect_test::{expect, Expect};
2-
use mbe::ast_to_token_tree;
2+
use mbe::syntax_node_to_token_tree;
33
use syntax::{ast, AstNode};
44

55
use crate::{CfgAtom, CfgExpr, CfgOptions, DnfExpr};
@@ -8,7 +8,7 @@ fn assert_parse_result(input: &str, expected: CfgExpr) {
88
let (tt, _) = {
99
let source_file = ast::SourceFile::parse(input).ok().unwrap();
1010
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
11-
ast_to_token_tree(&tt)
11+
syntax_node_to_token_tree(tt.syntax())
1212
};
1313
let cfg = CfgExpr::parse(&tt);
1414
assert_eq!(cfg, expected);
@@ -18,7 +18,7 @@ fn check_dnf(input: &str, expect: Expect) {
1818
let (tt, _) = {
1919
let source_file = ast::SourceFile::parse(input).ok().unwrap();
2020
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
21-
ast_to_token_tree(&tt)
21+
syntax_node_to_token_tree(tt.syntax())
2222
};
2323
let cfg = CfgExpr::parse(&tt);
2424
let actual = format!("#![cfg({})]", DnfExpr::new(cfg));
@@ -29,7 +29,7 @@ fn check_why_inactive(input: &str, opts: &CfgOptions, expect: Expect) {
2929
let (tt, _) = {
3030
let source_file = ast::SourceFile::parse(input).ok().unwrap();
3131
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
32-
ast_to_token_tree(&tt)
32+
syntax_node_to_token_tree(tt.syntax())
3333
};
3434
let cfg = CfgExpr::parse(&tt);
3535
let dnf = DnfExpr::new(cfg);
@@ -42,7 +42,7 @@ fn check_enable_hints(input: &str, opts: &CfgOptions, expected_hints: &[&str]) {
4242
let (tt, _) = {
4343
let source_file = ast::SourceFile::parse(input).ok().unwrap();
4444
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
45-
ast_to_token_tree(&tt)
45+
syntax_node_to_token_tree(tt.syntax())
4646
};
4747
let cfg = CfgExpr::parse(&tt);
4848
let dnf = DnfExpr::new(cfg);

crates/hir_def/src/attr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use either::Either;
1212
use hir_expand::{hygiene::Hygiene, name::AsName, AstId, InFile};
1313
use itertools::Itertools;
1414
use la_arena::ArenaMap;
15-
use mbe::{ast_to_token_tree, DelimiterKind};
15+
use mbe::{syntax_node_to_token_tree, DelimiterKind};
1616
use smallvec::{smallvec, SmallVec};
1717
use syntax::{
1818
ast::{self, AstNode, AttrsOwner},
@@ -679,7 +679,7 @@ impl Attr {
679679
};
680680
Some(Interned::new(AttrInput::Literal(value)))
681681
} else if let Some(tt) = ast.token_tree() {
682-
Some(Interned::new(AttrInput::TokenTree(ast_to_token_tree(&tt).0)))
682+
Some(Interned::new(AttrInput::TokenTree(syntax_node_to_token_tree(tt.syntax()).0)))
683683
} else {
684684
None
685685
};

crates/hir_expand/src/builtin_macro.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ mod tests {
610610

611611
let fragment = crate::to_fragment_kind(&macro_call);
612612
let args = macro_call.token_tree().unwrap();
613-
let parsed_args = mbe::ast_to_token_tree(&args).0;
613+
let parsed_args = mbe::syntax_node_to_token_tree(args.syntax()).0;
614614
let call_id = AstId::new(file_id.into(), ast_id_map.ast_id(&macro_call));
615615

616616
let arg_id = db.intern_macro(MacroCallLoc {

crates/hir_expand/src/db.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<TokenExpander>>
281281
MacroDefKind::Declarative(ast_id) => match ast_id.to_node(db) {
282282
ast::Macro::MacroRules(macro_rules) => {
283283
let arg = macro_rules.token_tree()?;
284-
let (tt, def_site_token_map) = mbe::ast_to_token_tree(&arg);
284+
let (tt, def_site_token_map) = mbe::syntax_node_to_token_tree(arg.syntax());
285285
let mac = match mbe::MacroRules::parse(&tt) {
286286
Ok(it) => it,
287287
Err(err) => {
@@ -294,7 +294,7 @@ fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<TokenExpander>>
294294
}
295295
ast::Macro::MacroDef(macro_def) => {
296296
let arg = macro_def.body()?;
297-
let (tt, def_site_token_map) = mbe::ast_to_token_tree(&arg);
297+
let (tt, def_site_token_map) = mbe::syntax_node_to_token_tree(arg.syntax());
298298
let mac = match mbe::MacroDef::parse(&tt) {
299299
Ok(it) => it,
300300
Err(err) => {

crates/hir_expand/src/eager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ pub fn expand_eager_macro(
107107
mut diagnostic_sink: &mut dyn FnMut(mbe::ExpandError),
108108
) -> Result<MacroCallId, ErrorEmitted> {
109109
let parsed_args = diagnostic_sink.option_with(
110-
|| Some(mbe::ast_to_token_tree(&macro_call.value.token_tree()?).0),
110+
|| Some(mbe::syntax_node_to_token_tree(&macro_call.value.token_tree()?.syntax()).0),
111111
|| err("malformed macro invocation"),
112112
)?;
113113

crates/mbe/src/benchmark.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ use syntax::{
88
use test_utils::{bench, bench_fixture, skip_slow_tests};
99

1010
use crate::{
11-
ast_to_token_tree,
1211
parser::{Op, RepeatKind, Separator},
13-
MacroRules,
12+
syntax_node_to_token_tree, MacroRules,
1413
};
1514

1615
#[test]
@@ -65,7 +64,7 @@ fn macro_rules_fixtures_tt() -> FxHashMap<String, tt::Subtree> {
6564
.filter_map(ast::MacroRules::cast)
6665
.map(|rule| {
6766
let id = rule.name().unwrap().to_string();
68-
let (def_tt, _) = ast_to_token_tree(&rule.token_tree().unwrap());
67+
let (def_tt, _) = syntax_node_to_token_tree(rule.token_tree().unwrap().syntax());
6968
(id, def_tt)
7069
})
7170
.collect()

crates/mbe/src/expander.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ mod tests {
120120
use syntax::{ast, AstNode};
121121

122122
use super::*;
123-
use crate::ast_to_token_tree;
123+
use crate::syntax_node_to_token_tree;
124124

125125
#[test]
126126
fn test_expand_rule() {
@@ -159,7 +159,8 @@ mod tests {
159159
let macro_definition =
160160
source_file.syntax().descendants().find_map(ast::MacroRules::cast).unwrap();
161161

162-
let (definition_tt, _) = ast_to_token_tree(&macro_definition.token_tree().unwrap());
162+
let (definition_tt, _) =
163+
syntax_node_to_token_tree(macro_definition.token_tree().unwrap().syntax());
163164
crate::MacroRules::parse(&definition_tt).unwrap()
164165
}
165166

@@ -168,7 +169,8 @@ mod tests {
168169
let macro_invocation =
169170
source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
170171

171-
let (invocation_tt, _) = ast_to_token_tree(&macro_invocation.token_tree().unwrap());
172+
let (invocation_tt, _) =
173+
syntax_node_to_token_tree(macro_invocation.token_tree().unwrap().syntax());
172174

173175
expand_rules(&rules.rules, &invocation_tt)
174176
}

crates/mbe/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl fmt::Display for ExpandError {
6666

6767
pub use crate::{
6868
syntax_bridge::{
69-
ast_to_token_tree, parse_exprs_with_sep, parse_to_token_tree, syntax_node_to_token_tree,
69+
parse_exprs_with_sep, parse_to_token_tree, syntax_node_to_token_tree,
7070
token_tree_to_syntax_node,
7171
},
7272
token_map::TokenMap,

crates/mbe/src/syntax_bridge.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@ use tt::buffer::{Cursor, TokenBuffer};
1313
use crate::{subtree_source::SubtreeTokenSource, tt_iter::TtIter};
1414
use crate::{ExpandError, TokenMap};
1515

16-
/// Convert the syntax tree (what user has written) to a `TokenTree` (what macro
17-
/// will consume).
18-
pub fn ast_to_token_tree(ast: &impl ast::AstNode) -> (tt::Subtree, TokenMap) {
19-
syntax_node_to_token_tree(ast.syntax())
20-
}
21-
2216
/// Convert the syntax node to a `TokenTree` (what macro
2317
/// will consume).
2418
pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> (tt::Subtree, TokenMap) {
@@ -812,7 +806,7 @@ mod tests {
812806
// - T!['}']
813807
// - WHITE_SPACE
814808
let token_tree = ast::TokenTree::cast(token_tree).unwrap();
815-
let tt = ast_to_token_tree(&token_tree).0;
809+
let tt = syntax_node_to_token_tree(token_tree.syntax()).0;
816810

817811
assert_eq!(tt.delimiter_kind(), Some(tt::DelimiterKind::Brace));
818812
}
@@ -821,15 +815,15 @@ mod tests {
821815
fn test_token_tree_multi_char_punct() {
822816
let source_file = ast::SourceFile::parse("struct Foo { a: x::Y }").ok().unwrap();
823817
let struct_def = source_file.syntax().descendants().find_map(ast::Struct::cast).unwrap();
824-
let tt = ast_to_token_tree(&struct_def).0;
818+
let tt = syntax_node_to_token_tree(struct_def.syntax()).0;
825819
token_tree_to_syntax_node(&tt, FragmentKind::Item).unwrap();
826820
}
827821

828822
#[test]
829823
fn test_missing_closing_delim() {
830824
let source_file = ast::SourceFile::parse("m!(x").tree();
831825
let node = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
832-
let tt = ast_to_token_tree(&node).0.to_string();
826+
let tt = syntax_node_to_token_tree(node.syntax()).0.to_string();
833827
assert_eq_text!(&*tt, "( x");
834828
}
835829
}

crates/mbe/src/tests.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ macro_rules! impl_fixture {
2929
let macro_invocation =
3030
source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
3131

32-
let (invocation_tt, _) = ast_to_token_tree(&macro_invocation.token_tree().unwrap());
32+
let (invocation_tt, _) =
33+
syntax_node_to_token_tree(macro_invocation.token_tree().unwrap().syntax());
3334

3435
self.rules.expand(&invocation_tt).result()
3536
}
@@ -100,7 +101,7 @@ macro_rules! impl_fixture {
100101
.descendants()
101102
.find_map(ast::TokenTree::cast)
102103
.unwrap();
103-
let mut wrapped = ast_to_token_tree(&wrapped).0;
104+
let mut wrapped = syntax_node_to_token_tree(wrapped.syntax()).0;
104105
wrapped.delimiter = None;
105106
wrapped
106107
};
@@ -163,7 +164,8 @@ fn parse_macro_rules_to_tt(ra_fixture: &str) -> tt::Subtree {
163164
let macro_definition =
164165
source_file.syntax().descendants().find_map(ast::MacroRules::cast).unwrap();
165166

166-
let (definition_tt, _) = ast_to_token_tree(&macro_definition.token_tree().unwrap());
167+
let (definition_tt, _) =
168+
syntax_node_to_token_tree(macro_definition.token_tree().unwrap().syntax());
167169

168170
let parsed = parse_to_token_tree(
169171
&ra_fixture[macro_definition.token_tree().unwrap().syntax().text_range()],
@@ -180,7 +182,7 @@ fn parse_macro_def_to_tt(ra_fixture: &str) -> tt::Subtree {
180182
let macro_definition =
181183
source_file.syntax().descendants().find_map(ast::MacroDef::cast).unwrap();
182184

183-
let (definition_tt, _) = ast_to_token_tree(&macro_definition.body().unwrap());
185+
let (definition_tt, _) = syntax_node_to_token_tree(macro_definition.body().unwrap().syntax());
184186

185187
let parsed =
186188
parse_to_token_tree(&ra_fixture[macro_definition.body().unwrap().syntax().text_range()])

0 commit comments

Comments
 (0)