Skip to main content

Crate rustc_public

Crate rustc_public 

Source
Expand description

§rustc_public — A Public Interface to rustc

This crate provides a public API for querying and analyzing Rust programs through the compiler’s internal representations. It is designed for third-party tools such as verification engines, linters, and code generators that need access to type information, MIR bodies, monomorphized instances, and ABI details.

The goal is to publish this crate on crates.io with semver guarantees. For more details on the proposed plan, see https://github.com/rust-lang/compiler-team/issues/949.

§Usage

For now, the entry point is the run! macro, which sets up the compiler session and provides access to the API within a callback. All queries must be performed inside this callback since the data structures are tied to the compiler’s thread-local state.

use rustc_public::*;
use std::ops::ControlFlow;

let result = run!(args, || -> ControlFlow<()> {
    // Find all crates with the same name (potential duplicates).
    for krate in external_crates() {
        let dupes = find_crates(&krate.name);
        if dupes.len() > 1 {
            println!("Warning: multiple versions of `{}`", krate.name);
        }
    }
    ControlFlow::Continue(())
});

§Crate Discovery

Use local_crate() to access the crate being compiled, external_crates() to list dependencies, or find_crates() to search by name. Use entry_fn() to find the program entry point, and all_local_items() to retrieve all local definitions.

§Status

This API is not yet published and is still subject to breaking changes. For more information, see https://github.com/rust-lang/rustc_public.

Re-exports§

pub use crate::crate_def::CrateDef;
pub use crate::crate_def::CrateDefType;
pub use crate::crate_def::DefId;
pub use crate::error::*;

Modules§

abi
alloc 🔒
Memory allocation implementation for rustc_public.
compiler_interface
Define the interface with the Rust compiler.
crate_def
Module that define a common trait for things that represent a crate definition, such as, a function, a trait, an enum, and any other definitions.
error
When things go wrong, we need some error handling. There are a few different types of errors in rustc_public:
mir
rustc_internal
Unstable internal APIs for bridging with rustc internals.
target
Provide information about the machine that this is being compiled into.
ty
unstable 🔒
Module that collects the things that have no stability guarantees.
visitor

Macros§

bridge_impl 🔒
run
Instantiate and run the compiler with the provided arguments and callback.
run_with_tcx
Instantiate and run the compiler with the provided arguments and callback.

Structs§

Crate
Metadata about a crate in the current compilation.
CrateItem
A definition in the local crate (function, static, const, or constructor).
CrateNum
A unique identifier for a crate within the current compilation session.
Opaque
An opaque wrapper around internal compiler data.
ThreadLocalIndex 🔒
Marker type for indexes into thread local structures.

Enums§

CtorKind
The kind of a data constructor.
ItemKind
The kind of a CrateItem.

Constants§

ThreadLocalIndex 🔒
Emulating unit struct struct ThreadLocalIndex;

Functions§

all_local_items
Return all items in the local crate that have a MIR body.
all_trait_decls
Return all trait declarations from the local crate and all its dependencies.
all_trait_impls
Return all trait implementations from the local crate and all its dependencies.
entry_fn
Return the program entry point (usually main) if defined in the local crate.
external_crates
Return all external (non-local) crates in the compilation.
find_crates
Find all crates matching the given name.
local_crate
Return the local crate (the crate currently being compiled).
opaque
Create an Opaque value from any debuggable type.

Type Aliases§

AssocItems
A collection of associated items (methods, constants, or types within a trait or impl).
CrateItems
A collection of items defined in a crate.
Filename
A file path string used for source locations and diagnostics.
ImplTraitDecls
A collection of trait implementation blocks.
Symbol
A symbol name (e.g., function name, crate name), currently represented as a String.
TraitDecls
A collection of trait declarations.