diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs index 98177f4301..38c16503cc 100644 --- a/crates/hir_def/src/attr.rs +++ b/crates/hir_def/src/attr.rs @@ -735,7 +735,7 @@ impl Attr { hygiene: &Hygiene, id: AttrId, ) -> Option { - let (parse, _) = mbe::token_tree_to_syntax_node(tt, mbe::TopEntryPoint::MetaItem).ok()?; + let (parse, _) = mbe::token_tree_to_syntax_node(tt, mbe::TopEntryPoint::MetaItem); let ast = ast::Meta::cast(parse.syntax_node())?; Self::from_src(db, ast, hygiene, id) diff --git a/crates/hir_expand/src/builtin_derive_macro.rs b/crates/hir_expand/src/builtin_derive_macro.rs index c1542f48f0..bd75c51cbc 100644 --- a/crates/hir_expand/src/builtin_derive_macro.rs +++ b/crates/hir_expand/src/builtin_derive_macro.rs @@ -72,7 +72,7 @@ struct BasicAdtInfo { } fn parse_adt(tt: &tt::Subtree) -> Result { - let (parsed, token_map) = mbe::token_tree_to_syntax_node(tt, mbe::TopEntryPoint::MacroItems)?; // FragmentKind::Items doesn't parse attrs? + let (parsed, token_map) = mbe::token_tree_to_syntax_node(tt, mbe::TopEntryPoint::MacroItems); // FragmentKind::Items doesn't parse attrs? let macro_items = ast::MacroItems::cast(parsed.syntax_node()).ok_or_else(|| { debug!("derive node didn't parse"); mbe::ExpandError::UnexpectedToken diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs index c39825b591..69749decd5 100644 --- a/crates/hir_expand/src/db.rs +++ b/crates/hir_expand/src/db.rs @@ -202,8 +202,7 @@ pub fn expand_speculative( }; let expand_to = macro_expand_to(db, actual_macro_call); - let (node, rev_tmap) = - token_tree_to_syntax_node(&speculative_expansion.value, expand_to).ok()?; + let (node, rev_tmap) = token_tree_to_syntax_node(&speculative_expansion.value, expand_to); let range = rev_tmap.first_range_by_token(token_id, token_to_map.kind())?; let token = node.syntax_node().covering_element(range).into_token()?; @@ -264,17 +263,7 @@ fn parse_macro_expansion( tracing::debug!("expanded = {}", tt.as_debug_string()); tracing::debug!("kind = {:?}", expand_to); - let (parse, rev_token_map) = match token_tree_to_syntax_node(&tt, expand_to) { - Ok(it) => it, - Err(err) => { - tracing::debug!( - "failed to parse expansion to {:?} = {}", - expand_to, - tt.as_debug_string() - ); - return ExpandResult::only_err(err); - } - }; + let (parse, rev_token_map) = token_tree_to_syntax_node(&tt, expand_to); match result.err { Some(err) => { @@ -502,7 +491,7 @@ fn macro_expand_to(db: &dyn AstDatabase, id: MacroCallId) -> ExpandTo { fn token_tree_to_syntax_node( tt: &tt::Subtree, expand_to: ExpandTo, -) -> Result<(Parse, mbe::TokenMap), ExpandError> { +) -> (Parse, mbe::TokenMap) { let entry_point = match expand_to { ExpandTo::Statements => mbe::TopEntryPoint::MacroStmts, ExpandTo::Items => mbe::TopEntryPoint::MacroItems, diff --git a/crates/hir_expand/src/eager.rs b/crates/hir_expand/src/eager.rs index 3d683d0640..f2586fa909 100644 --- a/crates/hir_expand/src/eager.rs +++ b/crates/hir_expand/src/eager.rs @@ -104,12 +104,13 @@ pub fn expand_eager_macro( macro_call: InFile, def: MacroDefId, resolver: &dyn Fn(ast::Path) -> Option, - mut diagnostic_sink: &mut dyn FnMut(mbe::ExpandError), + diagnostic_sink: &mut dyn FnMut(mbe::ExpandError), ) -> Result { - let parsed_args = diagnostic_sink.option_with( - || Some(mbe::syntax_node_to_token_tree(macro_call.value.token_tree()?.syntax()).0), - || err("malformed macro invocation"), - )?; + let parsed_args = macro_call + .value + .token_tree() + .map(|tt| mbe::syntax_node_to_token_tree(tt.syntax()).0) + .unwrap_or_default(); let ast_map = db.ast_id_map(macro_call.file_id); let call_id = InFile::new(macro_call.file_id, ast_map.ast_id(¯o_call.value)); @@ -130,9 +131,7 @@ pub fn expand_eager_macro( }); let arg_file_id = arg_id; - let parsed_args = diagnostic_sink - .result(mbe::token_tree_to_syntax_node(&parsed_args, mbe::TopEntryPoint::Expr))? - .0; + let parsed_args = mbe::token_tree_to_syntax_node(&parsed_args, mbe::TopEntryPoint::Expr).0; let result = eager_macro_recur( db, InFile::new(arg_file_id.as_file(), parsed_args.syntax_node()), @@ -140,8 +139,7 @@ pub fn expand_eager_macro( resolver, diagnostic_sink, )?; - let subtree = - diagnostic_sink.option(to_subtree(&result), || err("failed to parse macro result"))?; + let subtree = to_subtree(&result); if let MacroDefKind::BuiltInEager(eager, _) = def.kind { let res = eager.expand(db, arg_id, &subtree); @@ -165,10 +163,10 @@ pub fn expand_eager_macro( } } -fn to_subtree(node: &SyntaxNode) -> Option { +fn to_subtree(node: &SyntaxNode) -> tt::Subtree { let mut subtree = mbe::syntax_node_to_token_tree(node).0; subtree.delimiter = None; - Some(subtree) + subtree } fn lazy_expand( diff --git a/crates/mbe/src/syntax_bridge.rs b/crates/mbe/src/syntax_bridge.rs index 5d70558241..bac1d4ba98 100644 --- a/crates/mbe/src/syntax_bridge.rs +++ b/crates/mbe/src/syntax_bridge.rs @@ -10,7 +10,7 @@ use syntax::{ }; use tt::buffer::{Cursor, TokenBuffer}; -use crate::{to_parser_input::to_parser_input, tt_iter::TtIter, ExpandError, TokenMap}; +use crate::{to_parser_input::to_parser_input, tt_iter::TtIter, TokenMap}; /// Convert the syntax node to a `TokenTree` (what macro /// will consume). @@ -46,7 +46,7 @@ pub fn syntax_node_to_token_tree_censored( pub fn token_tree_to_syntax_node( tt: &tt::Subtree, entry_point: parser::TopEntryPoint, -) -> Result<(Parse, TokenMap), ExpandError> { +) -> (Parse, TokenMap) { let buffer = match tt { tt::Subtree { delimiter: None, token_trees } => { TokenBuffer::from_tokens(token_trees.as_slice()) @@ -67,7 +67,7 @@ pub fn token_tree_to_syntax_node( } } let (parse, range_map) = tree_sink.finish(); - Ok((parse, range_map)) + (parse, range_map) } /// Convert a string to a `TokenTree`