From 3abcdc03ba335fb3487c62547f61746e4a199fe6 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Sun, 4 Apr 2021 01:46:45 +0200 Subject: Make `ast_to_token_tree` infallible It could never return `None`, so reflect that in the return type --- crates/mbe/src/benchmark.rs | 2 +- crates/mbe/src/expander.rs | 6 ++---- crates/mbe/src/syntax_bridge.rs | 20 ++++++++++---------- crates/mbe/src/tests.rs | 11 +++++------ crates/mbe/src/tests/rule.rs | 2 +- 5 files changed, 19 insertions(+), 22 deletions(-) (limited to 'crates/mbe/src') diff --git a/crates/mbe/src/benchmark.rs b/crates/mbe/src/benchmark.rs index ba814a2e1..38707ffa5 100644 --- a/crates/mbe/src/benchmark.rs +++ b/crates/mbe/src/benchmark.rs @@ -65,7 +65,7 @@ fn macro_rules_fixtures_tt() -> FxHashMap { .filter_map(ast::MacroRules::cast) .map(|rule| { let id = rule.name().unwrap().to_string(); - let (def_tt, _) = ast_to_token_tree(&rule.token_tree().unwrap()).unwrap(); + let (def_tt, _) = ast_to_token_tree(&rule.token_tree().unwrap()); (id, def_tt) }) .collect() diff --git a/crates/mbe/src/expander.rs b/crates/mbe/src/expander.rs index 3197c834c..bfef7f73d 100644 --- a/crates/mbe/src/expander.rs +++ b/crates/mbe/src/expander.rs @@ -159,8 +159,7 @@ mod tests { let macro_definition = source_file.syntax().descendants().find_map(ast::MacroRules::cast).unwrap(); - let (definition_tt, _) = - ast_to_token_tree(¯o_definition.token_tree().unwrap()).unwrap(); + let (definition_tt, _) = ast_to_token_tree(¯o_definition.token_tree().unwrap()); crate::MacroRules::parse(&definition_tt).unwrap() } @@ -169,8 +168,7 @@ mod tests { let macro_invocation = source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); - let (invocation_tt, _) = - ast_to_token_tree(¯o_invocation.token_tree().unwrap()).unwrap(); + let (invocation_tt, _) = ast_to_token_tree(¯o_invocation.token_tree().unwrap()); expand_rules(&rules.rules, &invocation_tt) } diff --git a/crates/mbe/src/syntax_bridge.rs b/crates/mbe/src/syntax_bridge.rs index ae0780072..9ba98f7fb 100644 --- a/crates/mbe/src/syntax_bridge.rs +++ b/crates/mbe/src/syntax_bridge.rs @@ -43,18 +43,18 @@ pub struct TokenMap { /// Convert the syntax tree (what user has written) to a `TokenTree` (what macro /// will consume). -pub fn ast_to_token_tree(ast: &impl ast::AstNode) -> Option<(tt::Subtree, TokenMap)> { +pub fn ast_to_token_tree(ast: &impl ast::AstNode) -> (tt::Subtree, TokenMap) { syntax_node_to_token_tree(ast.syntax()) } /// Convert the syntax node to a `TokenTree` (what macro /// will consume). -pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> Option<(tt::Subtree, TokenMap)> { +pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> (tt::Subtree, TokenMap) { let global_offset = node.text_range().start(); let mut c = Convertor::new(node, global_offset); - let subtree = c.go()?; + let subtree = c.go(); c.id_alloc.map.entries.shrink_to_fit(); - Some((subtree, c.id_alloc.map)) + (subtree, c.id_alloc.map) } // 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)> { }, }; - let subtree = conv.go()?; + let subtree = conv.go(); Some((subtree, conv.id_alloc.map)) } @@ -319,7 +319,7 @@ trait SrcToken: std::fmt::Debug { trait TokenConvertor { type Token: SrcToken; - fn go(&mut self) -> Option { + fn go(&mut self) -> tt::Subtree { let mut subtree = tt::Subtree::default(); subtree.delimiter = None; while self.peek().is_some() { @@ -327,10 +327,10 @@ trait TokenConvertor { } if subtree.token_trees.len() == 1 { if let tt::TokenTree::Subtree(first) = &subtree.token_trees[0] { - return Some(first.clone()); + return first.clone(); } } - Some(subtree) + subtree } fn collect_leaf(&mut self, result: &mut Vec) { @@ -858,7 +858,7 @@ mod tests { // - T!['}'] // - WHITE_SPACE let token_tree = ast::TokenTree::cast(token_tree).unwrap(); - let tt = ast_to_token_tree(&token_tree).unwrap().0; + let tt = ast_to_token_tree(&token_tree).0; assert_eq!(tt.delimiter_kind(), Some(tt::DelimiterKind::Brace)); } @@ -867,7 +867,7 @@ mod tests { fn test_token_tree_multi_char_punct() { let source_file = ast::SourceFile::parse("struct Foo { a: x::Y }").ok().unwrap(); let struct_def = source_file.syntax().descendants().find_map(ast::Struct::cast).unwrap(); - let tt = ast_to_token_tree(&struct_def).unwrap().0; + let tt = ast_to_token_tree(&struct_def).0; token_tree_to_syntax_node(&tt, FragmentKind::Item).unwrap(); } } diff --git a/crates/mbe/src/tests.rs b/crates/mbe/src/tests.rs index 6da18ecf4..3698ff3f0 100644 --- a/crates/mbe/src/tests.rs +++ b/crates/mbe/src/tests.rs @@ -29,8 +29,7 @@ macro_rules! impl_fixture { let macro_invocation = source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); - let (invocation_tt, _) = ast_to_token_tree(¯o_invocation.token_tree().unwrap()) - .ok_or_else(|| ExpandError::ConversionError)?; + let (invocation_tt, _) = ast_to_token_tree(¯o_invocation.token_tree().unwrap()); self.rules.expand(&invocation_tt).result() } @@ -101,7 +100,7 @@ macro_rules! impl_fixture { .descendants() .find_map(ast::TokenTree::cast) .unwrap(); - let mut wrapped = ast_to_token_tree(&wrapped).unwrap().0; + let mut wrapped = ast_to_token_tree(&wrapped).0; wrapped.delimiter = None; wrapped }; @@ -151,7 +150,7 @@ pub(crate) fn parse_macro_error(ra_fixture: &str) -> ParseError { pub(crate) fn parse_to_token_tree_by_syntax(ra_fixture: &str) -> tt::Subtree { let source_file = ast::SourceFile::parse(ra_fixture).ok().unwrap(); - let tt = syntax_node_to_token_tree(source_file.syntax()).unwrap().0; + let tt = syntax_node_to_token_tree(source_file.syntax()).0; let parsed = parse_to_token_tree(ra_fixture).unwrap().0; assert_eq!(tt, parsed); @@ -164,7 +163,7 @@ fn parse_macro_rules_to_tt(ra_fixture: &str) -> tt::Subtree { let macro_definition = source_file.syntax().descendants().find_map(ast::MacroRules::cast).unwrap(); - let (definition_tt, _) = ast_to_token_tree(¯o_definition.token_tree().unwrap()).unwrap(); + let (definition_tt, _) = ast_to_token_tree(¯o_definition.token_tree().unwrap()); let parsed = parse_to_token_tree( &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 { let macro_definition = source_file.syntax().descendants().find_map(ast::MacroDef::cast).unwrap(); - let (definition_tt, _) = ast_to_token_tree(¯o_definition.body().unwrap()).unwrap(); + let (definition_tt, _) = ast_to_token_tree(¯o_definition.body().unwrap()); let parsed = parse_to_token_tree(&ra_fixture[macro_definition.body().unwrap().syntax().text_range()]) diff --git a/crates/mbe/src/tests/rule.rs b/crates/mbe/src/tests/rule.rs index bf48112b3..5c61a98fd 100644 --- a/crates/mbe/src/tests/rule.rs +++ b/crates/mbe/src/tests/rule.rs @@ -44,6 +44,6 @@ fn parse_macro_arm(arm_definition: &str) -> Result