Ty lecture

The many kinds of types

  • HIR types (rustdoc): describe the syntax of a type
  • fn foo(x: u32) → u32 { } — these two instances of u32 are distinct HIR types; they have distinct spans, etc
  • fn foo(x: &u32) -> &u32) — the & here 

fn foo(x: Ty<'tcx>) {
  match x.sty {
  }
}

  • There are a lot of related types, we’ll cover these in time
  • regions (aka, lifetimes)
  • “substitutions”
  • etc

Interning

  • We create a LOT of types during compilation
  • They are allocated from a long-lived arena
  • and they are canonicalized
  • CtxtInterners (link)
  • PartialEq for TyS (link) can just compare pointers

- Global <-- types that are global (u32) get allocated here
  - Type-checking a specific fn
    - local arena <-- types specific to type-checking

Generics and substitutions

  • Consider this (generic) struct:
struct MyStruct<T> { x: u32, y: T }
//     ^ def_id1
//              ^ def_id2
// def_id => "Def path" => crate::foo::MyStruct
  • A reference to this struct (e.g., MyStruct<u32>) is as TyKind::Adt (link)
  • Adt(&'tcx AdtDef, SubstsRef<'tcx>),
  • The AdtDef defines the struct (MyStruct) (enums, unions)
  • could also be a DefId, but the AdtDef struct has useful helper methods
  • tcx.adt_def(def_id) (link) to get one
  • The SubstsRef (link) is an interned list of types ([u32])
  • &'tcx List<Kind<'tcx>>
  • [u32, f32] — list a 
  • [f32] — subslice of a
  • [f32] — list b
  • &'tcx [Ty<'tcx>] “morally 1.0”
  • &'tcx [Kind<'tcx>] “morally 2.0” — kinds are either types or regions
  • well, actually a list of kinds — we’ll come to that
  • What is the Generics struct (link)?
  • Defines the type parameters

Unsubstituted Generics