Skip to content

Add check for rules that no repetition which could match an empty token #4139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 39 additions & 11 deletions crates/ra_mbe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::{
#[derive(Debug, PartialEq, Eq)]
pub enum ParseError {
Expected(String),
RepetitionEmtpyTokenTree,
}

#[derive(Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -194,20 +195,46 @@ impl Rule {
}
}

fn to_parse_error(e: ExpandError) -> ParseError {
let msg = match e {
ExpandError::InvalidRepeat => "invalid repeat".to_string(),
_ => "invalid macro definition".to_string(),
};
ParseError::Expected(msg)
}

fn validate(pattern: &tt::Subtree) -> Result<(), ParseError> {
for op in parse_pattern(pattern) {
let op = match op {
Ok(it) => it,
Err(e) => {
let msg = match e {
ExpandError::InvalidRepeat => "invalid repeat".to_string(),
_ => "invalid macro definition".to_string(),
};
return Err(ParseError::Expected(msg));
}
};
let op = op.map_err(to_parse_error)?;

match op {
Op::TokenTree(tt::TokenTree::Subtree(subtree)) | Op::Repeat { subtree, .. } => {
Op::TokenTree(tt::TokenTree::Subtree(subtree)) => validate(subtree)?,
Op::Repeat { subtree, separator, .. } => {
// 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

if separator.is_none() {
if parse_pattern(subtree).all(|child_op| {
match child_op.map_err(to_parse_error) {
Ok(Op::Var { kind, .. }) => {
// vis is optional
if kind.map_or(false, |it| it == "vis") {
return true;
}
}
Ok(Op::Repeat { kind, .. }) => {
return matches!(
kind,
parser::RepeatKind::ZeroOrMore | parser::RepeatKind::ZeroOrOne
)
}
_ => {}
}
false
}) {
return Err(ParseError::RepetitionEmtpyTokenTree);
}
}
validate(subtree)?
}
_ => (),
Expand All @@ -216,6 +243,7 @@ fn validate(pattern: &tt::Subtree) -> Result<(), ParseError> {
Ok(())
}

#[derive(Debug)]
pub struct ExpandResult<T>(pub T, pub Option<ExpandError>);

impl<T> ExpandResult<T> {
Expand Down
37 changes: 36 additions & 1 deletion crates/ra_mbe/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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(
Expand Down