diff --git a/compiler/rustc_mir_transform/src/inline.rs b/compiler/rustc_mir_transform/src/inline.rs index 9551c7a2a8d6b..7d4c4a823a8db 100644 --- a/compiler/rustc_mir_transform/src/inline.rs +++ b/compiler/rustc_mir_transform/src/inline.rs @@ -388,14 +388,11 @@ impl<'tcx> Inliner<'tcx> { return Err("never inline hint"); } - // Only inline local functions if they would be eligible for cross-crate - // inlining. This is to ensure that the final crate doesn't have MIR that - // reference unexported symbols - if callsite.callee.def_id().is_local() { - let is_generic = callsite.callee.args.non_erasable_generics().next().is_some(); - if !is_generic && !callee_attrs.requests_inline() { - return Err("not exported"); - } + // Reachability pass defines which functions are eligible for inlining. Generally inlining + // other functions is incorrect because they could reference symbols that aren't exported. + let is_generic = callsite.callee.args.non_erasable_generics().next().is_some(); + if !is_generic && !callee_attrs.requests_inline() { + return Err("not exported"); } if callsite.fn_sig.c_variadic() { diff --git a/tests/ui/mir/mir-inlining/always-encode-mirs.rs b/tests/ui/mir/mir-inlining/always-encode-mirs.rs new file mode 100644 index 0000000000000..f3731996cf29b --- /dev/null +++ b/tests/ui/mir/mir-inlining/always-encode-mirs.rs @@ -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()); +} diff --git a/tests/ui/mir/mir-inlining/auxiliary/internal.rs b/tests/ui/mir/mir-inlining/auxiliary/internal.rs new file mode 100644 index 0000000000000..fa0f9c2da4130 --- /dev/null +++ b/tests/ui/mir/mir-inlining/auxiliary/internal.rs @@ -0,0 +1,7 @@ +// compile-flags: -Zalways-encode-mir + +static S: usize = 42; + +pub fn f() -> &'static usize { + &S +}