Skip to content

Commit 4c8fdcf

Browse files
LeSeulArtichautJoshua Nelson
authored and
Joshua Nelson
committed
Add stub about the THIR
1 parent 674f73d commit 4c8fdcf

File tree

3 files changed

+45
-19
lines changed

3 files changed

+45
-19
lines changed

src/SUMMARY.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@
8484
- [The HIR (High-level IR)](./hir.md)
8585
- [Lowering AST to HIR](./lowering.md)
8686
- [Debugging](./hir-debugging.md)
87+
- [The THIR (Typed High-level IR)](./thir.md)
8788
- [The MIR (Mid-level IR)](./mir/index.md)
88-
- [THIR and MIR construction](./mir/construction.md)
89+
- [MIR construction](./mir/construction.md)
8990
- [MIR visitor and traversal](./mir/visitor.md)
9091
- [MIR passes: getting the MIR for a function](./mir/passes.md)
9192
- [Identifiers in the Compiler](./identifiers.md)

src/mir/construction.md

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# THIR and MIR construction
1+
# MIR construction
22

33
<!-- toc -->
44

@@ -13,23 +13,9 @@ list of items:
1313
* Drop code (the `Drop::drop` function is not called directly)
1414
* Drop implementations of types without an explicit `Drop` implementation
1515

16-
The lowering is triggered by calling the [`mir_built`] query.
17-
There is an intermediate representation
18-
between [HIR] and [MIR] called the [THIR] that is only used during lowering.
19-
[THIR] means "Typed HIR" and used to be called "HAIR (High-level Abstract IR)".
20-
The [THIR]'s most important feature is that the various adjustments (which happen
21-
without explicit syntax) like coercions, autoderef, autoref and overloaded method
22-
calls have become explicit casts, deref operations, reference expressions or
23-
concrete function calls.
24-
25-
The [THIR] is a shallow wrapper around [HIR], with datatypes that mirror the [HIR] datatypes.
26-
For example, instead of `-x` being a `thir::ExprKind::Neg(thir::Expr)`
27-
(a deep copy), it is a `thir::ExprKind::Neg(hir::Expr)` (a shallow copy).
28-
This shallowness enables the [THIR] to represent all datatypes that [HIR] has, but
29-
without having to create an in-memory copy of the entire [HIR].
30-
[MIR] lowering will first convert the topmost expression from
31-
[HIR] to [THIR] (in [`rustc_mir_build::thir::cx::expr`]) and then process
32-
the [THIR] expressions recursively.
16+
The lowering is triggered by calling the [`mir_built`] query. The MIR builder does
17+
not actually use the HIR but operates on the [THIR] instead, processing THIR
18+
expressions recursively.
3319

3420
The lowering creates local variables for every argument as specified in the signature.
3521
Next, it creates local variables for every binding specified (e.g. `(a, b): (i32, String)`)

src/thir.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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

Comments
 (0)