Expand unmatched mbe fragments to reasonable default token trees

Currently we expand unmatched fragments by not replacing them at all,
leaving us with `$ident`. This trips up the parser or subsequent macro
calls. Instead it makes more sense to replace these with some reasonable
default depending on the fragment kind which should make more recursive
macro calls work better for completions.
This commit is contained in:
Lukas Wirth 2022-10-10 14:25:14 +02:00
parent 476d043874
commit 78f33c0e96
6 changed files with 146 additions and 45 deletions

View file

@ -21,7 +21,7 @@ mod token_map;
use std::fmt;
use crate::{
parser::{MetaTemplate, Op},
parser::{MetaTemplate, MetaVarKind, Op},
tt_iter::TtIter,
};
@ -291,9 +291,9 @@ fn validate(pattern: &MetaTemplate) -> Result<(), ParseError> {
// Checks that no repetition which could match an empty token
// https://github.com/rust-lang/rust/blob/a58b1ed44f5e06976de2bdc4d7dc81c36a96934f/src/librustc_expand/mbe/macro_rules.rs#L558
let lsh_is_empty_seq = separator.is_none() && subtree.iter().all(|child_op| {
match child_op {
match *child_op {
// vis is optional
Op::Var { kind: Some(kind), .. } => kind == "vis",
Op::Var { kind: Some(kind), .. } => kind == MetaVarKind::Vis,
Op::Repeat {
kind: parser::RepeatKind::ZeroOrMore | parser::RepeatKind::ZeroOrOne,
..