Skip to content

fixed the duplicate metavariables issue - #4444

Open
hriztam wants to merge 8 commits into
Rust-GCC:masterfrom
hriztam:fix/metavariables
Open

hriztam wants to merge 8 commits into
Rust-GCC:masterfrom
hriztam:fix/metavariables

Conversation

@hriztam

@hriztam hriztam commented Feb 21, 2026

Copy link
Copy Markdown
Contributor

Addresses #4409.

This change moves duplicate matcher binding validation for macro_rules! from invocation-time matching into macro-definition validation during early name resolution. This makes duplicate metavariable bindings fail even when the macro is never invoked, matching rustc’s behavior for duplicate matcher binding.

It also adds an unused macro-definition warning pass in expansion by scanning AST macro definitions and checking whether each definition is referenced by a resolved invocation (excluding builtins and #[macro_export] cases), producing unused macro definition.

The expander-side duplicate-binding guard logic was removed since diagnostics are now emitted at definition validation time.

Tests:

  • Updated gcc/testsuite/rust/compile/macros/mbe/macro-duplicate-binding.rs to cover the uninvoked reproducer and assert both:
    • duplicate matcher binding error
    • unused macro definition warning

Also I tried to format the code using clang-format but that resulted in a much larger pr with a lot of files formatted at once, so I reverted it for the time being. Let me know if that is something which is needed

Signed-off-by: Hritam Shrivastava hritamstark05@gmail.com

Addresses Rust-GCC#4409.

Validate duplicate matcher bindings for `macro_rules!` during definition
resolution so duplicates are diagnosed even if the macro is never invoked.

gcc/rust/ChangeLog:

	* expand/rust-macro-expand.cc (MacroExpander::expand_decl_macro):
	Always emit the failed-to-match error when no rule matches.
	(MacroExpander::match_matcher): Remove duplicate-binding checks.
	* expand/rust-macro-expand.h (MacroExpander): Remove
	had_duplicate_error state.
	* resolve/rust-early-name-resolver-2.0.cc
	(check_matcher_for_duplicate_bindings): New helper.
	(Early::visit): Check for duplicate matcher bindings in macro
	definitions.

gcc/testsuite/ChangeLog:

	* rust/compile/macros/mbe/macro-duplicate-binding.rs: Check
	duplicate matcher bindings on an unused macro definition.

@P-E-P P-E-P left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some files are artifacts generated from the configure script and should not be pushed.

@hriztam

hriztam commented Feb 24, 2026

Copy link
Copy Markdown
Contributor Author

ok let me update the PR

@hriztam
hriztam requested a review from P-E-P February 24, 2026 08:23
@CohenArthur

Copy link
Copy Markdown
Member

@hriztam changes to the .gitignore that are due to your personal setup shouldn't be pushed :) also the testsuite is failing in the CI, please make sure it passes before we can merge this. Finally before merging you'll need to squash your commits that add and remove the generated files. thanks!

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.

3 participants