Skip to content

Commit eb05560

Browse files
authored
Unrolled build for rust-lang#136330
Rollup merge of rust-lang#136330 - nnethercote:rm-unnecessary-hooks, r=oli-obk Remove unnecessary hooks Some hooks can be downgraded to vanilla functions. r? `@oli-obk`
2 parents aa4cfd0 + 1d2cb61 commit eb05560

File tree

8 files changed

+26
-35
lines changed

8 files changed

+26
-35
lines changed

compiler/rustc_driver_impl/src/pretty.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_ast_pretty::pprust as pprust_ast;
77
use rustc_middle::bug;
88
use rustc_middle::mir::{write_mir_graphviz, write_mir_pretty};
99
use rustc_middle::ty::{self, TyCtxt};
10+
use rustc_mir_build::thir::print::{thir_flat, thir_tree};
1011
use rustc_session::Session;
1112
use rustc_session::config::{OutFileName, PpHirMode, PpMode, PpSourceMode};
1213
use rustc_smir::rustc_internal::pretty::write_smir_pretty;
@@ -313,7 +314,7 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
313314
tcx.dcx().abort_if_errors();
314315
debug!("pretty printing THIR tree");
315316
for did in tcx.hir().body_owners() {
316-
let _ = writeln!(out, "{:?}:\n{}\n", did, tcx.thir_tree(did));
317+
let _ = writeln!(out, "{:?}:\n{}\n", did, thir_tree(tcx, did));
317318
}
318319
out
319320
}
@@ -324,7 +325,7 @@ pub fn print<'tcx>(sess: &Session, ppm: PpMode, ex: PrintExtra<'tcx>) {
324325
tcx.dcx().abort_if_errors();
325326
debug!("pretty printing THIR flat");
326327
for did in tcx.hir().body_owners() {
327-
let _ = writeln!(out, "{:?}:\n{}\n", did, tcx.thir_flat(did));
328+
let _ = writeln!(out, "{:?}:\n{}\n", did, thir_flat(tcx, did));
328329
}
329330
out
330331
}

compiler/rustc_middle/src/hooks/mod.rs

+7-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
//! "Hooks" provide a way for `tcx` functionality to be provided by some downstream crate without
2-
//! everything in rustc having to depend on that crate. This is somewhat similar to queries, but
3-
//! queries come with a lot of machinery for caching and incremental compilation, whereas hooks are
4-
//! just plain function pointers without any of the query magic.
1+
//! "Hooks" let you write `tcx` methods in downstream crates and call them in this crate, reducing
2+
//! the amount of code that needs to be in this crate (which is already very big). This is somewhat
3+
//! similar to queries, but queries come with a lot of machinery for caching and incremental
4+
//! compilation, whereas hooks are just plain function pointers without any of the query magic.
55
66
use rustc_hir::def_id::{DefId, DefPathHash};
77
use rustc_session::StableCrateId;
@@ -75,12 +75,6 @@ declare_hooks! {
7575
/// (Eligible functions might nevertheless be skipped for other reasons.)
7676
hook is_eligible_for_coverage(key: LocalDefId) -> bool;
7777

78-
/// Create the MIR for a given `DefId` - this includes
79-
/// unreachable code.
80-
/// You do not want to call this yourself, instead use the cached version
81-
/// via `mir_built`
82-
hook build_mir(key: LocalDefId) -> mir::Body<'tcx>;
83-
8478
/// Imports all `SourceFile`s from the given crate into the current session.
8579
/// This normally happens automatically when we decode a `Span` from
8680
/// that crate's metadata - however, the incr comp cache needs
@@ -99,14 +93,11 @@ declare_hooks! {
9993
/// Will fetch a DefId from a DefPathHash for a foreign crate.
10094
hook def_path_hash_to_def_id_extern(hash: DefPathHash, stable_crate_id: StableCrateId) -> DefId;
10195

102-
/// Create a THIR tree for debugging.
103-
hook thir_tree(key: LocalDefId) -> String;
104-
105-
/// Create a list-like THIR representation for debugging.
106-
hook thir_flat(key: LocalDefId) -> String;
107-
10896
/// Returns `true` if we should codegen an instance in the local crate, or returns `false` if we
10997
/// can just link to the upstream crate and therefore don't need a mono item.
98+
///
99+
/// Note: this hook isn't called within `rustc_middle` but #127779 suggests it's a hook instead
100+
/// of a normal function because external tools might want to override it.
110101
hook should_codegen_locally(instance: crate::ty::Instance<'tcx>) -> bool;
111102

112103
hook alloc_self_profile_query_strings() -> ();

compiler/rustc_mir_build/src/builder/custom/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//! present, and if so we branch off into this module, which implements the attribute by
77
//! implementing a custom lowering from THIR to MIR.
88
//!
9-
//! The result of this lowering is returned "normally" from the `build_mir` hook, with the only
9+
//! The result of this lowering is returned "normally" from `build_mir`, with the only
1010
//! notable difference being that the `injected` field in the body is set. Various components of the
1111
//! MIR pipeline, like borrowck and the pass manager will then consult this field (via
1212
//! `body.should_skip()`) to skip the parts of the MIR pipeline that precede the MIR phase the user

compiler/rustc_mir_build/src/builder/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
2020
use rustc_middle::hir::place::PlaceBase as HirPlaceBase;
2121
use rustc_middle::middle::region;
2222
use rustc_middle::mir::*;
23-
use rustc_middle::query::TyCtxtAt;
2423
use rustc_middle::thir::{self, ExprId, LintLevel, LocalVarId, Param, ParamId, PatKind, Thir};
2524
use rustc_middle::ty::{self, ScalarInt, Ty, TyCtxt, TypeVisitableExt, TypingMode};
2625
use rustc_middle::{bug, span_bug};
@@ -45,9 +44,9 @@ pub(crate) fn closure_saved_names_of_captured_variables<'tcx>(
4544
.collect()
4645
}
4746

48-
/// Construct the MIR for a given `DefId`.
49-
pub(crate) fn build_mir<'tcx>(tcx: TyCtxtAt<'tcx>, def: LocalDefId) -> Body<'tcx> {
50-
let tcx = tcx.tcx;
47+
/// Create the MIR for a given `DefId`, including unreachable code. Do not call
48+
/// this directly; instead use the cached version via `mir_built`.
49+
pub fn build_mir<'tcx>(tcx: TyCtxt<'tcx>, def: LocalDefId) -> Body<'tcx> {
5150
tcx.ensure_with_value().thir_abstract_const(def);
5251
if let Err(e) = tcx.check_match(def) {
5352
return construct_error(tcx, def, e);

compiler/rustc_mir_build/src/lib.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
// The `builder` module used to be named `build`, but that was causing GitHub's
1515
// "Go to file" feature to silently ignore all files in the module, probably
1616
// because it assumes that "build" is a build-output directory. See #134365.
17-
mod builder;
17+
pub mod builder;
1818
mod check_tail_calls;
1919
mod check_unsafety;
2020
mod errors;
21-
mod thir;
21+
pub mod thir;
2222

2323
use rustc_middle::util::Providers;
2424

@@ -27,12 +27,9 @@ rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
2727
pub fn provide(providers: &mut Providers) {
2828
providers.check_match = thir::pattern::check_match;
2929
providers.lit_to_const = thir::constant::lit_to_const;
30-
providers.hooks.build_mir = builder::build_mir;
3130
providers.closure_saved_names_of_captured_variables =
3231
builder::closure_saved_names_of_captured_variables;
3332
providers.check_unsafety = check_unsafety::check_unsafety;
3433
providers.check_tail_calls = check_tail_calls::check_tail_calls;
3534
providers.thir_body = thir::cx::thir_body;
36-
providers.hooks.thir_tree = thir::print::thir_tree;
37-
providers.hooks.thir_flat = thir::print::thir_flat;
3835
}

compiler/rustc_mir_build/src/thir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
pub(crate) mod constant;
88
pub(crate) mod cx;
99
pub(crate) mod pattern;
10-
pub(crate) mod print;
10+
pub mod print;
1111
mod util;

compiler/rustc_mir_build/src/thir/print.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use std::fmt::{self, Write};
22

3-
use rustc_middle::query::TyCtxtAt;
43
use rustc_middle::thir::*;
54
use rustc_middle::ty;
5+
use rustc_middle::ty::TyCtxt;
66
use rustc_span::def_id::LocalDefId;
77

8-
pub(crate) fn thir_tree(tcx: TyCtxtAt<'_>, owner_def: LocalDefId) -> String {
9-
match super::cx::thir_body(*tcx, owner_def) {
8+
/// Create a THIR tree for debugging.
9+
pub fn thir_tree(tcx: TyCtxt<'_>, owner_def: LocalDefId) -> String {
10+
match super::cx::thir_body(tcx, owner_def) {
1011
Ok((thir, _)) => {
1112
let thir = thir.steal();
1213
let mut printer = ThirPrinter::new(&thir);
@@ -17,8 +18,9 @@ pub(crate) fn thir_tree(tcx: TyCtxtAt<'_>, owner_def: LocalDefId) -> String {
1718
}
1819
}
1920

20-
pub(crate) fn thir_flat(tcx: TyCtxtAt<'_>, owner_def: LocalDefId) -> String {
21-
match super::cx::thir_body(*tcx, owner_def) {
21+
/// Create a list-like THIR representation for debugging.
22+
pub fn thir_flat(tcx: TyCtxt<'_>, owner_def: LocalDefId) -> String {
23+
match super::cx::thir_body(tcx, owner_def) {
2224
Ok((thir, _)) => format!("{:#?}", thir.steal()),
2325
Err(_) => "error".into(),
2426
}

compiler/rustc_mir_transform/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use rustc_middle::mir::{
3333
use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt};
3434
use rustc_middle::util::Providers;
3535
use rustc_middle::{bug, query, span_bug};
36+
use rustc_mir_build::builder::build_mir;
3637
use rustc_span::source_map::Spanned;
3738
use rustc_span::{DUMMY_SP, sym};
3839
use tracing::debug;
@@ -370,7 +371,7 @@ fn mir_const_qualif(tcx: TyCtxt<'_>, def: LocalDefId) -> ConstQualifs {
370371
}
371372

372373
fn mir_built(tcx: TyCtxt<'_>, def: LocalDefId) -> &Steal<Body<'_>> {
373-
let mut body = tcx.build_mir(def);
374+
let mut body = build_mir(tcx, def);
374375

375376
pass_manager::dump_mir_for_phase_change(tcx, &body);
376377

0 commit comments

Comments
 (0)