4139: Add check for rules that no repetition which could match an empty token r=matklad a=edwin0cheng

Fix #4103

for `/ui/issues/issue-57597.rs`

This is `ParseError` of the macro rules , because it is how rustc handle it : 

a58b1ed44f/src/librustc_expand/mbe/macro_rules.rs (L558)


Co-authored-by: Edwin Cheng <edwin0cheng@gmail.com>
This commit is contained in:
bors[bot] 2020-04-25 16:26:51 +00:00 committed by GitHub
commit 5671bacfa6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 75 additions and 12 deletions

View file

@ -1657,7 +1657,7 @@ impl MacroFixture {
}
}
pub(crate) fn parse_macro(ra_fixture: &str) -> MacroFixture {
fn parse_macro_to_tt(ra_fixture: &str) -> tt::Subtree {
let source_file = ast::SourceFile::parse(ra_fixture).ok().unwrap();
let macro_definition =
source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
@ -1671,10 +1671,24 @@ pub(crate) fn parse_macro(ra_fixture: &str) -> MacroFixture {
.0;
assert_eq!(definition_tt, parsed);
definition_tt
}
pub(crate) fn parse_macro(ra_fixture: &str) -> MacroFixture {
let definition_tt = parse_macro_to_tt(ra_fixture);
let rules = MacroRules::parse(&definition_tt).unwrap();
MacroFixture { rules }
}
pub(crate) fn parse_macro_error(ra_fixture: &str) -> ParseError {
let definition_tt = parse_macro_to_tt(ra_fixture);
match MacroRules::parse(&definition_tt) {
Ok(_) => panic!("Expect error"),
Err(err) => err,
}
}
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;
@ -1840,6 +1854,27 @@ fn test_no_space_after_semi_colon() {
);
}
// https://github.com/rust-lang/rust/blob/master/src/test/ui/issues/issue-57597.rs
#[test]
fn test_rustc_issue_57597() {
fn test_error(fixture: &str) {
assert_eq!(parse_macro_error(fixture), ParseError::RepetitionEmtpyTokenTree);
}
test_error("macro_rules! foo { ($($($i:ident)?)+) => {}; }");
test_error("macro_rules! foo { ($($($i:ident)?)*) => {}; }");
test_error("macro_rules! foo { ($($($i:ident)?)?) => {}; }");
test_error("macro_rules! foo { ($($($($i:ident)?)?)?) => {}; }");
test_error("macro_rules! foo { ($($($($i:ident)*)?)?) => {}; }");
test_error("macro_rules! foo { ($($($($i:ident)?)*)?) => {}; }");
test_error("macro_rules! foo { ($($($($i:ident)?)?)*) => {}; }");
test_error("macro_rules! foo { ($($($($i:ident)*)*)?) => {}; }");
test_error("macro_rules! foo { ($($($($i:ident)?)*)*) => {}; }");
test_error("macro_rules! foo { ($($($($i:ident)?)*)+) => {}; }");
test_error("macro_rules! foo { ($($($($i:ident)+)?)*) => {}; }");
test_error("macro_rules! foo { ($($($($i:ident)+)*)?) => {}; }");
}
#[test]
fn test_expand_bad_literal() {
parse_macro(