Skip to content

Commit d48dbdc

Browse files
committed
Move generator check earlier in inlining.
1 parent 4a66a70 commit d48dbdc

File tree

3 files changed

+12
-19
lines changed

3 files changed

+12
-19
lines changed

compiler/rustc_mir_transform/src/inline.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ fn inline<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) -> bool {
6868
if body.source.promoted.is_some() {
6969
return false;
7070
}
71+
// Avoid inlining into generators, since their `optimized_mir` is used for layout computation,
72+
// which can create a cycle, even when no attempt is made to inline the function in the other
73+
// direction.
74+
if body.generator.is_some() {
75+
return false;
76+
}
7177

7278
let mut this = Inliner {
7379
tcx,
@@ -202,14 +208,6 @@ impl<'tcx> Inliner<'tcx> {
202208

203209
if let Some(callee_def_id) = callee.def_id().as_local() {
204210
let callee_hir_id = self.tcx.hir().local_def_id_to_hir_id(callee_def_id);
205-
// Avoid inlining into generators,
206-
// since their `optimized_mir` is used for layout computation, which can
207-
// create a cycle, even when no attempt is made to inline the function
208-
// in the other direction.
209-
if caller_body.generator.is_some() {
210-
return Err("local generator (query cycle avoidance)");
211-
}
212-
213211
// Avoid a cycle here by only using `instance_mir` only if we have
214212
// a lower `HirId` than the callee. This ensures that the callee will
215213
// not inline us. This trick only works without incremental compilation.

src/test/mir-opt/generator_drop_cleanup.main-{closure#0}.generator_drop.0.mir

+5-10
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,16 @@ fn main::{closure#0}(_1: *mut [generator@$DIR/generator-drop-cleanup.rs:10:15: 1
2020
let _3: std::string::String; // in scope 0 at $DIR/generator-drop-cleanup.rs:11:13: 11:15
2121
let _4: (); // in scope 0 at $DIR/generator-drop-cleanup.rs:12:9: 12:14
2222
let mut _5: (); // in scope 0 at $DIR/generator-drop-cleanup.rs:12:9: 12:14
23-
let mut _7: (); // in scope 0 at $DIR/generator-drop-cleanup.rs:10:18: 10:18
24-
let mut _8: (); // in scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
25-
let mut _9: u32; // in scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
23+
let mut _6: (); // in scope 0 at $DIR/generator-drop-cleanup.rs:10:18: 10:18
24+
let mut _7: (); // in scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
25+
let mut _8: u32; // in scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
2626
scope 1 {
2727
debug _s => (((*_1) as variant#3).0: std::string::String); // in scope 1 at $DIR/generator-drop-cleanup.rs:11:13: 11:15
2828
}
29-
scope 2 (inlined String::new) { // at $DIR/generator-drop-cleanup.rs:11:18: 11:31
30-
let mut _6: std::vec::Vec<u8>; // in scope 2 at $DIR/generator-drop-cleanup.rs:11:18: 11:31
31-
scope 3 (inlined Vec::<u8>::new) { // at $DIR/generator-drop-cleanup.rs:11:18: 11:31
32-
}
33-
}
3429

3530
bb0: {
36-
_9 = discriminant((*_1)); // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
37-
switchInt(move _9) -> [0_u32: bb7, 3_u32: bb10, otherwise: bb11]; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
31+
_8 = discriminant((*_1)); // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
32+
switchInt(move _8) -> [0_u32: bb7, 3_u32: bb10, otherwise: bb11]; // scope 0 at $DIR/generator-drop-cleanup.rs:10:15: 13:6
3833
}
3934

4035
bb1: {

src/test/ui/mir/remove-zsts-query-cycle.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// optimized mir -> remove zsts -> layout of a generator -> optimized mir.
33
//
44
// edition:2018
5-
// compile-flags: --crate-type=lib
5+
// compile-flags: --crate-type=lib -Zinline-mir=yes
66
// build-pass
77

88
pub async fn listen() -> Result<(), std::io::Error> {

0 commit comments

Comments
 (0)