Skip to content

Fix stack overflow when macro_rules! macros shadow builtin attributes or types #142946

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions compiler/rustc_resolve/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,20 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
None
};
if let Some(kind) = ambiguity_error_kind {
// Prevent infinite recursion when macro_rules! macros shadow builtin attributes.
// Only apply this fix for macro_rules! to avoid breaking legitimate proc-macro ambiguity detection.
if kind == AmbiguityKind::BuiltinAttr {
if is_builtin(innermost_res)
&& flags.contains(Flags::MACRO_RULES)
{
return Some(Ok(innermost_binding));
} else if is_builtin(res)
&& innermost_flags.contains(Flags::MACRO_RULES)
{
return Some(Ok(binding));
}
}

let misc = |f: Flags| {
if f.contains(Flags::MISC_SUGGEST_CRATE) {
AmbiguityErrorMisc::SuggestCrate
Expand Down
38 changes: 38 additions & 0 deletions tests/ui/resolve/macro-builtin-attr-no-crash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Regression test for stack overflow when a `macro_rules!` macro shadows
// a builtin attribute and is used recursively within its own expansion.

//@ check-pass

#![allow(unused)]

#[macro_export]
macro_rules! test {
() => {
#[test]
fn generated_test() {
assert!(true);
}
};
}

// Additional case with derive
#[macro_export]
macro_rules! derive {
() => {
#[derive(Debug)]
struct Foo;
};
}

#[cfg(test)]
mod tests {
use super::*;
test!();
}

mod another_test {
use super::*;
derive!();
}

fn main() {}
Loading