Skip to content

Add effective visibility to crate metadata - #166653

Open
N1ark wants to merge 5 commits into
rust-lang:mainfrom
N1ark:metadata-exported
Open

N1ark wants to merge 5 commits into
rust-lang:mainfrom
N1ark:metadata-exported

Conversation

@N1ark

@N1ark N1ark commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Fixes #90258 -- that issue was closed but it still occurs, I think it was "fixed" by the leaked trait RingSlices being removed, not by actually having it's cause fixed :p

Also fixes a diagnostic that considered a foreign trait not provided by that module to be sealed -- see bf3fd5f

We currently don't handle foreign traits correctly when suggesting candidates, as we check visibility which returns whether the trait is pub but not whether it's actually exported by the crate.

A solution is then to encode effective visibility into crate metadata (as tcx.effective_visibilities only works on local items). This makes crate metadata slightly bigger but hopefully should be negligible, as it's a simple 4 variant enum. a perf run might be needed

Additionally, this also makes life easier for out-of-tree tools that want to learn effective visibility of items -- in Charon, we have to copy a quite nasty logic from rustc to be able to get this back ^^' fwiw rustdoc does a similar "is this visible" walk here but in that case we also need to check doc(hidden) so i don't think it's equivalent or replaceable

Reproducer:

// dep.rs
mod private {
    pub trait Tr {
        fn method(&self) {}
    }
    impl Tr for u8 {}
}
// main.rs
use dep as _;

fn main() {
    0u8.method();
}
$ rustc --edition 2021 --crate-type lib dep.rs
$ rustc --edition 2021 main.rs --extern dep=libdep.rlib
error[E0599]: no method named `method` found for type `u8` in the current scope
 --> main.rs:4:9
  |
4 |     0u8.method();
  |         ^^^^^^ method not found in `u8`
  |
 ::: dep.rs:3:12
  |
3 |         fn method(&self) {}
  |            ------ the method is available for `u8` here
  |
  = help: items from traits can only be used if the trait is in scope
help: trait `Tr` which provides `method` is implemented but not in scope; perhaps you want to import it
  |
1 + use dep::private::Tr;
  |

LLM Disclosure: this was implemented with assistance from an LLM as I am unfamiliar with the implementation of the query and metadata systems. I have of course reviewed all code and rewritten all comments etc.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 27, 2026
@rustbot

rustbot commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

r? @khyperia

rustbot has assigned @khyperia.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 79 candidates
  • Random selection from 21 candidates

@N1ark N1ark changed the title Fix import suggestion for unnameable traits Add effective visibility to crate metadata Nov 27, 2026
Comment on lines +293 to +299
impl<'tcx> TyCtxt<'tcx> {
/// Whether the item is public at the given effective visibility level.
/// Unlike [`EffectiveVisibilities::is_public_at_level`], this works on foreign items.
pub fn is_public_at_level(self, def_id: DefId, level: Level) -> bool {
self.effective_visibility_level(def_id).is_some_and(|l| l >= level)
}
}

@N1ark N1ark Nov 27, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

btw i added this to match EffectiveVisibilities::is_public_at_level and for convenience but i don't know exactly what's the policy on adding functions to TyCtxt -- happy to remove it to avoid cluttering the already large amount of inherent methods

View changes since the review

Instead of checking whether the supertrait is in the visible parent map, which only contains items reachable from direct dependencies, check whether it is nameable from outside its crate, like we already do for local traits.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. 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.

Rustc suggests importing trait inside private module

3 participants