Skip to content

Handle macro_use prelude - #5105

Merged
P-E-P merged 5 commits into
Rust-GCC:masterfrom
P-E-P:extended_testsuite
Nov 27, 2026
Merged

P-E-P merged 5 commits into
Rust-GCC:masterfrom
P-E-P:extended_testsuite

Conversation

@P-E-P

@P-E-P P-E-P commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

External crates may use the #[macro_use] attribute to bring their macros within the crate's scope. This allow macro call within a crate without specifying its canonical path within the external crate name.

This is achieved using the macro_use prelude.

@P-E-P
P-E-P force-pushed the extended_testsuite branch from 8febd62 to 78bdb99 Compare November 23, 2026 12:55
@P-E-P P-E-P changed the title Add RFL single file crates to testsuite Handle macro_use prelude Nov 23, 2026
@P-E-P
P-E-P force-pushed the extended_testsuite branch 3 times, most recently from 7f14751 to bb74379 Compare November 23, 2026 15:39
Comment thread gcc/rust/rust-session-manager.h Resolved
Comment thread gcc/rust/rust-session-manager.cc Resolved
Comment thread gcc/rust/resolve/rust-forever-stack.h Resolved
Comment thread gcc/rust/resolve/rust-forever-stack.hxx Resolved
Comment thread gcc/rust/resolve/rust-forever-stack.hxx Resolved
@P-E-P
P-E-P force-pushed the extended_testsuite branch 4 times, most recently from ba7c5e4 to e7b5d90 Compare November 27, 2026 13:56
Macros from extern crate should be accessible, their identifier should
be brought into scope as shadowable identifiers when using the macro_use
attribute.

gcc/rust/ChangeLog:

	* expand/rust-extern-crate-loader.cc (ExternCrateLoaderVisitor::visit):
	Identify macro_use on extern crates.
	* resolve/rust-forever-stack.h: Add macro_use prelude to forever stack.
	Add function to add macros to the macro_use prelude.
	* resolve/rust-forever-stack.hxx: Add macros to macro_use prelude and
	dump macro_use prelude.
	* resolve/rust-name-resolution-context.cc (NameResolutionContext::NameResolutionContext):
	Initialize prelude.
	(NameResolutionContext::merge): Add all extern crate macros to
	macro_use prelude when the macro_use attribute is used.
	* resolve/rust-name-resolution-context.h: Add macro_use_prelude node.
	* rust-session-manager.cc (Session::expansion): Forward macro_use
	attribute status.
	(Session::load_extern_crate): Likewise.
	* rust-session-manager.h: Likewise.

Signed-off-by: Pierre-Emmanuel Patry <pierre.emmanuel.patry@opensrcsec.com>
@P-E-P
P-E-P force-pushed the extended_testsuite branch 2 times, most recently from dbe231a to 920bf21 Compare November 27, 2026 14:23
@P-E-P
P-E-P marked this pull request as ready for review November 27, 2026 14:24
Macros imported from extern crates using the macro_use prelude should
be resolved and remain shadowable, and thus be resolved after all other
namespaces.

gcc/rust/ChangeLog:

	* resolve/rust-forever-stack.h: Add function prototype.
	* resolve/rust-forever-stack.hxx: Add function to retrieve identifier
	from macro_use prelude.
	* resolve/rust-name-resolution-context.hxx: Lookup macro_use prelude
	when everything else has failed.

Signed-off-by: Pierre-Emmanuel Patry <pierre.emmanuel.patry@opensrcsec.com>
We need macros from both the std lib as well as the core library. In
order to do so we need to add the macro_use attribute on their
declaration.

gcc/rust/ChangeLog:

	* ast/rust-ast.cc (Crate::inject_extern_crate): Inject the macro_use
	attribute when injecting the crate.

Signed-off-by: Pierre-Emmanuel Patry <pierre.emmanuel.patry@opensrcsec.com>
gcc/testsuite/ChangeLog:

	* rust/minicore/build_error.rs: New test.
	* rust/minicore/build_error_core/lib.rs: New test.
	* rust/minicore/compiler_builtins.rs: New test.
	* rust/minicore/compiler_builtins_core/lib.rs: New test.

Signed-off-by: Pierre-Emmanuel Patry <pierre.emmanuel.patry@opensrcsec.com>
gcc/rust/ChangeLog:

	* expand/rust-extern-crate-loader.cc (ExternCrateLoaderVisitor::visit):
	Use explicit MacroUse enum instead of boolean.
	* resolve/rust-name-resolution-context.cc (NameResolutionContext::merge):
	Likewise.
	* resolve/rust-name-resolution-context.h: Likewise.
	* rust-session-manager.cc (Session::load_extern_crate): Likewise.
	* rust-session-manager.h: Likewise.
	* rust-load-crate.h: New file with MacroUse definition.

Signed-off-by: Pierre-Emmanuel Patry <pierre.emmanuel.patry@opensrcsec.com>
@P-E-P
P-E-P force-pushed the extended_testsuite branch from 920bf21 to b43f388 Compare November 27, 2026 14:55
@P-E-P
P-E-P added this pull request to the merge queue Nov 27, 2026
Merged via the queue into Rust-GCC:master with commit 6a45b39 Nov 27, 2026
13 checks passed
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.

2 participants