Skip to content

Commit 3abcdc0

Browse files
Make ast_to_token_tree infallible
It could never return `None`, so reflect that in the return type
1 parent bcf600f commit 3abcdc0

File tree

11 files changed

+32
-41
lines changed

11 files changed

+32
-41
lines changed

crates/cfg/src/tests.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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).unwrap()
11+
ast_to_token_tree(&tt)
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).unwrap()
21+
ast_to_token_tree(&tt)
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).unwrap()
32+
ast_to_token_tree(&tt)
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).unwrap()
45+
ast_to_token_tree(&tt)
4646
};
4747
let cfg = CfgExpr::parse(&tt);
4848
let dnf = DnfExpr::new(cfg);

crates/hir_def/src/attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ impl Attr {
533533
};
534534
Some(AttrInput::Literal(value))
535535
} else if let Some(tt) = ast.token_tree() {
536-
Some(AttrInput::TokenTree(ast_to_token_tree(&tt)?.0))
536+
Some(AttrInput::TokenTree(ast_to_token_tree(&tt).0))
537537
} else {
538538
None
539539
};

crates/hir_expand/src/builtin_macro.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ mod tests {
584584
};
585585

586586
let args = macro_call.token_tree().unwrap();
587-
let parsed_args = mbe::ast_to_token_tree(&args).unwrap().0;
587+
let parsed_args = mbe::ast_to_token_tree(&args).0;
588588
let call_id = AstId::new(file_id.into(), ast_id_map.ast_id(&macro_call));
589589

590590
let arg_id = db.intern_eager_expansion({

crates/hir_expand/src/db.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ pub fn expand_hypothetical(
119119
token_to_map: syntax::SyntaxToken,
120120
) -> Option<(SyntaxNode, syntax::SyntaxToken)> {
121121
let macro_file = MacroFile { macro_call_id: actual_macro_call };
122-
let (tt, tmap_1) = mbe::syntax_node_to_token_tree(hypothetical_args.syntax()).unwrap();
122+
let (tt, tmap_1) = mbe::syntax_node_to_token_tree(hypothetical_args.syntax());
123123
let range =
124124
token_to_map.text_range().checked_sub(hypothetical_args.syntax().text_range().start())?;
125125
let token_id = tmap_1.token_by_range(range)?;
@@ -143,10 +143,7 @@ fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<(TokenExpander,
143143
MacroDefKind::Declarative(ast_id) => match ast_id.to_node(db) {
144144
syntax::ast::Macro::MacroRules(macro_rules) => {
145145
let arg = macro_rules.token_tree()?;
146-
let (tt, tmap) = mbe::ast_to_token_tree(&arg).or_else(|| {
147-
log::warn!("fail on macro_rules to token tree: {:#?}", arg);
148-
None
149-
})?;
146+
let (tt, tmap) = mbe::ast_to_token_tree(&arg);
150147
let rules = match MacroRules::parse(&tt) {
151148
Ok(it) => it,
152149
Err(err) => {
@@ -159,10 +156,7 @@ fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<(TokenExpander,
159156
}
160157
syntax::ast::Macro::MacroDef(macro_def) => {
161158
let arg = macro_def.body()?;
162-
let (tt, tmap) = mbe::ast_to_token_tree(&arg).or_else(|| {
163-
log::warn!("fail on macro_def to token tree: {:#?}", arg);
164-
None
165-
})?;
159+
let (tt, tmap) = mbe::ast_to_token_tree(&arg);
166160
let rules = match MacroDef::parse(&tt) {
167161
Ok(it) => it,
168162
Err(err) => {
@@ -202,7 +196,7 @@ fn macro_arg_text(db: &dyn AstDatabase, id: MacroCallId) -> Option<GreenNode> {
202196

203197
fn macro_arg(db: &dyn AstDatabase, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>> {
204198
let arg = db.macro_arg_text(id)?;
205-
let (tt, tmap) = mbe::syntax_node_to_token_tree(&SyntaxNode::new_root(arg))?;
199+
let (tt, tmap) = mbe::syntax_node_to_token_tree(&SyntaxNode::new_root(arg));
206200
Some(Arc::new((tt, tmap)))
207201
}
208202

crates/hir_expand/src/eager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ pub fn expand_eager_macro(
106106
mut diagnostic_sink: &mut dyn FnMut(mbe::ExpandError),
107107
) -> Result<EagerMacroId, ErrorEmitted> {
108108
let parsed_args = diagnostic_sink.option_with(
109-
|| Some(mbe::ast_to_token_tree(&macro_call.value.token_tree()?)?.0),
109+
|| Some(mbe::ast_to_token_tree(&macro_call.value.token_tree()?).0),
110110
|| err("malformed macro invocation"),
111111
)?;
112112

@@ -161,7 +161,7 @@ pub fn expand_eager_macro(
161161
}
162162

163163
fn to_subtree(node: &SyntaxNode) -> Option<tt::Subtree> {
164-
let mut subtree = mbe::syntax_node_to_token_tree(node)?.0;
164+
let mut subtree = mbe::syntax_node_to_token_tree(node).0;
165165
subtree.delimiter = None;
166166
Some(subtree)
167167
}

crates/mbe/src/benchmark.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ fn macro_rules_fixtures_tt() -> FxHashMap<String, tt::Subtree> {
6565
.filter_map(ast::MacroRules::cast)
6666
.map(|rule| {
6767
let id = rule.name().unwrap().to_string();
68-
let (def_tt, _) = ast_to_token_tree(&rule.token_tree().unwrap()).unwrap();
68+
let (def_tt, _) = ast_to_token_tree(&rule.token_tree().unwrap());
6969
(id, def_tt)
7070
})
7171
.collect()

crates/mbe/src/expander.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ mod tests {
159159
let macro_definition =
160160
source_file.syntax().descendants().find_map(ast::MacroRules::cast).unwrap();
161161

162-
let (definition_tt, _) =
163-
ast_to_token_tree(&macro_definition.token_tree().unwrap()).unwrap();
162+
let (definition_tt, _) = ast_to_token_tree(&macro_definition.token_tree().unwrap());
164163
crate::MacroRules::parse(&definition_tt).unwrap()
165164
}
166165

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

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

175173
expand_rules(&rules.rules, &invocation_tt)
176174
}

crates/mbe/src/syntax_bridge.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,18 @@ pub struct TokenMap {
4343

4444
/// Convert the syntax tree (what user has written) to a `TokenTree` (what macro
4545
/// will consume).
46-
pub fn ast_to_token_tree(ast: &impl ast::AstNode) -> Option<(tt::Subtree, TokenMap)> {
46+
pub fn ast_to_token_tree(ast: &impl ast::AstNode) -> (tt::Subtree, TokenMap) {
4747
syntax_node_to_token_tree(ast.syntax())
4848
}
4949

5050
/// Convert the syntax node to a `TokenTree` (what macro
5151
/// will consume).
52-
pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> Option<(tt::Subtree, TokenMap)> {
52+
pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> (tt::Subtree, TokenMap) {
5353
let global_offset = node.text_range().start();
5454
let mut c = Convertor::new(node, global_offset);
55-
let subtree = c.go()?;
55+
let subtree = c.go();
5656
c.id_alloc.map.entries.shrink_to_fit();
57-
Some((subtree, c.id_alloc.map))
57+
(subtree, c.id_alloc.map)
5858
}
5959

6060
// The following items are what `rustc` macro can be parsed into :
@@ -108,7 +108,7 @@ pub fn parse_to_token_tree(text: &str) -> Option<(tt::Subtree, TokenMap)> {
108108
},
109109
};
110110

111-
let subtree = conv.go()?;
111+
let subtree = conv.go();
112112
Some((subtree, conv.id_alloc.map))
113113
}
114114

@@ -319,18 +319,18 @@ trait SrcToken: std::fmt::Debug {
319319
trait TokenConvertor {
320320
type Token: SrcToken;
321321

322-
fn go(&mut self) -> Option<tt::Subtree> {
322+
fn go(&mut self) -> tt::Subtree {
323323
let mut subtree = tt::Subtree::default();
324324
subtree.delimiter = None;
325325
while self.peek().is_some() {
326326
self.collect_leaf(&mut subtree.token_trees);
327327
}
328328
if subtree.token_trees.len() == 1 {
329329
if let tt::TokenTree::Subtree(first) = &subtree.token_trees[0] {
330-
return Some(first.clone());
330+
return first.clone();
331331
}
332332
}
333-
Some(subtree)
333+
subtree
334334
}
335335

336336
fn collect_leaf(&mut self, result: &mut Vec<tt::TokenTree>) {
@@ -858,7 +858,7 @@ mod tests {
858858
// - T!['}']
859859
// - WHITE_SPACE
860860
let token_tree = ast::TokenTree::cast(token_tree).unwrap();
861-
let tt = ast_to_token_tree(&token_tree).unwrap().0;
861+
let tt = ast_to_token_tree(&token_tree).0;
862862

863863
assert_eq!(tt.delimiter_kind(), Some(tt::DelimiterKind::Brace));
864864
}
@@ -867,7 +867,7 @@ mod tests {
867867
fn test_token_tree_multi_char_punct() {
868868
let source_file = ast::SourceFile::parse("struct Foo { a: x::Y }").ok().unwrap();
869869
let struct_def = source_file.syntax().descendants().find_map(ast::Struct::cast).unwrap();
870-
let tt = ast_to_token_tree(&struct_def).unwrap().0;
870+
let tt = ast_to_token_tree(&struct_def).0;
871871
token_tree_to_syntax_node(&tt, FragmentKind::Item).unwrap();
872872
}
873873
}

crates/mbe/src/tests.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ 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())
33-
.ok_or_else(|| ExpandError::ConversionError)?;
32+
let (invocation_tt, _) = ast_to_token_tree(&macro_invocation.token_tree().unwrap());
3433

3534
self.rules.expand(&invocation_tt).result()
3635
}
@@ -101,7 +100,7 @@ macro_rules! impl_fixture {
101100
.descendants()
102101
.find_map(ast::TokenTree::cast)
103102
.unwrap();
104-
let mut wrapped = ast_to_token_tree(&wrapped).unwrap().0;
103+
let mut wrapped = ast_to_token_tree(&wrapped).0;
105104
wrapped.delimiter = None;
106105
wrapped
107106
};
@@ -151,7 +150,7 @@ pub(crate) fn parse_macro_error(ra_fixture: &str) -> ParseError {
151150

152151
pub(crate) fn parse_to_token_tree_by_syntax(ra_fixture: &str) -> tt::Subtree {
153152
let source_file = ast::SourceFile::parse(ra_fixture).ok().unwrap();
154-
let tt = syntax_node_to_token_tree(source_file.syntax()).unwrap().0;
153+
let tt = syntax_node_to_token_tree(source_file.syntax()).0;
155154

156155
let parsed = parse_to_token_tree(ra_fixture).unwrap().0;
157156
assert_eq!(tt, parsed);
@@ -164,7 +163,7 @@ fn parse_macro_rules_to_tt(ra_fixture: &str) -> tt::Subtree {
164163
let macro_definition =
165164
source_file.syntax().descendants().find_map(ast::MacroRules::cast).unwrap();
166165

167-
let (definition_tt, _) = ast_to_token_tree(&macro_definition.token_tree().unwrap()).unwrap();
166+
let (definition_tt, _) = ast_to_token_tree(&macro_definition.token_tree().unwrap());
168167

169168
let parsed = parse_to_token_tree(
170169
&ra_fixture[macro_definition.token_tree().unwrap().syntax().text_range()],
@@ -181,7 +180,7 @@ fn parse_macro_def_to_tt(ra_fixture: &str) -> tt::Subtree {
181180
let macro_definition =
182181
source_file.syntax().descendants().find_map(ast::MacroDef::cast).unwrap();
183182

184-
let (definition_tt, _) = ast_to_token_tree(&macro_definition.body().unwrap()).unwrap();
183+
let (definition_tt, _) = ast_to_token_tree(&macro_definition.body().unwrap());
185184

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

crates/mbe/src/tests/rule.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ fn parse_macro_arm(arm_definition: &str) -> Result<crate::MacroRules, ParseError
4444
let macro_definition =
4545
source_file.syntax().descendants().find_map(ast::MacroRules::cast).unwrap();
4646

47-
let (definition_tt, _) = ast_to_token_tree(&macro_definition.token_tree().unwrap()).unwrap();
47+
let (definition_tt, _) = ast_to_token_tree(&macro_definition.token_tree().unwrap());
4848
crate::MacroRules::parse(&definition_tt)
4949
}

0 commit comments

Comments
 (0)