Skip to content

Commit 3d7402b

Browse files
Resolve instance for SymFn in global/naked asm
1 parent cb31a00 commit 3d7402b

File tree

5 files changed

+79
-3
lines changed

5 files changed

+79
-3
lines changed

compiler/rustc_codegen_cranelift/src/global_asm.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@ pub(crate) fn codegen_global_asm_item(tcx: TyCtxt<'_>, global_asm: &mut String,
6565

6666
let ty = tcx.typeck(item_id.owner_id).expr_ty(expr);
6767
let instance = match ty.kind() {
68-
&ty::FnDef(def_id, args) => Instance::new(def_id, args),
68+
&ty::FnDef(def_id, args) => Instance::expect_resolve(
69+
tcx,
70+
ty::TypingEnv::fully_monomorphized(),
71+
def_id,
72+
args,
73+
expr.span,
74+
),
6975
_ => span_bug!(op_sp, "asm sym is not a function"),
7076
};
7177
let symbol = tcx.symbol_name(instance);

compiler/rustc_codegen_ssa/src/mir/naked_asm.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ fn inline_to_global_operand<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
8686
);
8787

8888
let instance = match mono_type.kind() {
89-
&ty::FnDef(def_id, args) => Instance::new(def_id, args),
89+
&ty::FnDef(def_id, args) => {
90+
Instance::expect_resolve(cx.tcx(), cx.typing_env(), def_id, args, value.span)
91+
}
9092
_ => bug!("asm sym is not a function"),
9193
};
9294

compiler/rustc_codegen_ssa/src/mono_item.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,13 @@ impl<'a, 'tcx: 'a> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> {
7474
hir::InlineAsmOperand::SymFn { expr } => {
7575
let ty = cx.tcx().typeck(item_id.owner_id).expr_ty(expr);
7676
let instance = match ty.kind() {
77-
&ty::FnDef(def_id, args) => Instance::new(def_id, args),
77+
&ty::FnDef(def_id, args) => Instance::expect_resolve(
78+
cx.tcx(),
79+
ty::TypingEnv::fully_monomorphized(),
80+
def_id,
81+
args,
82+
expr.span,
83+
),
7884
_ => span_bug!(*op_sp, "asm sym is not a function"),
7985
};
8086

+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
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)