Skip to content

Diagnostic doesn't mention cfg-ed item referenced through glob import #141256

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

Closed
greeble-dev opened this issue May 19, 2025 · 4 comments · Fixed by #141548
Closed

Diagnostic doesn't mention cfg-ed item referenced through glob import #141256

greeble-dev opened this issue May 19, 2025 · 4 comments · Fixed by #141548
Labels
A-cfg Area: `cfg` conditional compilation A-diagnostics Area: Messages for errors, warnings, and lints D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@greeble-dev
Copy link

greeble-dev commented May 19, 2025

Code

pub mod original {
    #[cfg(FALSE)]
    pub mod gated {
        pub fn foo() {}
    }
}

pub mod reexport {
    pub use super::original::*;
}

pub fn main() {
    reexport::gated::foo();

    // A direct reference produces the desired output.
    //original::gated::foo();
}

Current output

error[E0433]: failed to resolve: could not find `gated` in `reexport`
  --> <source>:14:15
   |
14 |     reexport::gated::foo();
   |               ^^^^^ could not find `gated` in `reexport`

Desired output

error[E0433]: failed to resolve: could not find `gated` in `reexport`
  --> <source>:14:15
   |
17 |     reexport::gated::foo();
   |               ^^^^^ could not find `gated` in `reexport`
   |
note: found an item that was configured out
  --> <source>:3:13
   |
3  |     pub mod gated {
   |             ^^^^^
note: the item is gated here
  --> <source>:2:5
   |
2  |     #[cfg(FALSE)]
   |     ^^^^^^^^^^^^^

Rationale and extra context

If an item in a cfg gated module is directly referenced then the diagnostic will mention the gate. This does not happen if the gated module is re-exported through a glob import.

Rust Version

rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-unknown-linux-gnu
release: 1.86.0
LLVM version: 19.1.7
rustc 1.89.0-nightly (777d37277 2025-05-17)
binary: rustc
commit-hash: 777d372772aa3b39ba7273fcb8208a89f2ab0afd
commit-date: 2025-05-17
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.4

Anything else?

Compiler Explorer: https://godbolt.org/z/d3rr35G4T

@greeble-dev greeble-dev 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 May 19, 2025
@Urgau
Copy link
Member

Urgau commented May 19, 2025

For prioritization purpose: was this encountered in real code or is this more theoretical?

@greeble-dev
Copy link
Author

greeble-dev commented May 19, 2025

@Urgau, this was encountered in real code. Roughly, an omnibus crate that wants to let users depend only on the omnibus crate and import omnibus::foo, omnibus::bar etc, even though behind the scenes foo and bar are from other crates that might be feature gated. One step in this chain is a glob import.

In case it's useful for context, here's a PR for the omnibus that adds some details and pitches a workaround: bevyengine/bevy#19297.

@alice-i-cecile
Copy link

Bevy maintainer here: this is real code, and users have tripped over this multiple times. It's easy to teach them what to do if they reach out for help, but the compiler should be the first line of defense here :) We have this setup due to our dynamic linking setup, which is in place to improve compile times.

@xizheyin
Copy link
Contributor

That should be the problem, I need to investigate further.

pub(crate) fn find_similarly_named_module_or_crate(
&mut self,
ident: Symbol,
current_module: Module<'ra>,
) -> Option<Symbol> {
let mut candidates = self
.extern_prelude
.keys()
.map(|ident| ident.name)
.chain(
self.module_map
.iter()
.filter(|(_, module)| {
current_module.is_ancestor_of(**module) && current_module != **module
})
.flat_map(|(_, module)| module.kind.name()),
)
.filter(|c| !c.to_string().is_empty())
.collect::<Vec<_>>();
candidates.sort();
candidates.dedup();
find_best_match_for_name(&candidates, ident, None).filter(|sugg| *sugg != ident)
}

@Urgau Urgau added P-medium Medium priority A-cfg Area: `cfg` conditional compilation labels May 20, 2025
@fmease fmease added the D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. label May 21, 2025
tgross35 added a commit to tgross35/rust that referenced this issue May 28, 2025
consider glob imports in cfg suggestion

Fixes rust-lang#141256

r? `@petrochenkov`
jhpratt added a commit to jhpratt/rust that referenced this issue May 28, 2025
consider glob imports in cfg suggestion

Fixes rust-lang#141256

r? ``@petrochenkov``
@bors bors closed this as completed in ae95246 May 28, 2025
rust-timer added a commit that referenced this issue May 28, 2025
Rollup merge of #141548 - bvanjoi:issue-141256, r=petrochenkov

consider glob imports in cfg suggestion

Fixes #141256

r? ```@petrochenkov```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cfg Area: `cfg` conditional compilation A-diagnostics Area: Messages for errors, warnings, and lints D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants