Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

alexcrichton
Copy link
Member

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

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'
Copy link
Member

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?

Copy link
Member Author

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).

Copy link
Contributor

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.

@rcxdude
Copy link
Contributor

rcxdude commented Feb 17, 2014

I just noticed that you can trigger similar behaviour with the code:

fn main() {
    print!(std::test!());
}

It seems there is a fundamental issue with returning the unexpanded expression from expand_expr()

@rcxdude
Copy link
Contributor

rcxdude commented Feb 17, 2014

Also, this does not happen with a simple macro created by macro_rules!()

@alexcrichton
Copy link
Member Author

Closing in favor of #12370

@alexcrichton alexcrichton deleted the issue-11692 branch February 18, 2014 17:17
bors added a commit that referenced this pull request Feb 19, 2014
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.
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 25, 2022
fix: Fix import insertion inserting after last comment in a file
matthiaskrgr pushed a commit to matthiaskrgr/rust that referenced this pull request Mar 21, 2024
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Infinite loop when compiling nested macros
3 participants