Skip to content

Commit b1b244d

Browse files
committed
Auto merge of #115194 - tmiasko:inline-always-encode-mir, r=compiler-errors
Fix inlining with -Zalways-encode-mir Only inline functions that are considered eligible for inlining by the reachability pass. This constraint was previously indirectly enforced by only exporting MIR of eligible functions, but that approach doesn't work with -Zalways-encode-mir enabled.
2 parents 59a8294 + fe3cd2d commit b1b244d

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

compiler/rustc_mir_transform/src/inline.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -388,14 +388,11 @@ impl<'tcx> Inliner<'tcx> {
388388
return Err("never inline hint");
389389
}
390390

391-
// Only inline local functions if they would be eligible for cross-crate
392-
// inlining. This is to ensure that the final crate doesn't have MIR that
393-
// reference unexported symbols
394-
if callsite.callee.def_id().is_local() {
395-
let is_generic = callsite.callee.args.non_erasable_generics().next().is_some();
396-
if !is_generic && !callee_attrs.requests_inline() {
397-
return Err("not exported");
398-
}
391+
// Reachability pass defines which functions are eligible for inlining. Generally inlining
392+
// other functions is incorrect because they could reference symbols that aren't exported.
393+
let is_generic = callsite.callee.args.non_erasable_generics().next().is_some();
394+
if !is_generic && !callee_attrs.requests_inline() {
395+
return Err("not exported");
399396
}
400397

401398
if callsite.fn_sig.c_variadic() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Regression test for MIR inlining with -Zalways-encode-mir enabled in the auxiliary crate.
2+
// Previously we inlined function not eligible for inlining which lead to linking error:
3+
// undefined reference to `internal::S'
4+
//
5+
// aux-build:internal.rs
6+
// build-pass
7+
// compile-flags: -O
8+
extern crate internal;
9+
10+
fn main() {
11+
println!("{}", internal::f());
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// compile-flags: -Zalways-encode-mir
2+
3+
static S: usize = 42;
4+
5+
pub fn f() -> &'static usize {
6+
&S
7+
}

0 commit comments

Comments
 (0)