Make ast_to_token_tree infallible

It could never return `None`, so reflect that in the return type
This commit is contained in:
Jonas Schievink 2021-04-04 01:46:45 +02:00
parent bcf600fc88
commit 3abcdc03ba
11 changed files with 32 additions and 41 deletions

View file

@ -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<tt::Subtree> {
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<tt::TokenTree>) {
@ -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();
}
}