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/cfg/src/tests.rs | 8 ++++---- crates/hir_def/src/attr.rs | 2 +- crates/hir_expand/src/builtin_macro.rs | 2 +- crates/hir_expand/src/db.rs | 14 ++++---------- crates/hir_expand/src/eager.rs | 4 ++-- 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 +- crates/rust-analyzer/src/cargo_target_spec.rs | 2 +- 11 files changed, 32 insertions(+), 41 deletions(-) (limited to 'crates') diff --git a/crates/cfg/src/tests.rs b/crates/cfg/src/tests.rs index bd0f9ec48..d8736c893 100644 --- a/crates/cfg/src/tests.rs +++ b/crates/cfg/src/tests.rs @@ -8,7 +8,7 @@ fn assert_parse_result(input: &str, expected: CfgExpr) { let (tt, _) = { let source_file = ast::SourceFile::parse(input).ok().unwrap(); let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); - ast_to_token_tree(&tt).unwrap() + ast_to_token_tree(&tt) }; let cfg = CfgExpr::parse(&tt); assert_eq!(cfg, expected); @@ -18,7 +18,7 @@ fn check_dnf(input: &str, expect: Expect) { let (tt, _) = { let source_file = ast::SourceFile::parse(input).ok().unwrap(); let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); - ast_to_token_tree(&tt).unwrap() + ast_to_token_tree(&tt) }; let cfg = CfgExpr::parse(&tt); let actual = format!("#![cfg({})]", DnfExpr::new(cfg)); @@ -29,7 +29,7 @@ fn check_why_inactive(input: &str, opts: &CfgOptions, expect: Expect) { let (tt, _) = { let source_file = ast::SourceFile::parse(input).ok().unwrap(); let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); - ast_to_token_tree(&tt).unwrap() + ast_to_token_tree(&tt) }; let cfg = CfgExpr::parse(&tt); let dnf = DnfExpr::new(cfg); @@ -42,7 +42,7 @@ fn check_enable_hints(input: &str, opts: &CfgOptions, expected_hints: &[&str]) { let (tt, _) = { let source_file = ast::SourceFile::parse(input).ok().unwrap(); let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); - ast_to_token_tree(&tt).unwrap() + ast_to_token_tree(&tt) }; let cfg = CfgExpr::parse(&tt); let dnf = DnfExpr::new(cfg); diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs index 2bab121d9..442c5fb5b 100644 --- a/crates/hir_def/src/attr.rs +++ b/crates/hir_def/src/attr.rs @@ -533,7 +533,7 @@ impl Attr { }; Some(AttrInput::Literal(value)) } else if let Some(tt) = ast.token_tree() { - Some(AttrInput::TokenTree(ast_to_token_tree(&tt)?.0)) + Some(AttrInput::TokenTree(ast_to_token_tree(&tt).0)) } else { None }; diff --git a/crates/hir_expand/src/builtin_macro.rs b/crates/hir_expand/src/builtin_macro.rs index 3aa3d8997..75ec4196b 100644 --- a/crates/hir_expand/src/builtin_macro.rs +++ b/crates/hir_expand/src/builtin_macro.rs @@ -584,7 +584,7 @@ mod tests { }; let args = macro_call.token_tree().unwrap(); - let parsed_args = mbe::ast_to_token_tree(&args).unwrap().0; + let parsed_args = mbe::ast_to_token_tree(&args).0; let call_id = AstId::new(file_id.into(), ast_id_map.ast_id(¯o_call)); let arg_id = db.intern_eager_expansion({ diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs index c0ab70b60..10fe60821 100644 --- a/crates/hir_expand/src/db.rs +++ b/crates/hir_expand/src/db.rs @@ -119,7 +119,7 @@ pub fn expand_hypothetical( token_to_map: syntax::SyntaxToken, ) -> Option<(SyntaxNode, syntax::SyntaxToken)> { let macro_file = MacroFile { macro_call_id: actual_macro_call }; - let (tt, tmap_1) = mbe::syntax_node_to_token_tree(hypothetical_args.syntax()).unwrap(); + let (tt, tmap_1) = mbe::syntax_node_to_token_tree(hypothetical_args.syntax()); let range = token_to_map.text_range().checked_sub(hypothetical_args.syntax().text_range().start())?; let token_id = tmap_1.token_by_range(range)?; @@ -143,10 +143,7 @@ fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option match ast_id.to_node(db) { syntax::ast::Macro::MacroRules(macro_rules) => { let arg = macro_rules.token_tree()?; - let (tt, tmap) = mbe::ast_to_token_tree(&arg).or_else(|| { - log::warn!("fail on macro_rules to token tree: {:#?}", arg); - None - })?; + let (tt, tmap) = mbe::ast_to_token_tree(&arg); let rules = match MacroRules::parse(&tt) { Ok(it) => it, Err(err) => { @@ -159,10 +156,7 @@ fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option { let arg = macro_def.body()?; - let (tt, tmap) = mbe::ast_to_token_tree(&arg).or_else(|| { - log::warn!("fail on macro_def to token tree: {:#?}", arg); - None - })?; + let (tt, tmap) = mbe::ast_to_token_tree(&arg); let rules = match MacroDef::parse(&tt) { Ok(it) => it, Err(err) => { @@ -202,7 +196,7 @@ fn macro_arg_text(db: &dyn AstDatabase, id: MacroCallId) -> Option { fn macro_arg(db: &dyn AstDatabase, id: MacroCallId) -> Option> { let arg = db.macro_arg_text(id)?; - let (tt, tmap) = mbe::syntax_node_to_token_tree(&SyntaxNode::new_root(arg))?; + let (tt, tmap) = mbe::syntax_node_to_token_tree(&SyntaxNode::new_root(arg)); Some(Arc::new((tt, tmap))) } diff --git a/crates/hir_expand/src/eager.rs b/crates/hir_expand/src/eager.rs index 9eedc8461..9705526fa 100644 --- a/crates/hir_expand/src/eager.rs +++ b/crates/hir_expand/src/eager.rs @@ -106,7 +106,7 @@ pub fn expand_eager_macro( mut diagnostic_sink: &mut dyn FnMut(mbe::ExpandError), ) -> Result { let parsed_args = diagnostic_sink.option_with( - || Some(mbe::ast_to_token_tree(¯o_call.value.token_tree()?)?.0), + || Some(mbe::ast_to_token_tree(¯o_call.value.token_tree()?).0), || err("malformed macro invocation"), )?; @@ -161,7 +161,7 @@ pub fn expand_eager_macro( } fn to_subtree(node: &SyntaxNode) -> Option { - let mut subtree = mbe::syntax_node_to_token_tree(node)?.0; + let mut subtree = mbe::syntax_node_to_token_tree(node).0; subtree.delimiter = None; Some(subtree) } 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