Skip to content
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

E0428: Duplicate Definition Message in Circular include! Cases #136527

Open
SauersML opened this issue Feb 4, 2025 · 2 comments
Open

E0428: Duplicate Definition Message in Circular include! Cases #136527

SauersML opened this issue Feb 4, 2025 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@SauersML
Copy link

SauersML commented Feb 4, 2025

Code

cargo new repro && cd repro && \
printf 'include!("functions.rs");\ninclude!("process.rs");\nfn main() {}\n' > src/main.rs && \
printf 'fn function() {}\n' > src/functions.rs && \
printf 'include!("functions.rs");\n' > src/process.rs && \
cargo check

Current output

Creating binary (application) `repro` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
    Checking repro v0.1.0 (/users/maenas/downloads/repro)
error[E0428]: the name `function` is defined multiple times
 --> src/functions.rs:1:1
  |
1 | fn function() {}
  | ^^^^^^^^^^^^^
  | |
  | `function` redefined here
  | previous definition of the value `function` here
  |
  = note: `function` must be defined only once in the value namespace of this module

For more information about this error, try `rustc --explain E0428`.
error: could not compile `repro` (bin "repro") due to 1 previous error

Desired output

Creating binary (application) `repro` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
    Checking repro v0.1.0 (/users/maenas/downloads/repro)
error[E0428]: the name `function` is defined multiple times
 --> src/functions.rs:1:1
  |
1 | fn function() {}
  | ^^^^^^^^^^^^^ duplicate inclusion of `function` here via circular `include!`
  = note: the first definition is included via `src/main.rs` with:
          include!("functions.rs");
  = note: the duplicate definition is brought in via `src/process.rs`, which itself does:
          include!("functions.rs");
  = help: this circular inclusion causes `functions.rs` to be processed twice.
For more information about this error, try `rustc --explain E0428`.
error: could not compile `repro` (bin "repro") due to 1 previous error

Or something like:

error[E0428]: duplicate definition caused by circular include! expansion
. . .
   = note: the file `functions.rs` was included multiple times:
           - once directly in `src/main.rs` via `include!("functions.rs");`
           - once indirectly in `src/process.rs` (which also does `include!("functions.rs");`)

Rationale and extra context

It says "function redefined here" and also "previous definition of the value function here." It appears to be referring to only one place, but it reads as though there is supposed to be a second "here" that is being left out, especially since that (showing two locations) is how this error message normally works.

The sentence "the name function is defined multiple times" also is confusing. In the source code, there's only one definition of function.

The error explanation doesn't mention this as a cause either. Not knowing the cause of the error, a user might:

  • look in the source code for multiple definitions
  • grep /src for multiple definitions
  • run rustc --explain E0428

without being able to determine the cause of the error. Of course, many or most would realize the true problem quickly, but the error message appears more confusing than other similar circular errors.

The cause of this error is circular use of the !include macro.

Other cases

cargo new repro && cd repro && \
printf 'include!("a.rs");\nfn main() {}\n' > src/main.rs && \
printf 'include!("a.rs");\n' > src/a.rs && \
cargo check

Rust Version

rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: aarch64-apple-darwin
release: 1.83.0
LLVM version: 19.1.1

Anything else?

No response

@SauersML SauersML added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 4, 2025
@theemathas
Copy link
Contributor

What is "the !import macro"?

What was the use case where you ran into this problem?

@SauersML
Copy link
Author

SauersML commented Feb 4, 2025

Oops, the !include macro. The attempted use case I ran into this on was splitting a large file up into multiple file smaller files without spending time refactoring, making a library, or creating mod.rs. I thought I had removed one of the !include lines but hadn't, so got the error.

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 T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants