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
rustcinternals. - 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.
- Crate
Item - A definition in the local crate (function, static, const, or constructor).
- Crate
Num - A unique identifier for a crate within the current compilation session.
- Opaque
- An opaque wrapper around internal compiler data.
- Thread
Local 🔒Index - Marker type for indexes into thread local structures.
Enums§
Constants§
- Thread
Local 🔒Index - 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
Opaquevalue from any debuggable type.
Type Aliases§
- Assoc
Items - A collection of associated items (methods, constants, or types within a trait or impl).
- Crate
Items - A collection of items defined in a crate.
- Filename
- A file path string used for source locations and diagnostics.
- Impl
Trait Decls - A collection of trait implementation blocks.
- Symbol
- A symbol name (e.g., function name, crate name), currently represented as a
String. - Trait
Decls - A collection of trait declarations.