Skip to content

Commit eba3228

Browse files
committed
Auto merge of rust-lang#86251 - Smittyvb:thir-tree-again, r=oli-obk
Support -Z unpretty=thir-tree again Currently `-Z unpretty=thir-tree` is broken after some THIR refactorings. This re-implements it, making it easier to debug THIR-related issues. We have to do analyzes before getting the THIR, since trying to create THIR from invalid HIR can ICE. But doing those analyzes requires the THIR to be built and stolen. We work around this by creating a separate query to construct the THIR tree string representation. Closes rust-lang/project-thir-unsafeck#8, fixes rust-lang#85552.
2 parents aea2e44 + 51df26e commit eba3228

File tree

8 files changed

+88
-3
lines changed

8 files changed

+88
-3
lines changed

Diff for: Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3726,6 +3726,7 @@ dependencies = [
37263726
"rustc_session",
37273727
"rustc_span",
37283728
"rustc_target",
3729+
"rustc_typeck",
37293730
"tracing",
37303731
"tracing-subscriber",
37313732
"tracing-tree",

Diff for: compiler/rustc_driver/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ rustc_interface = { path = "../rustc_interface" }
3434
rustc_serialize = { path = "../rustc_serialize" }
3535
rustc_ast = { path = "../rustc_ast" }
3636
rustc_span = { path = "../rustc_span" }
37+
rustc_typeck = { path = "../rustc_typeck" }
3738

3839
[target.'cfg(windows)'.dependencies]
3940
winapi = { version = "0.3", features = ["consoleapi", "debugapi", "processenv"] }

Diff for: compiler/rustc_driver/src/pretty.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_span::symbol::Ident;
1414
use rustc_span::FileName;
1515

1616
use std::cell::Cell;
17+
use std::fmt::Write;
1718
use std::path::Path;
1819

1920
pub use self::PpMode::*;
@@ -471,7 +472,6 @@ fn print_with_analysis(
471472
ofile: Option<&Path>,
472473
) -> Result<(), ErrorReported> {
473474
tcx.analysis(())?;
474-
475475
let out = match ppm {
476476
Mir => {
477477
let mut out = Vec::new();
@@ -486,8 +486,18 @@ fn print_with_analysis(
486486
}
487487

488488
ThirTree => {
489-
// FIXME(rust-lang/project-thir-unsafeck#8)
490-
todo!()
489+
let mut out = String::new();
490+
abort_on_err(rustc_typeck::check_crate(tcx), tcx.sess);
491+
debug!("pretty printing THIR tree");
492+
for did in tcx.body_owners() {
493+
let _ = writeln!(
494+
out,
495+
"{:?}:\n{}\n",
496+
did,
497+
tcx.thir_tree(ty::WithOptConstParam::unknown(did))
498+
);
499+
}
500+
out
491501
}
492502

493503
_ => unreachable!(),

Diff for: compiler/rustc_middle/src/query/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,12 @@ rustc_queries! {
230230
desc { |tcx| "building THIR for `{}`", tcx.def_path_str(key.did.to_def_id()) }
231231
}
232232

233+
/// Create a THIR tree for debugging.
234+
query thir_tree(key: ty::WithOptConstParam<LocalDefId>) -> String {
235+
no_hash
236+
desc { |tcx| "constructing THIR tree for `{}`", tcx.def_path_str(key.did.to_def_id()) }
237+
}
238+
233239
/// Set of all the `DefId`s in this crate that have MIR associated with
234240
/// them. This includes all the body owners, but also things like struct
235241
/// constructors.

Diff for: compiler/rustc_mir_build/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ pub fn provide(providers: &mut Providers) {
3030
providers.thir_check_unsafety = check_unsafety::thir_check_unsafety;
3131
providers.thir_check_unsafety_for_const_arg = check_unsafety::thir_check_unsafety_for_const_arg;
3232
providers.thir_body = thir::cx::thir_body;
33+
providers.thir_tree = thir::cx::thir_tree;
3334
}

Diff for: compiler/rustc_mir_build/src/thir/cx/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ crate fn thir_body<'tcx>(
3030
(tcx.alloc_steal_thir(cx.thir), expr)
3131
}
3232

33+
crate fn thir_tree<'tcx>(
34+
tcx: TyCtxt<'tcx>,
35+
owner_def: ty::WithOptConstParam<LocalDefId>,
36+
) -> String {
37+
format!("{:#?}", thir_body(tcx, owner_def).0.steal())
38+
}
39+
3340
struct Cx<'tcx> {
3441
tcx: TyCtxt<'tcx>,
3542
thir: Thir<'tcx>,

Diff for: src/test/ui/thir-tree.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// compile-flags: -Z unpretty=thir-tree
2+
// check-pass
3+
4+
pub fn main() {}

Diff for: src/test/ui/thir-tree.stdout

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
DefId(0:3 ~ thir_tree[348d]::main):
2+
Thir {
3+
arms: [],
4+
exprs: [
5+
Expr {
6+
ty: (),
7+
temp_lifetime: Some(
8+
Node(2),
9+
),
10+
span: $DIR/thir-tree.rs:4:15: 4:17 (#0),
11+
kind: Block {
12+
body: Block {
13+
targeted_by_break: false,
14+
region_scope: Node(1),
15+
opt_destruction_scope: None,
16+
span: $DIR/thir-tree.rs:4:15: 4:17 (#0),
17+
stmts: [],
18+
expr: None,
19+
safety_mode: Safe,
20+
},
21+
},
22+
},
23+
Expr {
24+
ty: (),
25+
temp_lifetime: Some(
26+
Node(2),
27+
),
28+
span: $DIR/thir-tree.rs:4:15: 4:17 (#0),
29+
kind: Scope {
30+
region_scope: Node(2),
31+
lint_level: Explicit(
32+
HirId {
33+
owner: DefId(0:3 ~ thir_tree[348d]::main),
34+
local_id: 2,
35+
},
36+
),
37+
value: e0,
38+
},
39+
},
40+
Expr {
41+
ty: (),
42+
temp_lifetime: Some(
43+
Node(2),
44+
),
45+
span: $DIR/thir-tree.rs:4:15: 4:17 (#0),
46+
kind: Scope {
47+
region_scope: Destruction(2),
48+
lint_level: Inherited,
49+
value: e1,
50+
},
51+
},
52+
],
53+
stmts: [],
54+
}
55+

0 commit comments

Comments
 (0)