diff options
| author | Michael Goulet <michael@errs.io> | 2025-04-27 21:55:50 +0000 |
|---|---|---|
| committer | Michael Goulet <michael@errs.io> | 2025-05-05 13:17:35 +0000 |
| commit | 3a1ee645cad501e2fd766bbacea5654a795f352b (patch) | |
| tree | 3e1b7b183e374efa343c52de64af42ba32a16d84 | |
| parent | 0eb0b8cb67ff2c37eab775eaac6b330347e5c97f (diff) | |
| download | rust-3a1ee645cad501e2fd766bbacea5654a795f352b.tar.gz rust-3a1ee645cad501e2fd766bbacea5654a795f352b.zip | |
Resolve instance for SymFn in global/naked asm
| -rw-r--r-- | compiler/rustc_codegen_ssa/src/base.rs | 8 | ||||
| -rw-r--r-- | compiler/rustc_codegen_ssa/src/mir/naked_asm.rs | 4 | ||||
| -rw-r--r-- | tests/ui/asm/global-asm-mono-sym-fn.rs | 27 | ||||
| -rw-r--r-- | tests/ui/asm/naked-asm-mono-sym-fn.rs | 35 |
4 files changed, 72 insertions, 2 deletions
diff --git a/compiler/rustc_codegen_ssa/src/base.rs b/compiler/rustc_codegen_ssa/src/base.rs index 89439e40937..775ab9071e7 100644 --- a/compiler/rustc_codegen_ssa/src/base.rs +++ b/compiler/rustc_codegen_ssa/src/base.rs @@ -457,7 +457,13 @@ where rustc_hir::InlineAsmOperand::SymFn { expr } => { let ty = cx.tcx().typeck(item_id.owner_id).expr_ty(expr); let instance = match ty.kind() { - &ty::FnDef(def_id, args) => Instance::new(def_id, args), + &ty::FnDef(def_id, args) => Instance::expect_resolve( + cx.tcx(), + ty::TypingEnv::fully_monomorphized(), + def_id, + args, + expr.span, + ), _ => span_bug!(*op_sp, "asm sym is not a function"), }; diff --git a/compiler/rustc_codegen_ssa/src/mir/naked_asm.rs b/compiler/rustc_codegen_ssa/src/mir/naked_asm.rs index 0301ef437c0..d2a687359e0 100644 --- a/compiler/rustc_codegen_ssa/src/mir/naked_asm.rs +++ b/compiler/rustc_codegen_ssa/src/mir/naked_asm.rs @@ -95,7 +95,9 @@ fn inline_to_global_operand<'a, 'tcx, Cx: LayoutOf<'tcx, LayoutOfResult = TyAndL ); let instance = match mono_type.kind() { - &ty::FnDef(def_id, args) => Instance::new(def_id, args), + &ty::FnDef(def_id, args) => { + Instance::expect_resolve(cx.tcx(), cx.typing_env(), def_id, args, value.span) + } _ => bug!("asm sym is not a function"), }; diff --git a/tests/ui/asm/global-asm-mono-sym-fn.rs b/tests/ui/asm/global-asm-mono-sym-fn.rs new file mode 100644 index 00000000000..e584a98badb --- /dev/null +++ b/tests/ui/asm/global-asm-mono-sym-fn.rs @@ -0,0 +1,27 @@ +// Test that we're properly monomorphizing sym args in global asm blocks +// that point to associated items. + +//@ edition: 2021 +//@ needs-asm-support +//@ only-x86_64-unknown-linux-gnu +//@ build-pass + +#![no_main] + +use std::arch::global_asm; + +fn foo() { + loop {} +} + +trait Foo { + fn bar(); +} + +impl Foo for i32 { + fn bar() { + loop {} + } +} + +global_asm!(".global main", "main:", "call {}", sym <i32 as Foo>::bar); diff --git a/tests/ui/asm/naked-asm-mono-sym-fn.rs b/tests/ui/asm/naked-asm-mono-sym-fn.rs new file mode 100644 index 00000000000..2bd554beacf --- /dev/null +++ b/tests/ui/asm/naked-asm-mono-sym-fn.rs @@ -0,0 +1,35 @@ +// Regression test for <https://github.com/rust-lang/rust/issues/140373>. +// Test that we're properly monomorphizing sym args in naked asm blocks +// that point to associated items. + +//@ edition: 2021 +//@ needs-asm-support +//@ only-x86_64 +//@ build-pass + +trait T { + extern "C" fn t(); +} + +enum E<const C: usize> {} + +impl<const C: usize> T for E<C> { + extern "C" fn t() { + println!("Const generic: {}", C); + } +} + +#[unsafe(naked)] +extern "C" fn foo<U: T>() { + core::arch::naked_asm!( + "push rax", + "call {fn}", + "pop rax", + "ret", + fn = sym <U as T>::t, + ); +} + +fn main() { + foo::<E<42>>(); +} |
