Skip to content

Commit 36728f1

Browse files
committed
Replace mir_built query with a hook and use mir_const everywhere instead
1 parent 91b87c4 commit 36728f1

File tree

15 files changed

+56
-73
lines changed

15 files changed

+56
-73
lines changed

compiler/rustc_incremental/src/persist/dirty_clean.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const BASE_HIR: &[&str] = &[
6565
const BASE_IMPL: &[&str] =
6666
&[label_strs::associated_item_def_ids, label_strs::generics_of, label_strs::impl_trait_header];
6767

68-
/// DepNodes for mir_built/Optimized, which is relevant in "executable"
68+
/// DepNodes for exported mir bodies, which is relevant in "executable"
6969
/// code, i.e., functions+methods
7070
const BASE_MIR: &[&str] = &[label_strs::optimized_mir, label_strs::promoted_mir];
7171

compiler/rustc_middle/src/hooks/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,10 @@ declare_hooks! {
7777
///
7878
/// (Eligible functions might nevertheless be skipped for other reasons.)
7979
hook is_eligible_for_coverage(key: LocalDefId) -> bool;
80+
81+
/// Create the MIR for a given `DefId` - this includes
82+
/// unreachable code.
83+
/// You do not want to call this yourself, instead use the cached version
84+
/// via `mir_const`
85+
hook build_mir(key: LocalDefId) -> mir::Body<'tcx>;
8086
}

compiler/rustc_middle/src/query/mod.rs

-7
Original file line numberDiff line numberDiff line change
@@ -485,19 +485,12 @@ rustc_queries! {
485485
separate_provide_extern
486486
}
487487

488-
/// Fetch the MIR for a given `DefId` right after it's built - this includes
489-
/// unreachable code.
490-
query mir_built(key: LocalDefId) -> &'tcx Steal<mir::Body<'tcx>> {
491-
desc { |tcx| "building MIR for `{}`", tcx.def_path_str(key) }
492-
}
493-
494488
/// Fetch the MIR for a given `DefId` up till the point where it is
495489
/// ready for const qualification.
496490
///
497491
/// See the README for the `mir` module for details.
498492
query mir_const(key: LocalDefId) -> &'tcx Steal<mir::Body<'tcx>> {
499493
desc { |tcx| "preparing `{}` for borrow checking", tcx.def_path_str(key) }
500-
no_hash
501494
}
502495

503496
/// Try to build an abstract representation of the given constant.

compiler/rustc_mir_build/src/build/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 `mir_built` query, with the only
9+
//! The result of this lowering is returned "normally" from the `build_mir` hook, 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/build/mod.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use rustc_middle::hir::place::PlaceBase as HirPlaceBase;
1818
use rustc_middle::middle::region;
1919
use rustc_middle::mir::interpret::Scalar;
2020
use rustc_middle::mir::*;
21+
use rustc_middle::query::TyCtxtAt;
2122
use rustc_middle::thir::{
2223
self, BindingMode, ExprId, LintLevel, LocalVarId, Param, ParamId, PatKind, Thir,
2324
};
@@ -30,13 +31,6 @@ use rustc_target::spec::abi::Abi;
3031

3132
use super::lints;
3233

33-
pub(crate) fn mir_built(
34-
tcx: TyCtxt<'_>,
35-
def: LocalDefId,
36-
) -> &rustc_data_structures::steal::Steal<Body<'_>> {
37-
tcx.alloc_steal_mir(mir_build(tcx, def))
38-
}
39-
4034
pub(crate) fn closure_saved_names_of_captured_variables<'tcx>(
4135
tcx: TyCtxt<'tcx>,
4236
def_id: LocalDefId,
@@ -54,7 +48,8 @@ pub(crate) fn closure_saved_names_of_captured_variables<'tcx>(
5448
}
5549

5650
/// Construct the MIR for a given `DefId`.
57-
fn mir_build<'tcx>(tcx: TyCtxt<'tcx>, def: LocalDefId) -> Body<'tcx> {
51+
pub(crate) fn mir_build<'tcx>(tcx: TyCtxtAt<'tcx>, def: LocalDefId) -> Body<'tcx> {
52+
let tcx = tcx.tcx;
5853
tcx.ensure_with_value().thir_abstract_const(def);
5954
if let Err(e) = tcx.check_match(def) {
6055
return construct_error(tcx, def, e);

compiler/rustc_mir_build/src/check_unsafety.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
139139
fn visit_inner_body(&mut self, def: LocalDefId) {
140140
if let Ok((inner_thir, expr)) = self.tcx.thir_body(def) {
141141
// Runs all other queries that depend on THIR.
142-
self.tcx.ensure_with_value().mir_built(def);
142+
self.tcx.ensure_with_value().mir_const(def);
143143
let inner_thir = &inner_thir.steal();
144144
let hir_context = self.tcx.local_def_id_to_hir_id(def);
145145
let safety_context = mem::replace(&mut self.safety_context, SafetyContext::Safe);
@@ -921,7 +921,7 @@ pub fn check_unsafety(tcx: TyCtxt<'_>, def: LocalDefId) {
921921

922922
let Ok((thir, expr)) = tcx.thir_body(def) else { return };
923923
// Runs all other queries that depend on THIR.
924-
tcx.ensure_with_value().mir_built(def);
924+
tcx.ensure_with_value().mir_const(def);
925925
let thir = &thir.steal();
926926
// If `thir` is empty, a type error occurred, skip this body.
927927
if thir.exprs.is_empty() {

compiler/rustc_mir_build/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ mod errors;
2323
pub mod lints;
2424
mod thir;
2525

26-
use rustc_middle::query::Providers;
26+
use rustc_middle::util::Providers;
2727

2828
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
2929

3030
pub fn provide(providers: &mut Providers) {
3131
providers.check_match = thir::pattern::check_match;
3232
providers.lit_to_const = thir::constant::lit_to_const;
33-
providers.mir_built = build::mir_built;
33+
providers.hooks.build_mir = build::mir_build;
3434
providers.closure_saved_names_of_captured_variables =
3535
build::closure_saved_names_of_captured_variables;
3636
providers.check_unsafety = check_unsafety::check_unsafety;

compiler/rustc_mir_transform/src/check_unsafety.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,8 @@ fn mir_unsafety_check_result(tcx: TyCtxt<'_>, def: LocalDefId) -> &UnsafetyCheck
497497
debug!("unsafety_violations({:?})", def);
498498

499499
// N.B., this borrow is valid because all the consumers of
500-
// `mir_built` force this.
501-
let body = &tcx.mir_built(def).borrow();
500+
// `mir_const` force this.
501+
let body = &tcx.mir_const(def).borrow();
502502

503503
if body.is_custom_mir() || body.tainted_by_errors.is_some() {
504504
return tcx.arena.alloc(UnsafetyCheckResult {

compiler/rustc_mir_transform/src/ffi_unwind_calls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn has_ffi_unwind_calls(tcx: TyCtxt<'_>, local_def_id: LocalDefId) -> bool {
5353
return false;
5454
}
5555

56-
let body = &*tcx.mir_built(local_def_id).borrow();
56+
let body = &*tcx.mir_const(local_def_id).borrow();
5757

5858
let body_ty = tcx.type_of(def_id).skip_binder();
5959
let body_abi = match body_ty.kind() {

compiler/rustc_mir_transform/src/lib.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,7 @@ fn mir_const(tcx: TyCtxt<'_>, def: LocalDefId) -> &Steal<Body<'_>> {
288288
tcx.ensure_with_value().mir_unsafety_check_result(def);
289289
}
290290

291-
// has_ffi_unwind_calls query uses the raw mir, so make sure it is run.
292-
tcx.ensure_with_value().has_ffi_unwind_calls(def);
293-
294-
let mut body = tcx.mir_built(def).steal();
291+
let mut body = tcx.build_mir(def);
295292

296293
pass_manager::dump_mir_for_phase_change(tcx, &body);
297294

@@ -339,6 +336,8 @@ fn mir_promoted(
339336
| DefKind::AnonConst => tcx.mir_const_qualif(def),
340337
_ => ConstQualifs::default(),
341338
};
339+
// has_ffi_unwind_calls query uses the raw mir, so make sure it is run.
340+
tcx.ensure_with_value().has_ffi_unwind_calls(def);
342341
let mut body = tcx.mir_const(def).steal();
343342
if let Some(error_reported) = const_qualifs.tainted_by_errors {
344343
body.tainted_by_errors = Some(error_reported);

tests/ui/binding/issue-53114-safety-checks.stderr

+32-32
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
error[E0793]: reference to packed field is unaligned
2+
--> $DIR/issue-53114-safety-checks.rs:23:13
3+
|
4+
LL | let _ = &p.b;
5+
| ^^^^
6+
|
7+
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
8+
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
9+
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
10+
11+
error[E0793]: reference to packed field is unaligned
12+
--> $DIR/issue-53114-safety-checks.rs:28:17
13+
|
14+
LL | let (_,) = (&p.b,);
15+
| ^^^^
16+
|
17+
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
18+
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
19+
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
20+
121
error[E0133]: access to union field is unsafe and requires unsafe function or block
222
--> $DIR/issue-53114-safety-checks.rs:24:13
323
|
@@ -31,20 +51,20 @@ LL | let (_,) = (&u2.a,);
3151
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
3252

3353
error[E0793]: reference to packed field is unaligned
34-
--> $DIR/issue-53114-safety-checks.rs:23:13
54+
--> $DIR/issue-53114-safety-checks.rs:37:16
3555
|
36-
LL | let _ = &p.b;
37-
| ^^^^
56+
LL | let _: _ = &p.b;
57+
| ^^^^
3858
|
3959
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
4060
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
4161
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
4262

4363
error[E0793]: reference to packed field is unaligned
44-
--> $DIR/issue-53114-safety-checks.rs:28:17
64+
--> $DIR/issue-53114-safety-checks.rs:42:20
4565
|
46-
LL | let (_,) = (&p.b,);
47-
| ^^^^
66+
LL | let (_,): _ = (&p.b,);
67+
| ^^^^
4868
|
4969
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
5070
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
@@ -83,20 +103,20 @@ LL | let (_,): _ = (&u2.a,);
83103
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
84104

85105
error[E0793]: reference to packed field is unaligned
86-
--> $DIR/issue-53114-safety-checks.rs:37:16
106+
--> $DIR/issue-53114-safety-checks.rs:51:11
87107
|
88-
LL | let _: _ = &p.b;
89-
| ^^^^
108+
LL | match &p.b { _ => { } }
109+
| ^^^^
90110
|
91111
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
92112
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
93113
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
94114

95115
error[E0793]: reference to packed field is unaligned
96-
--> $DIR/issue-53114-safety-checks.rs:42:20
116+
--> $DIR/issue-53114-safety-checks.rs:56:12
97117
|
98-
LL | let (_,): _ = (&p.b,);
99-
| ^^^^
118+
LL | match (&p.b,) { (_,) => { } }
119+
| ^^^^
100120
|
101121
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
102122
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
@@ -134,26 +154,6 @@ LL | match (&u2.a,) { (_,) => { } }
134154
|
135155
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
136156

137-
error[E0793]: reference to packed field is unaligned
138-
--> $DIR/issue-53114-safety-checks.rs:51:11
139-
|
140-
LL | match &p.b { _ => { } }
141-
| ^^^^
142-
|
143-
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
144-
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
145-
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
146-
147-
error[E0793]: reference to packed field is unaligned
148-
--> $DIR/issue-53114-safety-checks.rs:56:12
149-
|
150-
LL | match (&p.b,) { (_,) => { } }
151-
| ^^^^
152-
|
153-
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
154-
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
155-
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
156-
157157
error: aborting due to 18 previous errors
158158

159159
Some errors have detailed explanations: E0133, E0793.

tests/ui/coroutine/clone-rpit.next.stderr

+1-6
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,12 @@ note: ...which requires promoting constants in MIR for `foo::{closure#0}`...
1414
|
1515
LL | move |_: ()| {
1616
| ^^^^^^^^^^^^
17-
note: ...which requires preparing `foo::{closure#0}` for borrow checking...
18-
--> $DIR/clone-rpit.rs:14:5
19-
|
20-
LL | move |_: ()| {
21-
| ^^^^^^^^^^^^
2217
note: ...which requires checking if `foo::{closure#0}` contains FFI-unwind calls...
2318
--> $DIR/clone-rpit.rs:14:5
2419
|
2520
LL | move |_: ()| {
2621
| ^^^^^^^^^^^^
27-
note: ...which requires building MIR for `foo::{closure#0}`...
22+
note: ...which requires preparing `foo::{closure#0}` for borrow checking...
2823
--> $DIR/clone-rpit.rs:14:5
2924
|
3025
LL | move |_: ()| {

tests/ui/resolve/multiple_definitions_attribute_merging.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ LL | struct Dealigned<T>(u8, T);
1919
= Box<dyn Any>
2020
query stack during panic:
2121
#0 [mir_const] preparing `<impl at $DIR/multiple_definitions_attribute_merging.rs:15:10: 15:19>::eq` for borrow checking
22-
#1 [mir_promoted] promoting constants in MIR for `<impl at $DIR/multiple_definitions_attribute_merging.rs:15:10: 15:19>::eq`
22+
#1 [check_unsafety] unsafety-checking `<impl at $DIR/multiple_definitions_attribute_merging.rs:15:10: 15:19>::eq`
2323
end of query stack
2424
error: aborting due to 2 previous errors
2525

tests/ui/resolve/proc_macro_generated_packed.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | struct Dealigned<T>(u8, T);
1010
= Box<dyn Any>
1111
query stack during panic:
1212
#0 [mir_const] preparing `<impl at $DIR/proc_macro_generated_packed.rs:15:10: 15:19>::eq` for borrow checking
13-
#1 [mir_promoted] promoting constants in MIR for `<impl at $DIR/proc_macro_generated_packed.rs:15:10: 15:19>::eq`
13+
#1 [check_unsafety] unsafety-checking `<impl at $DIR/proc_macro_generated_packed.rs:15:10: 15:19>::eq`
1414
end of query stack
1515
error: aborting due to 1 previous error
1616

tests/ui/type-alias-enum-variants/self-in-enum-definition.stderr

-5
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ LL | V3 = Self::V1 {} as u8 + 2,
3737
note: ...which requires preparing `Alpha::V3::{constant#0}` for borrow checking...
3838
--> $DIR/self-in-enum-definition.rs:5:10
3939
|
40-
LL | V3 = Self::V1 {} as u8 + 2,
41-
| ^^^^^^^^^^^^^^^^^^^^^
42-
note: ...which requires building MIR for `Alpha::V3::{constant#0}`...
43-
--> $DIR/self-in-enum-definition.rs:5:10
44-
|
4540
LL | V3 = Self::V1 {} as u8 + 2,
4641
| ^^^^^^^^^^^^^^^^^^^^^
4742
= note: ...which requires computing layout of `Alpha`...

0 commit comments

Comments
 (0)