Skip to content

Diagnostic for non-inline modules inside blocks should encourage moving it out of the block #147314

@shepmaster

Description

@shepmaster

Code

fn main () {
    mod secret;
}

Current output

error: cannot declare a non-inline module inside a block unless it has a path attribute
 --> src/main.rs:2:5
  |
2 |     mod secret;
  |     ^^^^^^^^^^^

Desired output

error: cannot declare a non-inline module inside a block unless it has a path attribute. 
       Modules are usually placed outside of blocks, at the top level of the file.
 --> src/main.rs:2:5
  |
2 |     mod secret;
  |     ^^^^^^^^^^^

Rationale and extra context

Using #[path] is generally discouraged. The current message presents #[path] as the only solution.

Rust Version

rustc 1.92.0-nightly (5c7ae0c7e 2025-10-02)
binary: rustc
commit-hash: 5c7ae0c7ed184c603e5224604a9f33ca0e8e0b36
commit-date: 2025-10-02
host: aarch64-apple-darwin
release: 1.92.0-nightly
LLVM version: 21.1.2

Anything else?

Discovered while giving training to newcomers to Rust.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsE-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions