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.
This commit is contained in:
bors 2023-08-30 22:51:12 +00:00
commit b1b244da65
3 changed files with 24 additions and 8 deletions

View file

@ -388,14 +388,11 @@ impl<'tcx> Inliner<'tcx> {
return Err("never inline hint"); return Err("never inline hint");
} }
// Only inline local functions if they would be eligible for cross-crate // Reachability pass defines which functions are eligible for inlining. Generally inlining
// inlining. This is to ensure that the final crate doesn't have MIR that // other functions is incorrect because they could reference symbols that aren't exported.
// reference unexported symbols let is_generic = callsite.callee.args.non_erasable_generics().next().is_some();
if callsite.callee.def_id().is_local() { if !is_generic && !callee_attrs.requests_inline() {
let is_generic = callsite.callee.args.non_erasable_generics().next().is_some(); return Err("not exported");
if !is_generic && !callee_attrs.requests_inline() {
return Err("not exported");
}
} }
if callsite.fn_sig.c_variadic() { if callsite.fn_sig.c_variadic() {

View file

@ -0,0 +1,12 @@
// Regression test for MIR inlining with -Zalways-encode-mir enabled in the auxiliary crate.
// Previously we inlined function not eligible for inlining which lead to linking error:
// undefined reference to `internal::S'
//
// aux-build:internal.rs
// build-pass
// compile-flags: -O
extern crate internal;
fn main() {
println!("{}", internal::f());
}

View file

@ -0,0 +1,7 @@
// compile-flags: -Zalways-encode-mir
static S: usize = 42;
pub fn f() -> &'static usize {
&S
}