Skip to content

Implement Display for rustc_target::callconv::Conv #135808

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 10 commits into from
May 17, 2025
Merged
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
4 changes: 2 additions & 2 deletions compiler/rustc_const_eval/src/interpret/call.rs
Original file line number Diff line number Diff line change
@@ -353,8 +353,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
if caller_fn_abi.conv != callee_fn_abi.conv {
throw_ub_custom!(
fluent::const_eval_incompatible_calling_conventions,
callee_conv = format!("{:?}", callee_fn_abi.conv),
caller_conv = format!("{:?}", caller_fn_abi.conv),
callee_conv = format!("{}", callee_fn_abi.conv),
caller_conv = format!("{}", caller_fn_abi.conv),
)
}

32 changes: 32 additions & 0 deletions compiler/rustc_target/src/callconv/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fmt::Display;
use std::str::FromStr;
use std::{fmt, iter};

@@ -897,6 +898,37 @@ impl FromStr for Conv {
}
}

fn conv_to_externabi(conv: &Conv) -> ExternAbi {
match conv {
Conv::C => ExternAbi::C { unwind: false },
Conv::Rust => ExternAbi::Rust,
Conv::PreserveMost => ExternAbi::RustCold,
Conv::ArmAapcs => ExternAbi::Aapcs { unwind: false },
Conv::CCmseNonSecureCall => ExternAbi::CCmseNonSecureCall,
Conv::CCmseNonSecureEntry => ExternAbi::CCmseNonSecureEntry,
Conv::Msp430Intr => ExternAbi::Msp430Interrupt,
Conv::GpuKernel => ExternAbi::GpuKernel,
Conv::X86Fastcall => ExternAbi::Fastcall { unwind: false },
Conv::X86Intr => ExternAbi::X86Interrupt,
Conv::X86Stdcall => ExternAbi::Stdcall { unwind: false },
Conv::X86ThisCall => ExternAbi::Thiscall { unwind: false },
Conv::X86VectorCall => ExternAbi::Vectorcall { unwind: false },
Conv::X86_64SysV => ExternAbi::SysV64 { unwind: false },
Conv::X86_64Win64 => ExternAbi::Win64 { unwind: false },
Conv::AvrInterrupt => ExternAbi::AvrInterrupt,
Conv::AvrNonBlockingInterrupt => ExternAbi::AvrNonBlockingInterrupt,
Conv::RiscvInterrupt { kind: RiscvInterruptKind::Machine } => ExternAbi::RiscvInterruptM,
Conv::RiscvInterrupt { kind: RiscvInterruptKind::Supervisor } => ExternAbi::RiscvInterruptS,
Conv::Cold | Conv::PreserveAll => unreachable!(),
}
}

impl Display for Conv {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", conv_to_externabi(self))
}
}

// Some types are used a lot. Make sure they don't unintentionally get bigger.
#[cfg(target_pointer_width = "64")]
mod size_asserts {
9 changes: 4 additions & 5 deletions src/tools/miri/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -928,12 +928,11 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
self.read_c_str_with_char_size(ptr, wchar_t.size, wchar_t.align.abi)
}

/// Check that the ABI is what we expect.
fn check_abi<'a>(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>, exp_abi: Conv) -> InterpResult<'a, ()> {
/// Check that the calling convention is what we expect.
fn check_callconv<'a>(&self, fn_abi: &FnAbi<'tcx, Ty<'tcx>>, exp_abi: Conv) -> InterpResult<'a, ()> {
if fn_abi.conv != exp_abi {
throw_ub_format!(
"calling a function with ABI {:?} using caller ABI {:?}",
exp_abi,
"calling a function with calling convention {exp_abi} using caller calling convention {}",
fn_abi.conv
);
}
@@ -969,7 +968,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
exp_abi: Conv,
link_name: Symbol,
) -> InterpResult<'tcx, ()> {
self.check_abi(abi, exp_abi)?;
self.check_callconv(abi, exp_abi)?;
if let Some((body, instance)) = self.eval_context_mut().lookup_exported_symbol(link_name)? {
// If compiler-builtins is providing the symbol, then don't treat it as a clash.
// We'll use our built-in implementation in `emulate_foreign_item_inner` for increased
2 changes: 1 addition & 1 deletion src/tools/miri/tests/fail/function_calls/check_arg_abi.rs
Original file line number Diff line number Diff line change
@@ -4,6 +4,6 @@ fn main() {
}

unsafe {
let _ = malloc(0); //~ ERROR: calling a function with ABI C using caller ABI Rust
let _ = malloc(0); //~ ERROR: calling a function with calling convention "C" using caller calling convention "Rust"
};
}
4 changes: 2 additions & 2 deletions src/tools/miri/tests/fail/function_calls/check_arg_abi.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: Undefined Behavior: calling a function with ABI C using caller ABI Rust
error: Undefined Behavior: calling a function with calling convention "C" using caller calling convention "Rust"
--> tests/fail/function_calls/check_arg_abi.rs:LL:CC
|
LL | let _ = malloc(0);
| ^^^^^^^^^ calling a function with ABI C using caller ABI Rust
| ^^^^^^^^^ calling a function with calling convention "C" using caller calling convention "Rust"
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ fn main() {
// Make sure we check the ABI when Miri itself invokes a function
// as part of a shim implementation.
std::intrinsics::catch_unwind(
//~^ ERROR: calling a function with calling convention C using calling convention Rust
//~^ ERROR: calling a function with calling convention "C" using calling convention "Rust"
std::mem::transmute::<extern "C" fn(*mut u8), _>(try_fn),
std::ptr::null_mut(),
|_, _| unreachable!(),
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: Undefined Behavior: calling a function with calling convention C using calling convention Rust
error: Undefined Behavior: calling a function with calling convention "C" using calling convention "Rust"
--> tests/fail/function_calls/check_callback_abi.rs:LL:CC
|
LL | / std::intrinsics::catch_unwind(
@@ -7,7 +7,7 @@ LL | | std::mem::transmute::<extern "C" fn(*mut u8), _>(try_fn),
LL | | std::ptr::null_mut(),
LL | | |_, _| unreachable!(),
LL | | );
| |_________^ calling a function with calling convention C using calling convention Rust
| |_________^ calling a function with calling convention "C" using calling convention "Rust"
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: Undefined Behavior: calling a function with calling convention Rust using calling convention C
error: Undefined Behavior: calling a function with calling convention "Rust" using calling convention "C"
--> tests/fail/function_calls/exported_symbol_abi_mismatch.rs:LL:CC
|
LL | foo();
| ^^^^^ calling a function with calling convention Rust using calling convention C
| ^^^^^ calling a function with calling convention "Rust" using calling convention "C"
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: Undefined Behavior: calling a function with calling convention Rust using calling convention C
error: Undefined Behavior: calling a function with calling convention "Rust" using calling convention "C"
--> tests/fail/function_calls/exported_symbol_abi_mismatch.rs:LL:CC
|
LL | std::mem::transmute::<unsafe fn(), unsafe extern "C" fn()>(foo)();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ calling a function with calling convention Rust using calling convention C
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ calling a function with calling convention "Rust" using calling convention "C"
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: Undefined Behavior: calling a function with calling convention Rust using calling convention C
error: Undefined Behavior: calling a function with calling convention "Rust" using calling convention "C"
--> tests/fail/function_calls/exported_symbol_abi_mismatch.rs:LL:CC
|
LL | foo();
| ^^^^^ calling a function with calling convention Rust using calling convention C
| ^^^^^ calling a function with calling convention "Rust" using calling convention "C"
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ fn main() {
#[cfg(fn_ptr)]
unsafe {
std::mem::transmute::<unsafe fn(), unsafe extern "C" fn()>(foo)();
//~[fn_ptr]^ ERROR: calling a function with calling convention Rust using calling convention C
//~[fn_ptr]^ ERROR: calling a function with calling convention "Rust" using calling convention "C"
}

// `Instance` caching should not suppress ABI check.
@@ -28,8 +28,8 @@ fn main() {
}
unsafe {
foo();
//~[no_cache]^ ERROR: calling a function with calling convention Rust using calling convention C
//~[cache]| ERROR: calling a function with calling convention Rust using calling convention C
//~[no_cache]^ ERROR: calling a function with calling convention "Rust" using calling convention "C"
//~[cache]| ERROR: calling a function with calling convention "Rust" using calling convention "C"
}
}
}
2 changes: 1 addition & 1 deletion src/tools/miri/tests/fail/tail_calls/cc-mismatch.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//@error-in-other-file: Undefined Behavior: calling a function with calling convention C using calling convention Rust
//@error-in-other-file: Undefined Behavior: calling a function with calling convention "C" using calling convention "Rust"
#![feature(explicit_tail_calls)]
#![allow(incomplete_features)]

4 changes: 2 additions & 2 deletions src/tools/miri/tests/fail/tail_calls/cc-mismatch.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: Undefined Behavior: calling a function with calling convention C using calling convention Rust
error: Undefined Behavior: calling a function with calling convention "C" using calling convention "Rust"
--> RUSTLIB/core/src/ops/function.rs:LL:CC
|
LL | extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ calling a function with calling convention C using calling convention Rust
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ calling a function with calling convention "C" using calling convention "Rust"
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
2 changes: 1 addition & 1 deletion tests/ui/consts/miri_unleashed/abi-mismatch.rs
Original file line number Diff line number Diff line change
@@ -10,6 +10,6 @@ const fn call_rust_fn(my_fn: extern "Rust" fn()) {

static VAL: () = call_rust_fn(unsafe { std::mem::transmute(c_fn as extern "C" fn()) });
//~^ ERROR could not evaluate static initializer
//~| NOTE calling a function with calling convention C using calling convention Rust
//~| NOTE calling a function with calling convention "C" using calling convention "Rust"

fn main() {}
2 changes: 1 addition & 1 deletion tests/ui/consts/miri_unleashed/abi-mismatch.stderr
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ error[E0080]: could not evaluate static initializer
--> $DIR/abi-mismatch.rs:11:18
|
LL | static VAL: () = call_rust_fn(unsafe { std::mem::transmute(c_fn as extern "C" fn()) });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ calling a function with calling convention C using calling convention Rust
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ calling a function with calling convention "C" using calling convention "Rust"
|
note: inside `call_rust_fn`
--> $DIR/abi-mismatch.rs:7:5