Skip to content

Commit 2515638

Browse files
authored
Rollup merge of rust-lang#82860 - LeSeulArtichaut:unpretty-thir, r=spastorino
Add `-Z unpretty` flag for the THIR This adds a new perma-unstable flag, `-Zunpretty=thir-tree`, that dumps the raw THIR tree for each body in the crate. Implements the THIR part of MCP rust-lang/compiler-team#408, helps with rust-lang/rustc-dev-guide#1062. Depends on rust-lang#82495, blocked on that. Only the two last commits are added by this PR. r? `@spastorino` cc `@estebank`
2 parents 56fee40 + 6bf4147 commit 2515638

File tree

9 files changed

+87
-62
lines changed

9 files changed

+87
-62
lines changed

Cargo.lock

+2
Original file line numberDiff line numberDiff line change
@@ -3870,13 +3870,15 @@ dependencies = [
38703870
"rustc_metadata",
38713871
"rustc_middle",
38723872
"rustc_mir",
3873+
"rustc_mir_build",
38733874
"rustc_parse",
38743875
"rustc_plugin_impl",
38753876
"rustc_save_analysis",
38763877
"rustc_serialize",
38773878
"rustc_session",
38783879
"rustc_span",
38793880
"rustc_target",
3881+
"rustc_typeck",
38803882
"tracing",
38813883
"tracing-subscriber",
38823884
"tracing-tree",

compiler/rustc_driver/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ 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_mir_build = { path = "../rustc_mir_build" }
38+
rustc_typeck = { path = "../rustc_typeck" }
3739

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

compiler/rustc_driver/src/pretty.rs

+17
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ use rustc_hir_pretty as pprust_hir;
99
use rustc_middle::hir::map as hir_map;
1010
use rustc_middle::ty::{self, TyCtxt};
1111
use rustc_mir::util::{write_mir_graphviz, write_mir_pretty};
12+
use rustc_mir_build::thir;
1213
use rustc_session::config::{Input, PpAstTreeMode, PpHirMode, PpMode, PpSourceMode};
1314
use rustc_session::Session;
1415
use rustc_span::symbol::Ident;
1516
use rustc_span::FileName;
1617

1718
use std::cell::Cell;
19+
use std::fmt::Write;
1820
use std::path::Path;
1921

2022
pub use self::PpMode::*;
@@ -469,6 +471,21 @@ pub fn print_after_hir_lowering<'tcx>(
469471
format!("{:#?}", krate)
470472
}),
471473

474+
ThirTree => {
475+
let mut out = String::new();
476+
abort_on_err(rustc_typeck::check_crate(tcx), tcx.sess);
477+
debug!("pretty printing THIR tree");
478+
for did in tcx.body_owners() {
479+
let hir = tcx.hir();
480+
let body = hir.body(hir.body_owned_by(hir.local_def_id_to_hir_id(did)));
481+
let arena = thir::Arena::default();
482+
let thir =
483+
thir::build_thir(tcx, ty::WithOptConstParam::unknown(did), &arena, &body.value);
484+
let _ = writeln!(out, "{:?}:\n{:#?}\n", did, thir);
485+
}
486+
out
487+
}
488+
472489
_ => unreachable!(),
473490
};
474491

compiler/rustc_mir_build/src/build/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::build;
22
use crate::build::scope::DropKind;
3-
use crate::thir::cx::build_thir;
4-
use crate::thir::{Arena, BindingMode, Expr, LintLevel, Pat, PatKind};
3+
use crate::thir::{build_thir, Arena, BindingMode, Expr, LintLevel, Pat, PatKind};
54
use rustc_attr::{self as attr, UnwindAttr};
65
use rustc_errors::ErrorReported;
76
use rustc_hir as hir;

compiler/rustc_mir_build/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ extern crate rustc_middle;
2020

2121
mod build;
2222
mod lints;
23-
mod thir;
23+
pub mod thir;
2424

2525
use rustc_middle::ty::query::Providers;
2626

compiler/rustc_mir_build/src/thir/cx/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_middle::middle::region;
1414
use rustc_middle::mir::interpret::{LitToConstError, LitToConstInput};
1515
use rustc_middle::ty::{self, Ty, TyCtxt};
1616

17-
crate fn build_thir<'thir, 'tcx>(
17+
pub fn build_thir<'thir, 'tcx>(
1818
tcx: TyCtxt<'tcx>,
1919
owner_def: ty::WithOptConstParam<LocalDefId>,
2020
arena: &'thir Arena<'thir, 'tcx>,

compiler/rustc_mir_build/src/thir/mod.rs

+40-39
Original file line numberDiff line numberDiff line change
@@ -18,50 +18,51 @@ use rustc_target::abi::VariantIdx;
1818
use rustc_target::asm::InlineAsmRegOrRegClass;
1919

2020
crate mod constant;
21+
2122
crate mod cx;
23+
pub use cx::build_thir;
2224

2325
crate mod pattern;
24-
crate use self::pattern::PatTyProj;
25-
crate use self::pattern::{BindingMode, FieldPat, Pat, PatKind, PatRange};
26+
pub use self::pattern::{Ascription, BindingMode, FieldPat, Pat, PatKind, PatRange, PatTyProj};
2627

2728
mod arena;
28-
crate use arena::Arena;
29+
pub use arena::Arena;
2930

3031
mod util;
3132

3233
#[derive(Copy, Clone, Debug)]
33-
crate enum LintLevel {
34+
pub enum LintLevel {
3435
Inherited,
3536
Explicit(hir::HirId),
3637
}
3738

3839
#[derive(Debug)]
39-
crate struct Block<'thir, 'tcx> {
40-
crate targeted_by_break: bool,
41-
crate region_scope: region::Scope,
42-
crate opt_destruction_scope: Option<region::Scope>,
43-
crate span: Span,
44-
crate stmts: &'thir [Stmt<'thir, 'tcx>],
45-
crate expr: Option<&'thir Expr<'thir, 'tcx>>,
46-
crate safety_mode: BlockSafety,
40+
pub struct Block<'thir, 'tcx> {
41+
pub targeted_by_break: bool,
42+
pub region_scope: region::Scope,
43+
pub opt_destruction_scope: Option<region::Scope>,
44+
pub span: Span,
45+
pub stmts: &'thir [Stmt<'thir, 'tcx>],
46+
pub expr: Option<&'thir Expr<'thir, 'tcx>>,
47+
pub safety_mode: BlockSafety,
4748
}
4849

4950
#[derive(Copy, Clone, Debug)]
50-
crate enum BlockSafety {
51+
pub enum BlockSafety {
5152
Safe,
5253
ExplicitUnsafe(hir::HirId),
5354
PushUnsafe,
5455
PopUnsafe,
5556
}
5657

5758
#[derive(Debug)]
58-
crate struct Stmt<'thir, 'tcx> {
59-
crate kind: StmtKind<'thir, 'tcx>,
60-
crate opt_destruction_scope: Option<region::Scope>,
59+
pub struct Stmt<'thir, 'tcx> {
60+
pub kind: StmtKind<'thir, 'tcx>,
61+
pub opt_destruction_scope: Option<region::Scope>,
6162
}
6263

6364
#[derive(Debug)]
64-
crate enum StmtKind<'thir, 'tcx> {
65+
pub enum StmtKind<'thir, 'tcx> {
6566
Expr {
6667
/// scope for this statement; may be used as lifetime of temporaries
6768
scope: region::Scope,
@@ -111,23 +112,23 @@ rustc_data_structures::static_assert_size!(Expr<'_, '_>, 144);
111112
/// example, method calls and overloaded operators are absent: they are
112113
/// expected to be converted into `Expr::Call` instances.
113114
#[derive(Debug)]
114-
crate struct Expr<'thir, 'tcx> {
115+
pub struct Expr<'thir, 'tcx> {
115116
/// type of this expression
116-
crate ty: Ty<'tcx>,
117+
pub ty: Ty<'tcx>,
117118

118119
/// lifetime of this expression if it should be spilled into a
119120
/// temporary; should be None only if in a constant context
120-
crate temp_lifetime: Option<region::Scope>,
121+
pub temp_lifetime: Option<region::Scope>,
121122

122123
/// span of the expression in the source
123-
crate span: Span,
124+
pub span: Span,
124125

125126
/// kind of expression
126-
crate kind: ExprKind<'thir, 'tcx>,
127+
pub kind: ExprKind<'thir, 'tcx>,
127128
}
128129

129130
#[derive(Debug)]
130-
crate enum ExprKind<'thir, 'tcx> {
131+
pub enum ExprKind<'thir, 'tcx> {
131132
Scope {
132133
region_scope: region::Scope,
133134
lint_level: LintLevel,
@@ -316,41 +317,41 @@ crate enum ExprKind<'thir, 'tcx> {
316317
}
317318

318319
#[derive(Debug)]
319-
crate struct FieldExpr<'thir, 'tcx> {
320-
crate name: Field,
321-
crate expr: &'thir Expr<'thir, 'tcx>,
320+
pub struct FieldExpr<'thir, 'tcx> {
321+
pub name: Field,
322+
pub expr: &'thir Expr<'thir, 'tcx>,
322323
}
323324

324325
#[derive(Debug)]
325-
crate struct FruInfo<'thir, 'tcx> {
326-
crate base: &'thir Expr<'thir, 'tcx>,
327-
crate field_types: &'thir [Ty<'tcx>],
326+
pub struct FruInfo<'thir, 'tcx> {
327+
pub base: &'thir Expr<'thir, 'tcx>,
328+
pub field_types: &'thir [Ty<'tcx>],
328329
}
329330

330331
#[derive(Debug)]
331-
crate struct Arm<'thir, 'tcx> {
332-
crate pattern: Pat<'tcx>,
333-
crate guard: Option<Guard<'thir, 'tcx>>,
334-
crate body: &'thir Expr<'thir, 'tcx>,
335-
crate lint_level: LintLevel,
336-
crate scope: region::Scope,
337-
crate span: Span,
332+
pub struct Arm<'thir, 'tcx> {
333+
pub pattern: Pat<'tcx>,
334+
pub guard: Option<Guard<'thir, 'tcx>>,
335+
pub body: &'thir Expr<'thir, 'tcx>,
336+
pub lint_level: LintLevel,
337+
pub scope: region::Scope,
338+
pub span: Span,
338339
}
339340

340341
#[derive(Debug)]
341-
crate enum Guard<'thir, 'tcx> {
342+
pub enum Guard<'thir, 'tcx> {
342343
If(&'thir Expr<'thir, 'tcx>),
343344
IfLet(Pat<'tcx>, &'thir Expr<'thir, 'tcx>),
344345
}
345346

346347
#[derive(Copy, Clone, Debug)]
347-
crate enum LogicalOp {
348+
pub enum LogicalOp {
348349
And,
349350
Or,
350351
}
351352

352353
#[derive(Debug)]
353-
crate enum InlineAsmOperand<'thir, 'tcx> {
354+
pub enum InlineAsmOperand<'thir, 'tcx> {
354355
In {
355356
reg: InlineAsmRegOrRegClass,
356357
expr: &'thir Expr<'thir, 'tcx>,

compiler/rustc_mir_build/src/thir/pattern/mod.rs

+19-19
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,22 @@ crate enum PatternError {
4040
}
4141

4242
#[derive(Copy, Clone, Debug, PartialEq)]
43-
crate enum BindingMode {
43+
pub enum BindingMode {
4444
ByValue,
4545
ByRef(BorrowKind),
4646
}
4747

4848
#[derive(Clone, Debug, PartialEq)]
49-
crate struct FieldPat<'tcx> {
50-
crate field: Field,
51-
crate pattern: Pat<'tcx>,
49+
pub struct FieldPat<'tcx> {
50+
pub field: Field,
51+
pub pattern: Pat<'tcx>,
5252
}
5353

5454
#[derive(Clone, Debug, PartialEq)]
55-
crate struct Pat<'tcx> {
56-
crate ty: Ty<'tcx>,
57-
crate span: Span,
58-
crate kind: Box<PatKind<'tcx>>,
55+
pub struct Pat<'tcx> {
56+
pub ty: Ty<'tcx>,
57+
pub span: Span,
58+
pub kind: Box<PatKind<'tcx>>,
5959
}
6060

6161
impl<'tcx> Pat<'tcx> {
@@ -65,8 +65,8 @@ impl<'tcx> Pat<'tcx> {
6565
}
6666

6767
#[derive(Copy, Clone, Debug, PartialEq)]
68-
crate struct PatTyProj<'tcx> {
69-
crate user_ty: CanonicalUserType<'tcx>,
68+
pub struct PatTyProj<'tcx> {
69+
pub user_ty: CanonicalUserType<'tcx>,
7070
}
7171

7272
impl<'tcx> PatTyProj<'tcx> {
@@ -92,8 +92,8 @@ impl<'tcx> PatTyProj<'tcx> {
9292
}
9393

9494
#[derive(Copy, Clone, Debug, PartialEq)]
95-
crate struct Ascription<'tcx> {
96-
crate user_ty: PatTyProj<'tcx>,
95+
pub struct Ascription<'tcx> {
96+
pub user_ty: PatTyProj<'tcx>,
9797
/// Variance to use when relating the type `user_ty` to the **type of the value being
9898
/// matched**. Typically, this is `Variance::Covariant`, since the value being matched must
9999
/// have a type that is some subtype of the ascribed type.
@@ -112,12 +112,12 @@ crate struct Ascription<'tcx> {
112112
/// requires that `&'static str <: T_x`, where `T_x` is the type of `x`. Really, we should
113113
/// probably be checking for a `PartialEq` impl instead, but this preserves the behavior
114114
/// of the old type-check for now. See #57280 for details.
115-
crate variance: ty::Variance,
116-
crate user_ty_span: Span,
115+
pub variance: ty::Variance,
116+
pub user_ty_span: Span,
117117
}
118118

119119
#[derive(Clone, Debug, PartialEq)]
120-
crate enum PatKind<'tcx> {
120+
pub enum PatKind<'tcx> {
121121
Wild,
122122

123123
AscribeUserType {
@@ -195,10 +195,10 @@ crate enum PatKind<'tcx> {
195195
}
196196

197197
#[derive(Copy, Clone, Debug, PartialEq)]
198-
crate struct PatRange<'tcx> {
199-
crate lo: &'tcx ty::Const<'tcx>,
200-
crate hi: &'tcx ty::Const<'tcx>,
201-
crate end: RangeEnd,
198+
pub struct PatRange<'tcx> {
199+
pub lo: &'tcx ty::Const<'tcx>,
200+
pub hi: &'tcx ty::Const<'tcx>,
201+
pub end: RangeEnd,
202202
}
203203

204204
impl<'tcx> fmt::Display for Pat<'tcx> {

compiler/rustc_session/src/config.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2074,6 +2074,7 @@ fn parse_pretty(
20742074
("hir,identified", true) => Hir(PpHirMode::Identified),
20752075
("hir,typed", true) => Hir(PpHirMode::Typed),
20762076
("hir-tree", true) => HirTree,
2077+
("thir-tree", true) => ThirTree,
20772078
("mir", true) => Mir,
20782079
("mir-cfg", true) => MirCFG,
20792080
_ => {
@@ -2265,6 +2266,8 @@ pub enum PpMode {
22652266
Hir(PpHirMode),
22662267
/// `-Zunpretty=hir-tree`
22672268
HirTree,
2269+
/// `-Zunpretty=thir-tree`
2270+
ThirTree,
22682271
/// `-Zunpretty=mir`
22692272
Mir,
22702273
/// `-Zunpretty=mir-cfg`
@@ -2282,6 +2285,7 @@ impl PpMode {
22822285
| AstTree(PpAstTreeMode::Expanded)
22832286
| Hir(_)
22842287
| HirTree
2288+
| ThirTree
22852289
| Mir
22862290
| MirCFG => true,
22872291
}

0 commit comments

Comments
 (0)