-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Don't recursively expand undefined macros #12197
Conversation
This led to infinite recursion when compiling a macro which inside used an undefined macro. Instead some dummy expression is returned for the result of expansion (rather than the macro itself). Closes rust-lang#11692
fn main() { | ||
print!(test!()); | ||
//~^ ERROR: macro undefined: 'test' | ||
//~^^ ERROR: macro undefined: 'test' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would this print the error twice?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't actually quite figure that out. This has to do with constructing a nested parser that doesn't have the same macro context as the outer context, but at least one of the errors is for the sub-parser complaining about an unknown macro, and the second error message may come from the original parser parsing the expression again?
I figured going from infinity instances of 'macro undefined' to two was at least somewhat better. Fixing the two prints I think would require a better understanding of macro parsing (which may be a reason to reject this patch).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The double error occurs because of how format_args!() is defined. If the first argument is not a string literal then it emits an error but also expands to the first argument. So then the macro is attempted to be expanded again, and fails.
I just noticed that you can trigger similar behaviour with the code:
It seems there is a fundamental issue with returning the unexpanded expression from expand_expr() |
Also, this does not happen with a simple macro created by macro_rules!() |
Closing in favor of #12370 |
Closes #11692. Instead of returning the original expression, a dummy expression (with identical span) is returned. This prevents infinite loops of failed expansions as well as odd double error messages in certain situations. This is a slightly better fix than #12197, because it does not produce a double error and also fixes a few other cases where an infinite loop could happen. This does not fix the other issue in #11692 (non-builtin macros not being recognised when expanded inside macros), which I think should be moved into a separate issue.
fix: Fix import insertion inserting after last comment in a file
fix [`missing_docs_in_private_items`] on some proc macros fixes: rust-lang#12197 --- changelog: [`missing_docs_in_private_items`] support manually search for docs as fallback method
This led to infinite recursion when compiling a macro which inside used an
undefined macro. Instead some dummy expression is returned for the result of
expansion (rather than the macro itself).
Closes #11692