Skip to content

Fix inlining with -Zalways-encode-mir #115194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions compiler/rustc_mir_transform/src/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/mir/mir-inlining/always-encode-mirs.rs
Original file line number Diff line number Diff line change
@@ -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());
}
7 changes: 7 additions & 0 deletions tests/ui/mir/mir-inlining/auxiliary/internal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// compile-flags: -Zalways-encode-mir

static S: usize = 42;

pub fn f() -> &'static usize {
&S
}