Skip to content

Commit 5f13d6a

Browse files
bors[bot]Jonas Schievink
and
Jonas Schievink
authored
Merge #11340
11340: internal: Make syntax bridge fully infallible r=jonas-schievink a=jonas-schievink bors r+ Co-authored-by: Jonas Schievink <[email protected]>
2 parents 17afa2e + 5088926 commit 5f13d6a

File tree

5 files changed

+18
-31
lines changed

5 files changed

+18
-31
lines changed

crates/hir_def/src/attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ impl Attr {
735735
hygiene: &Hygiene,
736736
id: AttrId,
737737
) -> Option<Attr> {
738-
let (parse, _) = mbe::token_tree_to_syntax_node(tt, mbe::TopEntryPoint::MetaItem).ok()?;
738+
let (parse, _) = mbe::token_tree_to_syntax_node(tt, mbe::TopEntryPoint::MetaItem);
739739
let ast = ast::Meta::cast(parse.syntax_node())?;
740740

741741
Self::from_src(db, ast, hygiene, id)

crates/hir_expand/src/builtin_derive_macro.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ struct BasicAdtInfo {
7272
}
7373

7474
fn parse_adt(tt: &tt::Subtree) -> Result<BasicAdtInfo, mbe::ExpandError> {
75-
let (parsed, token_map) = mbe::token_tree_to_syntax_node(tt, mbe::TopEntryPoint::MacroItems)?; // FragmentKind::Items doesn't parse attrs?
75+
let (parsed, token_map) = mbe::token_tree_to_syntax_node(tt, mbe::TopEntryPoint::MacroItems); // FragmentKind::Items doesn't parse attrs?
7676
let macro_items = ast::MacroItems::cast(parsed.syntax_node()).ok_or_else(|| {
7777
debug!("derive node didn't parse");
7878
mbe::ExpandError::UnexpectedToken

crates/hir_expand/src/db.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,7 @@ pub fn expand_speculative(
202202
};
203203

204204
let expand_to = macro_expand_to(db, actual_macro_call);
205-
let (node, rev_tmap) =
206-
token_tree_to_syntax_node(&speculative_expansion.value, expand_to).ok()?;
205+
let (node, rev_tmap) = token_tree_to_syntax_node(&speculative_expansion.value, expand_to);
207206

208207
let range = rev_tmap.first_range_by_token(token_id, token_to_map.kind())?;
209208
let token = node.syntax_node().covering_element(range).into_token()?;
@@ -264,17 +263,7 @@ fn parse_macro_expansion(
264263
tracing::debug!("expanded = {}", tt.as_debug_string());
265264
tracing::debug!("kind = {:?}", expand_to);
266265

267-
let (parse, rev_token_map) = match token_tree_to_syntax_node(&tt, expand_to) {
268-
Ok(it) => it,
269-
Err(err) => {
270-
tracing::debug!(
271-
"failed to parse expansion to {:?} = {}",
272-
expand_to,
273-
tt.as_debug_string()
274-
);
275-
return ExpandResult::only_err(err);
276-
}
277-
};
266+
let (parse, rev_token_map) = token_tree_to_syntax_node(&tt, expand_to);
278267

279268
match result.err {
280269
Some(err) => {
@@ -502,7 +491,7 @@ fn macro_expand_to(db: &dyn AstDatabase, id: MacroCallId) -> ExpandTo {
502491
fn token_tree_to_syntax_node(
503492
tt: &tt::Subtree,
504493
expand_to: ExpandTo,
505-
) -> Result<(Parse<SyntaxNode>, mbe::TokenMap), ExpandError> {
494+
) -> (Parse<SyntaxNode>, mbe::TokenMap) {
506495
let entry_point = match expand_to {
507496
ExpandTo::Statements => mbe::TopEntryPoint::MacroStmts,
508497
ExpandTo::Items => mbe::TopEntryPoint::MacroItems,

crates/hir_expand/src/eager.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,13 @@ pub fn expand_eager_macro(
104104
macro_call: InFile<ast::MacroCall>,
105105
def: MacroDefId,
106106
resolver: &dyn Fn(ast::Path) -> Option<MacroDefId>,
107-
mut diagnostic_sink: &mut dyn FnMut(mbe::ExpandError),
107+
diagnostic_sink: &mut dyn FnMut(mbe::ExpandError),
108108
) -> Result<MacroCallId, ErrorEmitted> {
109-
let parsed_args = diagnostic_sink.option_with(
110-
|| Some(mbe::syntax_node_to_token_tree(macro_call.value.token_tree()?.syntax()).0),
111-
|| err("malformed macro invocation"),
112-
)?;
109+
let parsed_args = macro_call
110+
.value
111+
.token_tree()
112+
.map(|tt| mbe::syntax_node_to_token_tree(tt.syntax()).0)
113+
.unwrap_or_default();
113114

114115
let ast_map = db.ast_id_map(macro_call.file_id);
115116
let call_id = InFile::new(macro_call.file_id, ast_map.ast_id(&macro_call.value));
@@ -130,18 +131,15 @@ pub fn expand_eager_macro(
130131
});
131132
let arg_file_id = arg_id;
132133

133-
let parsed_args = diagnostic_sink
134-
.result(mbe::token_tree_to_syntax_node(&parsed_args, mbe::TopEntryPoint::Expr))?
135-
.0;
134+
let parsed_args = mbe::token_tree_to_syntax_node(&parsed_args, mbe::TopEntryPoint::Expr).0;
136135
let result = eager_macro_recur(
137136
db,
138137
InFile::new(arg_file_id.as_file(), parsed_args.syntax_node()),
139138
krate,
140139
resolver,
141140
diagnostic_sink,
142141
)?;
143-
let subtree =
144-
diagnostic_sink.option(to_subtree(&result), || err("failed to parse macro result"))?;
142+
let subtree = to_subtree(&result);
145143

146144
if let MacroDefKind::BuiltInEager(eager, _) = def.kind {
147145
let res = eager.expand(db, arg_id, &subtree);
@@ -165,10 +163,10 @@ pub fn expand_eager_macro(
165163
}
166164
}
167165

168-
fn to_subtree(node: &SyntaxNode) -> Option<tt::Subtree> {
166+
fn to_subtree(node: &SyntaxNode) -> tt::Subtree {
169167
let mut subtree = mbe::syntax_node_to_token_tree(node).0;
170168
subtree.delimiter = None;
171-
Some(subtree)
169+
subtree
172170
}
173171

174172
fn lazy_expand(

crates/mbe/src/syntax_bridge.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use syntax::{
1010
};
1111
use tt::buffer::{Cursor, TokenBuffer};
1212

13-
use crate::{to_parser_input::to_parser_input, tt_iter::TtIter, ExpandError, TokenMap};
13+
use crate::{to_parser_input::to_parser_input, tt_iter::TtIter, TokenMap};
1414

1515
/// Convert the syntax node to a `TokenTree` (what macro
1616
/// will consume).
@@ -46,7 +46,7 @@ pub fn syntax_node_to_token_tree_censored(
4646
pub fn token_tree_to_syntax_node(
4747
tt: &tt::Subtree,
4848
entry_point: parser::TopEntryPoint,
49-
) -> Result<(Parse<SyntaxNode>, TokenMap), ExpandError> {
49+
) -> (Parse<SyntaxNode>, TokenMap) {
5050
let buffer = match tt {
5151
tt::Subtree { delimiter: None, token_trees } => {
5252
TokenBuffer::from_tokens(token_trees.as_slice())
@@ -67,7 +67,7 @@ pub fn token_tree_to_syntax_node(
6767
}
6868
}
6969
let (parse, range_map) = tree_sink.finish();
70-
Ok((parse, range_map))
70+
(parse, range_map)
7171
}
7272

7373
/// Convert a string to a `TokenTree`

0 commit comments

Comments
 (0)