The Rust compiler processes code through a series of transformation passes, converting source code into executable binaries. Each pass performs specific analyses or transformations.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/rust-lang/rust/llms.txt
Use this file to discover all available pages before exploring further.
Compilation Pipeline Overview
Early Passes
Lexing and Parsing
parse - Source Code Parsing
parse - Source Code Parsing
rustc_interface::passes::parseThe first major pass that converts source code into an Abstract Syntax Tree (AST).Process:- Creates a parser from file or string input
- Tokenizes the source code (lexing)
- Parses tokens into AST nodes
- Injects command-line attributes into the crate
Input::File or Input::Str)Output: ast::CrateError Handling: Parse errors are emitted immediately and cause compilation to abort.pre_expansion_lint - Early Lint Checks
pre_expansion_lint - Early Lint Checks
rustc_interface::passes::pre_expansion_lintRuns lints on the AST before macro expansion. These lints check for issues that should be caught early.Timing: Before macro expansionInput: AST nodes, attributes, featuresLints Checked: Built-in pre-expansion lints from rustc_lint::BuiltinCombinedPreExpansionLintPassMacro Processing
configure_and_expand - Macro Expansion
configure_and_expand - Macro Expansion
rustc_interface::passes::configure_and_expandRuns the “early phases” of compilation: cfg processing, syntax expansion, and name resolution.Major Steps:- Crate Injection - Injects standard library imports
- Macro Expansion - Expands all macros recursively
- Test Harness - Injects test harness if
--testis specified - AST Validation - Validates the expanded AST
- Proc Macro Harness - Injects procedural macro runtime
- Name Resolution - Resolves all names and paths
- Respects recursion limits
- Handles
cfgattributes - Processes feature gates
ast::Crate, ResolverOutput: Fully expanded ast::Crate with all macros expandedexpand_crate - Macro Expansion Core
expand_crate - Macro Expansion Core
configure_and_expandThe core macro expansion pass that:- Expands declarative macros (
macro_rules!) - Calls procedural macros
- Expands built-in macros like
println!,derive, etc. - Handles macro imports and exports
#![recursion_limit] attributeAnalysis Passes
HIR Construction and Analysis
resolver_for_lowering_raw - Resolution for Lowering
resolver_for_lowering_raw - Resolution for Lowering
rustc_interface::passes::resolver_for_lowering_rawQuery: resolver_for_lowering_rawPrepares resolution data needed for AST lowering to HIR.Output:ResolverAstLowering- Resolution tables for lowering- Expanded
ast::Crate ResolverGlobalCtxt- Global resolution context
hir_crate - HIR Construction
hir_crate - HIR Construction
hir_crateLowers the expanded AST into High-Level Intermediate Representation (HIR).Performed by: rustc_ast_loweringTransformations:- Desugars complex syntax into simpler forms
- Converts
forloops toloop+match - Expands question mark operator (
?) - Normalizes closure syntax
Crate<'tcx> - The HIR crateearly_lint_checks - Early HIR Lints
early_lint_checks - Early HIR Lints
rustc_interface::passes::early_lint_checksQuery: early_lint_checksRuns lints that operate on the fully expanded AST and early HIR.Timing: After expansion, before type checkingType Checking and Inference
Type Collection
Type Collection
type_of, generics_of, predicates_ofCollects type signatures from all items in the crate.Process:- Collect types from item signatures
- Build generic parameter lists
- Collect trait bounds and where clauses
- Validate type well-formedness
Type Checking (Typeck)
Type Checking (Typeck)
rustc_hir_typeckQueries: Per-function type checking queriesPerforms type inference and checking for function bodies.Major Components:- Expression type checking
- Pattern type checking
- Method resolution
- Operator overload resolution
- Type coercion
- Closure signature inference
Trait Resolution
Trait Resolution
rustc_trait_selectionResolves trait implementations and validates trait bounds.Operations:- Trait selection (finding applicable impls)
- Projection (resolving associated types)
- Method candidate resolution
- Impl overlap checking
- Coherence checking
MIR Passes
mir_built - MIR Construction
mir_built - MIR Construction
mir_builtPrimary Crate: rustc_mir_buildBuilds MIR (Mid-level Intermediate Representation) from THIR (Typed HIR).Steps:- Lower HIR to THIR (adds type information)
- Build MIR control flow graph
- Generate MIR for match expressions
- Insert drop glue
mir::Body<'tcx> - Unoptimized MIRBorrow Checking
Borrow Checking
rustc_borrowckQueries: mir_borrowckValidates memory safety through borrow checking.Checks:- Validates borrows don’t outlive referenced data
- Ensures no simultaneous mutable and immutable borrows
- Prevents use-after-move
- Validates initialization before use
MIR Optimization Passes
MIR Optimization Passes
rustc_mir_transformPerforms optimization passes on MIR.Major Optimizations:- Inlining - Inline small functions
- Constant Propagation - Evaluate constants at compile time
- Dead Code Elimination - Remove unreachable code
- Copy Propagation - Eliminate unnecessary copies
- Destination Propagation - Optimize memory destinations
- Simplify CFG - Simplify control flow graph
- InstCombine - Combine instructions
Additional Analysis Passes
Reachability Analysis
Reachability Analysis
rustc_passes::reachableDetermines which items are reachable from entry points.Used For:- Dead code detection
- Determining what to include in metadata
- Link-time optimization decisions
Dead Code Detection
Dead Code Detection
rustc_passes::deadQuery: Provided through rustc_passes::provideDetects unused code that can be removed or warned about.Checks:- Unused functions
- Unused imports
- Unused variables
- Unused struct fields (private only)
Privacy Checking
Privacy Checking
rustc_privacyValidates privacy rules (pub/private).Checks:- Private types in public signatures
- Access to private items
- Computes effective visibilities
Stability Checking
Stability Checking
rustc_passes::stabilityValidates use of stable/unstable features.Checks:- Unstable feature gates
- Deprecated item usage
- Stability attribute consistency
Entry Point Detection
Entry Point Detection
rustc_passes::entryFinds the entry point for the program (main function or custom entry point).Validates:- Entry point signature
- Only one entry point exists
- Entry point visibility
Code Generation Passes
Monomorphization
Monomorphization
rustc_monomorphizeCreates concrete versions of generic functions for each type they’re used with.Process:- Collect all generic items used
- Generate concrete versions with type parameters substituted
- Partition into codegen units
- Handle dynamic dispatch (trait objects)
MonoItems - Concrete items ready for codegenCodegen Unit Partitioning
Codegen Unit Partitioning
collect_and_partition_mono_itemsDivides monomorphized items into compilation units for parallel codegen.Strategies:- Per-module partitioning (faster compilation)
- Single unit (better optimization, slower compilation)
LLVM Code Generation
LLVM Code Generation
rustc_codegen_llvmGenerates LLVM IR from MIR.Steps:- Convert MIR to LLVM IR
- Apply LLVM optimizations
- Generate object files
Linking
Linking
- Collect all object files
- Link with runtime libraries
- Apply LTO (Link-Time Optimization) if enabled
- Produce final executable/library
Pass Organization in Code
rustc_passes Crate
Therustc_passes crate (compiler/rustc_passes/) contains various analysis passes:
Analysis Pass
The mainanalysis query coordinates type checking and other analyses:
Location: rustc_interface::passes::analysis
Query: analysis
Runs:
- Parallel type checking of all items
- Privacy checking
- Liveness analysis
- Intrinsic checking
- Variance inference
- Other whole-crate analyses
Pass Timing and Profiling
All major passes are instrumented with timing information:rustc -Z time-passes to see timing information for each pass.