|
| 1 | +# The THIR |
| 2 | + |
| 3 | +<!-- toc --> |
| 4 | + |
| 5 | +The THIR ("Typed High-Level Intermediate Representation"), previously HAIR for |
| 6 | +"High-Level Abstract IR", is another IR used by rustc that is generated after |
| 7 | +[type checking]. It is (as of <!-- date: 2021-03 --> March 2021) only used for |
| 8 | +[MIR construction] and [exhaustiveness checking], but |
| 9 | +[it may also soon be used for unsafety checking][thir-unsafeck] as a replacement |
| 10 | +for the current MIR unsafety checker. |
| 11 | + |
| 12 | +[type checking]: ./type-checking.md |
| 13 | +[MIR construction]: ./mir/construction.md |
| 14 | +[exhaustiveness checking]: ./pat-exhaustive-checking.md |
| 15 | +[thir-unsafeck]: https://github.com/rust-lang/compiler-team/issues/402 |
| 16 | + |
| 17 | +As the name might suggest, the THIR is a lowered version of the [HIR] where all |
| 18 | +the types have been filled in, which is possible after type checking has completed. |
| 19 | +But it has some other interesting features that distinguish it from HIR: |
| 20 | +- like the MIR, the THIR only represents bodies, i.e. "executable code"; this includes |
| 21 | + function bodies, but also `const` initializers, for example. Consequently, the THIR |
| 22 | + has no representation for items like `struct`s or `trait`s. |
| 23 | +- a body of THIR is only stored temporarily and is dropped as soon as it's no longer |
| 24 | + needed, as opposed to being stored until the end of the compilation process (which |
| 25 | + is what is done with the HIR). |
| 26 | +- besides making the types of all nodes available, the THIR also has additional |
| 27 | + desugaring compared to the HIR. For example, automatic references and dereferences |
| 28 | + are made explicit, and method calls and overloaded operators are converted into |
| 29 | + plain function calls. Destruction scopes are also made explicit. |
| 30 | + |
| 31 | +[HIR]: ./hir.md |
| 32 | + |
| 33 | +The THIR lives in [`rustc_mir_build::thir`][thir]. To construct a `thir::Expr`, |
| 34 | +you can use the `build_thir` function, passing in the memory arena where the THIR |
| 35 | +will be allocated. Dropping this arena will result in the THIR being destroyed: |
| 36 | +this is useful to keep peak memory in check, as having a THIR representation of |
| 37 | +all bodies of a crate in memory at the same time would be very heavy. |
| 38 | + |
| 39 | +[thir]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir_build/thir/index.html |
0 commit comments