Skip to main content
The Rust compiler consists of 75 specialized crates that work together to compile Rust code. Each crate has a specific responsibility in the compilation pipeline.

Core Infrastructure Crates

The main compiler binary that orchestrates the entire compilation process. Acts as the entry point for the rustc executable.Dependencies: rustc_driver, rustc_driver_impl, rustc_codegen_ssa, rustc_public, rustc_public_bridge
An intentionally minimal re-export crate of rustc_driver_impl that allows parallel compilation of driver implementation code.
Contains the actual implementation of the compiler driver, including command-line argument parsing and compilation orchestration.
Provides the main interface for driving the compiler, including the query system initialization and compilation passes.
Manages the compilation session, including configuration, diagnostics state, and global compiler settings.

Data Structures and Utilities

Various data structures used by the Rust compiler. This crate is designed to be generic and not specific to rustc for easier unit testing.Includes specialized collections, synchronization primitives, and compiler-specific data structures.
A fast but limited type of allocator. Arenas destroy all objects within at once when the arena itself is destroyed. They don’t support deallocation of individual objects while the arena is alive.Used extensively for allocating compiler data structures that share the same lifetime.
Provides newtype wrappers for indices, including IndexVec for vectors indexed by custom types.
Procedural macros for generating index types and related functionality.
Specialized hash functions optimized for compiler use cases.
Serialization and deserialization infrastructure for compiler data structures.

Abstract Syntax Tree (AST)

The Rust Abstract Syntax Tree (AST) definition. This is the initial representation of Rust source code after parsing.Note: This API is completely unstable and subject to change.
Shared intermediate representation types used by both AST and HIR.
Converts the Abstract Syntax Tree (AST) into High-Level Intermediate Representation (HIR).Key dependencies: rustc_ast, rustc_hir, rustc_middle, rustc_span
Various analysis passes that run on the AST, including validation and attribute checking.
Pretty printing for AST nodes, useful for debugging and diagnostics.

High-Level Intermediate Representation (HIR)

HIR datatypes. HIR is a desugared and more compiler-friendly representation of Rust code.See the rustc dev guide for more info.
Identifier types for HIR nodes.
Type checking and analysis on HIR, including trait resolution setup and coherence checking.
Type checking for HIR expressions, patterns, and function bodies.
Pretty printing for HIR nodes.

Mid-Level Intermediate Representation (MIR)

Builds MIR (Mid-level Intermediate Representation) from THIR (Typed High-Level IR).
Dataflow analysis framework for MIR, used by borrowck and other analyses.
Optimization and transformation passes on MIR, including inlining, dead code elimination, and constant propagation.

Type System and Inference

The “main crate” of the Rust compiler. Contains common type definitions used by other crates in the rustc family.Includes:
  • Type system definitions (ty module)
  • MIR definitions
  • Query system declarations
  • Trait system types
Key dependencies: Extensive - nearly every compiler crate
Core type system intermediate representation, shared across different compiler contexts.
Procedural macros for type system IR.
Type inference engine for Rust. Handles low-level equality and subtyping operations.The type check pass is found in rustc_hir_analysis.
Trait resolution implementation, including trait selection, projection, and method resolution.For more information, see the rustc-dev-guide.
Query implementations for trait-related operations.
Next-generation trait solver implementation (experimental).
Various type-related utility functions and query implementations.

Borrow Checking

MIR type checking and borrow checking implementation. Ensures memory safety by validating lifetimes and borrows.Key dependencies: rustc_middle, rustc_mir_dataflow, rustc_infer, polonius-engine

Code Generation

Shared code generation abstractions used by all codegen backends. Provides a common interface for code generation.
LLVM-based code generation backend for rustc. The primary and most mature backend.Key dependencies: rustc_codegen_ssa, rustc_llvm, rustc_middle, rustc_targetFeatures: llvm_enzyme, llvm_offload
GCC-based code generation backend (experimental, excluded from main workspace).
Cranelift-based code generation backend for faster debug builds (experimental, excluded from main workspace).
Rust bindings to LLVM for code generation.
Monomorphization of generic functions - creates concrete versions of generic code for each type.
Symbol name mangling for generated code to avoid name conflicts.

Lexing and Parsing

Breaks source code into tokens. Low-level lexer with minimal dependencies.
Parses Rust source code into an Abstract Syntax Tree (AST).
Parser for format strings used in format!, println!, etc.

Name Resolution

Responsible for name resolution that doesn’t require the type checker.
  • Builds module structure
  • Resolves paths in macros, imports, expressions, types, patterns
  • Resolves label and lifetime names
Checks privacy rules and computes effective visibilities.

Macro Expansion

Macro expansion engine, including declarative macros and procedural macro support.
Implementation of built-in macros like println!, derive, cfg!, etc.
Procedural macros used internally by the compiler, including query system macros.
Runtime support for procedural macros.

Analysis Passes

Various analysis and validation passes, including:
  • Dead code detection
  • Reachability analysis
  • Stability checking
  • Language item collection
  • Entry point detection
Note: This API is completely unstable and subject to change.
Compile-time evaluation of constant expressions and validation of const contexts.
Exhaustiveness checking and usefulness analysis for pattern matching.
Safety checking for transmute operations.

Incremental Compilation and Queries

Implementation of the demand-driven query system, including caching and dependency tracking.Supports serializing the dependency graph for incremental compilation.
Incremental compilation support, including dependency graph management.

Metadata and Linking

Encoding and decoding of crate metadata for separate compilation.
Target platform specifications and code generation parameters.Contains information about different compilation targets, including platform ABIs, calling conventions, and linker settings.
Application Binary Interface (ABI) definitions for different platforms and types.

Diagnostics and Error Reporting

Diagnostic creation and emission infrastructure. Handles all compiler warnings and errors.
Definitions of all compiler error codes (E0001, E0002, etc.).
Internationalization and message formatting for error diagnostics.

Source Code Management

Source positions and related helper functions.Important concepts:
  • Spans (represented by SpanData)
  • Source file management
  • Macro expansion tracking
Feature gate definitions and checking for unstable features.
Parsing and validation of Rust attributes.

Linting

Lint checking infrastructure and built-in lints.
Definitions of all built-in lints.

Specialized Utilities

File system utilities for the compiler.
Utilities for generating Graphviz visualizations of compiler data structures.
Logging infrastructure for the compiler.
Support for AddressSanitizer, ThreadSanitizer, and other sanitizers.
Thread pool for parallel compilation.
Windows resource compilation support.
Internationalization data for the compiler.

Public API

Public API for compiler consumers.
Bridge between the internal compiler API and the public stable MIR API.Intended to be used by stable MIR consumers that are not in-tree.

Crate Dependency Graph

The Rust compiler has 75 crates working together. This modular architecture allows for:
  • Parallel compilation
  • Clear separation of concerns
  • Easier maintenance and testing
  • Flexible backend support (LLVM, GCC, Cranelift)

Compiler Passes

Learn about the compilation pipeline and analysis passes

Query System

Understand the demand-driven query system

Build docs developers (and LLMs) love