Skip to content

ast: Fix module path resolution for sibling modules in root#4406

Merged
P-E-P merged 1 commit intoRust-GCC:masterfrom
Harishankar14:module_path
Jan 27, 2026
Merged

ast: Fix module path resolution for sibling modules in root#4406
P-E-P merged 1 commit intoRust-GCC:masterfrom
Harishankar14:module_path

Conversation

@Harishankar14
Copy link
Contributor

Currently, the compiler fails to resolve module paths when a file in the root directory (e.g. 'bar.rs') attempts to load a sibling module (e.g. 'foo.rs') if a 'bar/' directory does not exist. The compiler incorrectly assumes that if 'bar.rs' is not 'mod.rs', it must look exclusively in a subdirectory.

This patch adds a fallback mechanism in 'Module::process_file_path'. If the subdirectory search fails, it strips the implicit subdirectory and attempts to resolve the module in the parent directory, consistent with Rust 2018 path rules.

Fixes #4402

gcc/rust/ChangeLog:

* ast/rust-ast.cc (Module::process_file_path): Add fallback search for sibling modules when subdirectory search fails.

gcc/testsuite/ChangeLog:

* rust/compile/issue-4402.rs: New test.
* rust/compile/issue_4402_foo.rs: New test.

Thank you for making Rust GCC better!

If your PR fixes an issue, you can add "Fixes #issue_number" into this
PR description and the git commit message. This way the issue will be
automatically closed when your PR is merged. If your change addresses
an issue but does not fully fix it please mark it as "Addresses #issue_number"
in the git commit message.

Here is a checklist to help you with your PR.

Note that you can skip the above if you are just opening a WIP PR in
order to get feedback.

*Please write a comment explaining your change. This is the message
that will be part of the merge commit.

@Harishankar14
Copy link
Contributor Author

@CohenArthur

@P-E-P P-E-P added this pull request to the merge queue Jan 26, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jan 26, 2026
@Harishankar14
Copy link
Contributor Author

@P-E-P ANY ISSUES , the bot removed the PR from merge queue, i guess have to make some modification accordingly with respect to your new PR that was added ?

@P-E-P
Copy link
Member

P-E-P commented Jan 26, 2026

@P-E-P ANY ISSUES , the bot removed the PR from merge queue, i guess have to make some modification accordingly with respect to your new PR that was added ?

Yes I forgot about it again; you need to add #[no_core] to the crate. If you could also rebase your branch on master and delete that merge commit that would be great.

@Harishankar14 Harishankar14 force-pushed the module_path branch 2 times, most recently from 16b7a99 to 1056418 Compare January 26, 2026 22:55
@Harishankar14
Copy link
Contributor Author

@P-E-P Should be GTG?

@P-E-P P-E-P enabled auto-merge January 27, 2026 00:32
@P-E-P
Copy link
Member

P-E-P commented Jan 27, 2026

You could use something that looks like the following:

In compile.exp

set ignore_list [ "issue_4402_foo.rs" ]
dg-runtest [lsearch -inline -all -not -exact [lsort [glob -nocomplain $srcdir/$subdir/*.rs]] [ lmap file $ignore_list "$src/$subdir/$file"]] "" ""

I haven't tested it so you'll have to experiment a bit but this could do the trick

Currently, the compiler fails to resolve module paths when a file in the
root directory (e.g. 'bar.rs') attempts to load a sibling module
(e.g. 'foo.rs') if a 'bar/' directory does not exist. The compiler
incorrectly assumes that if 'bar.rs' is not 'mod.rs', it must look
exclusively in a subdirectory.

This patch adds a fallback mechanism in 'Module::process_file_path'.
If the subdirectory search fails, it strips the implicit subdirectory
and attempts to resolve the module in the parent directory, consistent
with Rust 2018 path rules.

Fixes Rust-GCC#4402

gcc/rust/ChangeLog:

	* ast/rust-ast.cc (Module::process_file_path): Add fallback search
	for sibling modules when subdirectory search fails.

gcc/testsuite/ChangeLog:

	* rust/compile/issue-4402.rs: New test.
	* rust/compile/issue_4402_foo.rs: New test.
	* rust/compile/compile.exp:Ignore issue_4402_foo.rs

Signed-off-by: Harishankar <[email protected]>
auto-merge was automatically disabled January 27, 2026 12:52

Head branch was pushed to by a user without write access

@Harishankar14
Copy link
Contributor Author

@P-E-P I think it worked !! No failures when i tested it out locally

@P-E-P P-E-P added this pull request to the merge queue Jan 27, 2026
Merged via the queue into Rust-GCC:master with commit dc07f2a Jan 27, 2026
12 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.

Cannot resolve module path properly in simple setting

2 participants