RustcContributor::explore @compiler-errors session: method selection

Explore the inner workings of the Rust compiler's method selection process, from trait lookup to type checking and auto-dereffing, and discover how it handles edge cases and optimization techniques.

Key takeaways
  • The Rust compiler’s method selection process involves several steps, including:
    • Checking for traits in scope and assembling candidates
    • Assembling extension candidates for traits in scope
    • Using the pick method to select the best method
    • Checking the method’s where clauses and type checking
  • The pick method uses a recursive descent algorithm to select the best method, considering inherent and extension methods, and applying auto-dereffing and auto-gref
  • The method selection process is influenced by the type of the receiver, with special handling for raw pointers, arrays, and slices
  • The compiler uses a probe scope to assemble candidates and perform method selection, and a separate probe context to store information about the method call
  • The compiler also uses a lookup function to resolve method calls, which involves name resolution and trait lookup
  • The method selection process is affected by the where clauses of the method, and the compiler uses a predicate function to check whether the where clauses hold
  • The compiler uses a confirm function to confirm the selected method, which involves type checking and auto-dereffing
  • The method selection process is also influenced by the type of the method being called, with special handling for static functions and methods with generic types
  • The compiler uses a TDM (type-dependent metadata) to store information about the method call, including the method’s signature and where clauses
  • The method selection process is implemented in several stages, including:
    • Assembling candidates
    • Selecting the best method
    • Checking the method’s where clauses
    • Type checking
    • Auto-dereffing and auto-gref
    • Confirming the selected method
  • The compiler uses several special rules to handle edge cases, including:
    • Handling raw pointers and arrays
    • Handling slices and unsized types
    • Handling generic types and method calls
    • Handling static functions and methods
  • The method selection process is optimized for performance, using a combination of recursive descent and memoization to avoid redundant work.