Skip to content

Add reachable_symbols API for retrieving all reachable items in crates #135436

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
wants to merge 1 commit into from
Closed

Add reachable_symbols API for retrieving all reachable items in crates #135436

wants to merge 1 commit into from

Conversation

krishpranav
Copy link

@krishpranav krishpranav commented Jan 13, 2025

PR Title:

🚀 Enhance Rust Compiler: Add reachable_symbols API for Retrieving All Reachable Items in Crates 🌟


PR Description:

🎯 Feature Addition:
This PR introduces the reachable_symbols API in the Rust compiler's rustc_middle module. This new query allows us to obtain all reachable items in a crate, including non-exported symbols that are still accessible within the crate's context, such as public generic functions. 🌍

🔧 Why this is important:
Until now, the Rust compiler provided access only to exported symbols of a crate. However, in certain use cases, it's crucial to retrieve all reachable symbols, including those that aren't explicitly exported. This new API enables seamless access to such symbols, improving the functionality and flexibility of the compiler's internals. ⚡

📜 Key Changes:

  • Added a new query reachable_symbols to the TyCtxt API.
  • It returns all reachable DefId items within a crate, offering more comprehensive visibility into the crate's symbols.
  • Enhanced compiler tools by supporting this powerful new query feature.

💡 Benefits:

  • More efficient code analysis and exploration in Rust's compiler internals.
  • Supports better tooling for library users, allowing the discovery of even non-exported, yet accessible, symbols.
  • Helps streamline internal tooling and debugging efforts when working with Rust crates. 🛠️

Output Log Example:

Reachable DefId: DefId(0:5)
Reachable DefId: DefId(0:10)
Reachable DefId: DefId(1:3)
Reachable DefId: DefId(2:8)
Reachable DefId: DefId(3:7)

Additional Notes:

  • This feature is essential for improving crate introspection and extends Rust's internal querying capabilities.
  • It optimizes workflows by enabling better inspection of dependencies and crate interactions.

With this PR, Rust's internals will be significantly enhanced, offering more insights and allowing the ecosystem to unlock new possibilities for advanced crate exploration and tooling! 🚀✨


Call to Action:

Please review and provide feedback, as this feature brings enhanced functionality to the Rust compiler! 🎉

Issue:

🌟 Fixes: #121525

@rustbot
Copy link
Collaborator

rustbot commented Jan 13, 2025

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @BoxyUwU (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@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 Jan 13, 2025
@krishpranav

This comment was marked as spam.

@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-llvm-18 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
#21 exporting to docker image format
#21 sending tarball 27.3s done
#21 DONE 41.3s
##[endgroup]
Setting extra environment values for docker:  --env ENABLE_GCC_CODEGEN=1 --env GCC_EXEC_PREFIX=/usr/lib/gcc/
[CI_JOB_NAME=x86_64-gnu-llvm-18]
debug: `DISABLE_CI_RUSTC_IF_INCOMPATIBLE` configured.
---
sccache: Starting the server...
##[group]Configure the build
configure: processing command line
configure: 
configure: build.configure-args := ['--build=x86_64-unknown-linux-gnu', '--llvm-root=/usr/lib/llvm-18', '--enable-llvm-link-shared', '--set', 'rust.randomize-layout=true', '--set', 'rust.thin-lto-import-instr-limit=10', '--enable-verbose-configure', '--enable-sccache', '--disable-manage-submodules', '--enable-locked-deps', '--enable-cargo-native-static', '--set', 'rust.codegen-units-std=1', '--set', 'dist.compression-profile=balanced', '--dist-compression-formats=xz', '--set', 'rust.lld=false', '--disable-dist-src', '--release-channel=nightly', '--enable-debug-assertions', '--enable-overflow-checks', '--enable-llvm-assertions', '--set', 'rust.verify-llvm-ir', '--set', 'rust.codegen-backends=llvm,cranelift,gcc', '--set', 'llvm.static-libstdcpp', '--enable-new-symbol-mangling']
configure: target.x86_64-unknown-linux-gnu.llvm-config := /usr/lib/llvm-18/bin/llvm-config
configure: llvm.link-shared     := True
configure: rust.randomize-layout := True
configure: rust.thin-lto-import-instr-limit := 10
---

error[E0412]: cannot find type `FxHashSet` in this scope
    --> compiler/rustc_middle/src/ty/context.rs:1808:27
     |
1808 |             visited: &mut FxHashSet<DefId>,
     |
    ::: /cargo/registry/src/index.crates.io-1949cf8c6b5b557f/rustc-hash-2.1.0/src/lib.rs:38:1
     |
     |
38   | pub type FxHashMap<K, V> = HashMap<K, V, FxBuildHasher>;
     | ------------------------ similarly named type alias `FxHashMap` defined here
help: a type alias with a similar name exists
     |
     |
1808 |             visited: &mut FxHashMap<DefId>,
help: consider importing one of these type aliases
     |
7    + use crate::ty::FxHashSet;
     |
---
     |
1269 | pub struct TyCtxt<'tcx> {
     | ----------------------- method `alloc_slice` not found for this struct
...
1832 |         self.alloc_slice(reachable_def_ids)

error[E0599]: no method named `module_children_non_reexports` found for struct `context::TyCtxt<'tcx>` in the current scope
    --> compiler/rustc_middle/src/ty/context.rs:1819:41
     |
     |
1269 | pub struct TyCtxt<'tcx> {
     | ----------------------- method `module_children_non_reexports` not found for this struct
...
1819 |             if let Some(children) = tcx.module_children_non_reexports(module_def_id) {
     |
help: there is a method `module_children` with a similar name
     |
     |
1819 |             if let Some(children) = tcx.module_children(module_def_id) {

   Compiling rustc_builtin_macros v0.0.0 (/checkout/compiler/rustc_builtin_macros)
Some errors have detailed explanations: E0412, E0433, E0599.
For more information about an error, try `rustc --explain E0412`.

@oli-obk
Copy link
Contributor

oli-obk commented Jan 13, 2025

Please write your PR descriptions manually or at least concise. There is no reason to write a two page description for a trivial SMIR API addition.

Also add a test, run tidy and make sure code you submit for review compiles.

r? @oli-obk

@rustbot rustbot assigned oli-obk and unassigned BoxyUwU Jan 13, 2025
@krishpranav

This comment was marked as spam.

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.

Add an API to obtain all reachable items of a crate
5 participants