Skip to content

suggest declaring modules when file found but module not defined #142730

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

Conversation

bend-n
Copy link
Contributor

@bend-n bend-n commented Jun 19, 2025

suggests declaring modules when a module is found but not defined, i.e

├── main.rs: `use thing::thang;`
└── thing.rs: `struct thang`

or

├── main.rs: `use thing::thang;`
└── thing
    └── mod.rs: `struct thang`

which currently is just

error[E0432]: unresolved import `yeah`
 --> src/main.rs:1:1
  |
1 | use thing::thang;
  |     ^^^^^ use of unresolved module or unlinked crate `thing`
  |

but now would have this nice help:

= help: you may have forgotten to declare the module `thing`. use `mod thing` in this file to declare this module.

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 19, 2025
@bend-n
Copy link
Contributor Author

bend-n commented Jun 19, 2025

@rustbot label A-diagnostics

@rustbot rustbot added the A-diagnostics Area: Messages for errors, warnings, and lints label Jun 19, 2025
@rust-log-analyzer

This comment has been minimized.

@bend-n bend-n marked this pull request as ready for review June 20, 2025 12:00
@rustbot
Copy link
Collaborator

rustbot commented Jun 20, 2025

r? @petrochenkov

rustbot has assigned @petrochenkov.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 20, 2025
@rust-log-analyzer

This comment has been minimized.

@bend-n bend-n force-pushed the suggest_declaring_modules_when_file_found_but_module_not_defined branch from 7174cb4 to 397c51b Compare June 20, 2025 13:41
@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 23, 2025
@bend-n bend-n force-pushed the suggest_declaring_modules_when_file_found_but_module_not_defined branch from c1a4a41 to 18ca511 Compare June 23, 2025 17:13
@petrochenkov petrochenkov added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 24, 2025
let map = self.tcx.sess.source_map();
let sp = map
.span_extend_to_prev_char(
map.span_extend_to_prev_char(ident.span, '\n', true),
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a comment explaining what are you trying to do here?
(Also format this on a separate line to improve readability let sp = map.span_extend_to_prev_char(ident.span, '\n', true);)

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, wait, I think this can actually use current_crate_outer_attr_insert_span instead of UsePlacementFinder.
(The newly added comment tells that the current logic is clearly incorrect.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh yeah that seems like what i wanted i was wondering what the extern crate thing was spawned from

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 24, 2025
@bend-n bend-n force-pushed the suggest_declaring_modules_when_file_found_but_module_not_defined branch from 18ca511 to 184ef96 Compare June 24, 2025 17:56
@petrochenkov
Copy link
Contributor

Could you also add a new test exercising the various naming schemes (like …/x/y/mod.rs and others) for the module file?

@bend-n bend-n force-pushed the suggest_declaring_modules_when_file_found_but_module_not_defined branch from 184ef96 to 2f55dce Compare June 24, 2025 18:12
@bend-n bend-n force-pushed the suggest_declaring_modules_when_file_found_but_module_not_defined branch from 2f55dce to 3583423 Compare June 24, 2025 18:13
@bend-n
Copy link
Contributor Author

bend-n commented Jun 24, 2025

Could you also add a new test exercising the various naming schemes (like …/x/y/mod.rs and others) for the module file?

im not sure how to make a test with a normal directory structure? everything seems to use #[path = auxiliary/]...

@petrochenkov
Copy link
Contributor

Well, tests/ui/modules_and_files_visibility/mod_file_disambig.rs caught some file to report, the new test can probably do something similar to that case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants