Skip to content

Skip rewriting macro def with repeat #2410

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
Feb 4, 2018
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
12 changes: 9 additions & 3 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,10 @@ pub fn rewrite_macro_def(
// variables for new names with the same length first.

let old_body = context.snippet(branch.body).trim();
let (body_str, substs) = replace_names(old_body);
let (body_str, substs) = match replace_names(old_body) {
Some(result) => result,
None => return snippet,
};

// We'll hack the indent below, take this into account when formatting,
let mut config = context.config.clone();
Expand Down Expand Up @@ -377,7 +380,7 @@ pub fn rewrite_macro_def(
// Replaces `$foo` with `zfoo`. We must check for name overlap to ensure we
// aren't causing problems.
// This should also work for escaped `$` variables, where we leave earlier `$`s.
fn replace_names(input: &str) -> (String, HashMap<String, String>) {
fn replace_names(input: &str) -> Option<(String, HashMap<String, String>)> {
// Each substitution will require five or six extra bytes.
let mut result = String::with_capacity(input.len() + 64);
let mut substs = HashMap::new();
Expand Down Expand Up @@ -409,6 +412,9 @@ fn replace_names(input: &str) -> (String, HashMap<String, String>) {

dollar_count = 0;
cur_name = String::new();
} else if c == '(' && cur_name.is_empty() {
// FIXME: Support macro def with repeat.
return None;
} else if c.is_alphanumeric() {
cur_name.push(c);
}
Expand All @@ -433,7 +439,7 @@ fn replace_names(input: &str) -> (String, HashMap<String, String>) {

debug!("replace_names `{}` {:?}", result, substs);

(result, substs)
Some((result, substs))
}

// This is a bit sketchy. The token rules probably need tweaking, but it works
Expand Down
4 changes: 4 additions & 0 deletions tests/source/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,7 @@ macro foo() {
bar();
}
}

macro lex_err($kind: ident $(, $body: expr)*) {
Err(QlError::LexError(LexError::$kind($($body,)*)))
}
4 changes: 4 additions & 0 deletions tests/target/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -905,3 +905,7 @@ macro foo() {
bar();
}
}

macro lex_err($kind: ident $(, $body: expr)*) {
Err(QlError::LexError(LexError::$kind($($body,)*)))
}