update thir output (#1445)

This commit is contained in:
Tshepang Mbambo 2022-08-25 00:45:53 +02:00 committed by GitHub
parent db535c7aff
commit c9a2c851c9
1 changed files with 105 additions and 56 deletions

View File

@ -47,8 +47,18 @@ which is useful to keep peak memory in check. Having a THIR representation of
all bodies of a crate in memory at the same time would be very heavy. all bodies of a crate in memory at the same time would be very heavy.
You can get a debug representation of the THIR by passing the `-Zunpretty=thir-tree` flag You can get a debug representation of the THIR by passing the `-Zunpretty=thir-tree` flag
to `rustc`. Here is how a function with just the statement `let x = 1 + 2;` gets represented in to `rustc`.
THIR:
To demonstrate, let's use the following example:
```rust
fn main() {
let x = 1 + 2;
}
```
Here is how that gets represented in THIR (as of <!-- date-check --> Aug 2022):
```rust,no_run ```rust,no_run
Thir { Thir {
// no match arms // no match arms
@ -57,57 +67,73 @@ Thir {
// expression 0, a literal with a value of 1 // expression 0, a literal with a value of 1
Expr { Expr {
ty: i32, ty: i32,
temp_lifetime: Some(Node(6)), temp_lifetime: Some(
Node(1),
),
span: oneplustwo.rs:2:13: 2:14 (#0), span: oneplustwo.rs:2:13: 2:14 (#0),
kind: Literal { kind: Literal {
literal: Const { lit: Spanned {
ty: i32, node: Int(
val: Value(Scalar(0x00000001)), 1,
Unsuffixed,
),
span: oneplustwo.rs:2:13: 2:14 (#0),
}, },
user_ty: None, neg: false,
const_id: None,
}, },
}, },
// expression 1, scope surronding literal 1 // expression 1, scope surronding literal 1
Expr { Expr {
ty: i32, ty: i32,
temp_lifetime: Some(Node(6)), temp_lifetime: Some(
Node(1),
),
span: oneplustwo.rs:2:13: 2:14 (#0), span: oneplustwo.rs:2:13: 2:14 (#0),
kind: Scope { kind: Scope {
region_scope: Node(1),
lint_level: Explicit(HirId {
owner: DefId(0:3 ~ oneplustwo[6ccc]::main),
local_id: 1,
}),
// reference to expression 0 above // reference to expression 0 above
region_scope: Node(3),
lint_level: Explicit(
HirId {
owner: DefId(0:3 ~ oneplustwo[6932]::main),
local_id: 3,
},
),
value: e0, value: e0,
}, },
}, },
// expression 2, literal 2 // expression 2, literal 2
Expr { Expr {
ty: i32, ty: i32,
temp_lifetime: Some(Node(6)), temp_lifetime: Some(
Node(1),
),
span: oneplustwo.rs:2:17: 2:18 (#0), span: oneplustwo.rs:2:17: 2:18 (#0),
kind: Literal { kind: Literal {
literal: Const { lit: Spanned {
ty: i32, node: Int(
val: Value(Scalar(0x00000002)), 2,
Unsuffixed,
),
span: oneplustwo.rs:2:17: 2:18 (#0),
}, },
user_ty: None, neg: false,
const_id: None,
}, },
}, },
// expression 3, scope surrounding literal 2 // expression 3, scope surrounding literal 2
Expr { Expr {
ty: i32, ty: i32,
temp_lifetime: Some(Node(6)), temp_lifetime: Some(
Node(1),
),
span: oneplustwo.rs:2:17: 2:18 (#0), span: oneplustwo.rs:2:17: 2:18 (#0),
kind: Scope { kind: Scope {
region_scope: Node(2), region_scope: Node(4),
lint_level: Explicit(HirId { lint_level: Explicit(
owner: DefId(0:3 ~ oneplustwo[6ccc]::main), HirId {
local_id: 2, owner: DefId(0:3 ~ oneplustwo[6932]::main),
}), local_id: 4,
},
),
// reference to expression 2 above // reference to expression 2 above
value: e2, value: e2,
}, },
@ -115,7 +141,9 @@ Thir {
// expression 4, represents 1 + 2 // expression 4, represents 1 + 2
Expr { Expr {
ty: i32, ty: i32,
temp_lifetime: Some(Node(6)), temp_lifetime: Some(
Node(1),
),
span: oneplustwo.rs:2:13: 2:18 (#0), span: oneplustwo.rs:2:13: 2:18 (#0),
kind: Binary { kind: Binary {
op: Add, op: Add,
@ -127,30 +155,38 @@ Thir {
// expression 5, scope surronding expression 4 // expression 5, scope surronding expression 4
Expr { Expr {
ty: i32, ty: i32,
temp_lifetime: Some(Node(6)), temp_lifetime: Some(
Node(1),
),
span: oneplustwo.rs:2:13: 2:18 (#0), span: oneplustwo.rs:2:13: 2:18 (#0),
kind: Scope { kind: Scope {
region_scope: Node(3), region_scope: Node(5),
lint_level: Explicit(HirId { lint_level: Explicit(
owner: DefId(0:3 ~ oneplustwo[6ccc]::main), HirId {
local_id: 3, owner: DefId(0:3 ~ oneplustwo[6932]::main),
}), local_id: 5,
},
),
value: e4, value: e4,
}, },
}, },
// expression 6, block around statement // expression 6, block around statement
Expr { Expr {
ty: (), ty: (),
temp_lifetime: Some(Node(8)), temp_lifetime: Some(
Node(9),
),
span: oneplustwo.rs:1:11: 3:2 (#0), span: oneplustwo.rs:1:11: 3:2 (#0),
kind: Block { kind: Block {
body: Block { body: Block {
targeted_by_break: false, targeted_by_break: false,
region_scope: Node(7), region_scope: Node(8),
opt_destruction_scope: None, opt_destruction_scope: None,
span: oneplustwo.rs:1:11: 3:2 (#0), span: oneplustwo.rs:1:11: 3:2 (#0),
// reference to statement 0 below // reference to statement 0 below
stmts: [ s0 ], stmts: [
s0,
],
expr: None, expr: None,
safety_mode: Safe, safety_mode: Safe,
}, },
@ -160,25 +196,29 @@ Thir {
Expr { Expr {
ty: (), ty: (),
temp_lifetime: Some( temp_lifetime: Some(
Node(8), Node(9),
), ),
span: oneplustwo.rs:1:11: 3:2 (#0), span: oneplustwo.rs:1:11: 3:2 (#0),
kind: Scope { kind: Scope {
region_scope: Node(8), region_scope: Node(9),
lint_level: Explicit(HirId { lint_level: Explicit(
owner: DefId(0:3 ~ oneplustwo[6ccc]::main), HirId {
local_id: 8, owner: DefId(0:3 ~ oneplustwo[6932]::main),
}), local_id: 9,
},
),
value: e6, value: e6,
}, },
}, },
// destruction scope around expression 7 // destruction scope around expression 7
Expr { Expr {
ty: (), ty: (),
temp_lifetime: Some(Node(8)), temp_lifetime: Some(
Node(9),
),
span: oneplustwo.rs:1:11: 3:2 (#0), span: oneplustwo.rs:1:11: 3:2 (#0),
kind: Scope { kind: Scope {
region_scope: Destruction(8), region_scope: Destruction(9),
lint_level: Inherited, lint_level: Inherited,
value: e7, value: e7,
}, },
@ -188,8 +228,8 @@ Thir {
// let statement // let statement
Stmt { Stmt {
kind: Let { kind: Let {
remainder_scope: Remainder { block: 7, first_statement_index: 0}, remainder_scope: Remainder { block: 8, first_statement_index: 0},
init_scope: Node(6), init_scope: Node(1),
pattern: Pat { pattern: Pat {
ty: i32, ty: i32,
span: oneplustwo.rs:2:9: 2:10 (#0), span: oneplustwo.rs:2:9: 2:10 (#0),
@ -197,22 +237,31 @@ Thir {
mutability: Not, mutability: Not,
name: "x", name: "x",
mode: ByValue, mode: ByValue,
var: HirId { var: LocalVarId(
owner: DefId(0:3 ~ oneplustwo[6ccc]::main), HirId {
local_id: 5, owner: DefId(0:3 ~ oneplustwo[6932]::main),
local_id: 7,
}, },
),
ty: i32, ty: i32,
subpattern: None, subpattern: None,
is_primary: true, is_primary: true,
}, },
}, },
initializer: Some(e5), initializer: Some(
lint_level: Explicit(HirId { e5,
owner: DefId(0:3 ~ oneplustwo[6ccc]::main), ),
local_id: 4, else_block: None,
}), lint_level: Explicit(
HirId {
owner: DefId(0:3 ~ oneplustwo[6932]::main),
local_id: 6,
}, },
opt_destruction_scope: Some(Destruction(6)), ),
},
opt_destruction_scope: Some(
Destruction(1),
),
}, },
], ],
} }