Skip to content

Commit 3a1ee64

Browse files
Resolve instance for SymFn in global/naked asm
1 parent 0eb0b8c commit 3a1ee64

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed

compiler/rustc_codegen_ssa/src/base.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,13 @@ where
457457
rustc_hir::InlineAsmOperand::SymFn { expr } => {
458458
let ty = cx.tcx().typeck(item_id.owner_id).expr_ty(expr);
459459
let instance = match ty.kind() {
460-
&ty::FnDef(def_id, args) => Instance::new(def_id, args),
460+
&ty::FnDef(def_id, args) => Instance::expect_resolve(
461+
cx.tcx(),
462+
ty::TypingEnv::fully_monomorphized(),
463+
def_id,
464+
args,
465+
expr.span,
466+
),
461467
_ => span_bug!(*op_sp, "asm sym is not a function"),
462468
};
463469

compiler/rustc_codegen_ssa/src/mir/naked_asm.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ fn inline_to_global_operand<'a, 'tcx, Cx: LayoutOf<'tcx, LayoutOfResult = TyAndL
9595
);
9696

9797
let instance = match mono_type.kind() {
98-
&ty::FnDef(def_id, args) => Instance::new(def_id, args),
98+
&ty::FnDef(def_id, args) => {
99+
Instance::expect_resolve(cx.tcx(), cx.typing_env(), def_id, args, value.span)
100+
}
99101
_ => bug!("asm sym is not a function"),
100102
};
101103

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Test that we're properly monomorphizing sym args in global asm blocks
2+
// that point to associated items.
3+
4+
//@ edition: 2021
5+
//@ needs-asm-support
6+
//@ only-x86_64-unknown-linux-gnu
7+
//@ build-pass
8+
9+
#![no_main]
10+
11+
use std::arch::global_asm;
12+
13+
fn foo() {
14+
loop {}
15+
}
16+
17+
trait Foo {
18+
fn bar();
19+
}
20+
21+
impl Foo for i32 {
22+
fn bar() {
23+
loop {}
24+
}
25+
}
26+
27+
global_asm!(".global main", "main:", "call {}", sym <i32 as Foo>::bar);

tests/ui/asm/naked-asm-mono-sym-fn.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Regression test for <https://github.com/rust-lang/rust/issues/140373>.
2+
// Test that we're properly monomorphizing sym args in naked asm blocks
3+
// that point to associated items.
4+
5+
//@ edition: 2021
6+
//@ needs-asm-support
7+
//@ only-x86_64
8+
//@ build-pass
9+
10+
trait T {
11+
extern "C" fn t();
12+
}
13+
14+
enum E<const C: usize> {}
15+
16+
impl<const C: usize> T for E<C> {
17+
extern "C" fn t() {
18+
println!("Const generic: {}", C);
19+
}
20+
}
21+
22+
#[unsafe(naked)]
23+
extern "C" fn foo<U: T>() {
24+
core::arch::naked_asm!(
25+
"push rax",
26+
"call {fn}",
27+
"pop rax",
28+
"ret",
29+
fn = sym <U as T>::t,
30+
);
31+
}
32+
33+
fn main() {
34+
foo::<E<42>>();
35+
}

0 commit comments

Comments
 (0)