From 011a2363db4220fcbca9443b2718ac4e8d200cab Mon Sep 17 00:00:00 2001 From: EDSONZ-WASSWA Date: Tue, 17 Nov 2026 22:43:44 +0300 Subject: [PATCH 1/2] Preserve unsafe qualifier in overload! --- .../src/bin/diag-unsafe-fn.rs | 29 ++++++++ .../src/bin/diag-unsafe-fn.stderr | 34 ++++++++++ splat-overload-test/src/bin/unsafe-fn.rs | 32 +++++++++ splat-overload/src/lib.rs | 66 ++++++++++++++++--- 4 files changed, 151 insertions(+), 10 deletions(-) create mode 100644 splat-overload-diagnostics/src/bin/diag-unsafe-fn.rs create mode 100644 splat-overload-diagnostics/src/bin/diag-unsafe-fn.stderr create mode 100644 splat-overload-test/src/bin/unsafe-fn.rs diff --git a/splat-overload-diagnostics/src/bin/diag-unsafe-fn.rs b/splat-overload-diagnostics/src/bin/diag-unsafe-fn.rs new file mode 100644 index 0000000..b3c6010 --- /dev/null +++ b/splat-overload-diagnostics/src/bin/diag-unsafe-fn.rs @@ -0,0 +1,29 @@ +#![feature(splat)] +#![feature(tuple_trait)] +#![allow(incomplete_features, unused_braces)] + +use splat_overload::overload; + +struct Reader; + +overload! { + unsafe fn read(ptr: *const i32) -> i32 { unsafe { *ptr } } + unsafe fn read(ptr: *const i32, offset: usize) -> i32 { unsafe { *ptr.add(offset) } } +} + +overload! { + impl Reader { + unsafe fn get(&self, ptr: *const i32) -> i32 { unsafe { *ptr } } + unsafe fn get(&self, ptr: *const i32, offset: usize) -> i32 { unsafe { *ptr.add(offset) } } + } +} + +fn main() { + let x = 42; + let ptr = &raw const x; + // Missing unsafe block + read(ptr); + read(ptr, 0); + Reader.get(ptr); + Reader.get(ptr, 0); +} diff --git a/splat-overload-diagnostics/src/bin/diag-unsafe-fn.stderr b/splat-overload-diagnostics/src/bin/diag-unsafe-fn.stderr new file mode 100644 index 0000000..84e52c3 --- /dev/null +++ b/splat-overload-diagnostics/src/bin/diag-unsafe-fn.stderr @@ -0,0 +1,34 @@ +error[E0133]: call to unsafe function `read` is unsafe and requires unsafe block + --> splat-overload-diagnostics/src/bin/diag-unsafe-fn.rs:25:5 + | +25 | read(ptr); + | ^^^^^^^^^ call to unsafe function + | + = note: consult the function's documentation for information on how to avoid undefined behavior + +error[E0133]: call to unsafe function `read` is unsafe and requires unsafe block + --> splat-overload-diagnostics/src/bin/diag-unsafe-fn.rs:26:5 + | +26 | read(ptr, 0); + | ^^^^^^^^^^^^ call to unsafe function + | + = note: consult the function's documentation for information on how to avoid undefined behavior + +error[E0133]: call to unsafe function `Reader::get` is unsafe and requires unsafe block + --> splat-overload-diagnostics/src/bin/diag-unsafe-fn.rs:27:5 + | +27 | Reader.get(ptr); + | ^^^^^^^^^^^^^^^ call to unsafe function + | + = note: consult the function's documentation for information on how to avoid undefined behavior + +error[E0133]: call to unsafe function `Reader::get` is unsafe and requires unsafe block + --> splat-overload-diagnostics/src/bin/diag-unsafe-fn.rs:28:5 + | +28 | Reader.get(ptr, 0); + | ^^^^^^^^^^^^^^^^^^ call to unsafe function + | + = note: consult the function's documentation for information on how to avoid undefined behavior + +For more information about this error, try `rustc --explain E0133`. +error: could not compile `splat-overload-diagnostics` (bin "diag-unsafe-fn") due to 4 previous errors diff --git a/splat-overload-test/src/bin/unsafe-fn.rs b/splat-overload-test/src/bin/unsafe-fn.rs new file mode 100644 index 0000000..afcae9b --- /dev/null +++ b/splat-overload-test/src/bin/unsafe-fn.rs @@ -0,0 +1,32 @@ +#![feature(splat)] +#![feature(tuple_trait)] +#![allow(incomplete_features)] +#![allow(unused_braces)] + +use splat_overload::overload; + +struct Reader; + +overload! { + unsafe fn read(ptr: *const i32) -> i32 { unsafe { *ptr } } + unsafe fn read(ptr: *const i32, offset: usize) -> i32 { unsafe { *ptr.add(offset) } } +} + +overload! { + impl Reader { + unsafe fn get(&self, ptr: *const i32) -> i32 { unsafe { *ptr } } + unsafe fn get(&self, ptr: *const i32, offset: usize) -> i32 { unsafe { *ptr.add(offset) } } + } +} + +fn main() { + let x = 42; + let ptr = &raw const x; + // SAFETY: `ptr` points to `x`, which is live, and the offset is zero. + unsafe { + assert_eq!(read(ptr), 42); + assert_eq!(read(ptr, 0), 42); + assert_eq!(Reader.get(ptr), 42); + assert_eq!(Reader.get(ptr, 0), 42); + } +} diff --git a/splat-overload/src/lib.rs b/splat-overload/src/lib.rs index b30704a..08facf5 100644 --- a/splat-overload/src/lib.rs +++ b/splat-overload/src/lib.rs @@ -101,6 +101,38 @@ fn check_overload_receiver_kind(functions: &[ItemFn]) -> ReceiverKind { first_receiver } +/// Checks that all the overloaded functions are either `unsafe` or safe, and returns the `unsafe` +/// qualifier if they have one. +/// +/// All the overloads are dispatched through a single function, which can only be safe or unsafe. +/// A safe dispatch function would let safe code call the unsafe overloads. +fn check_overload_unsafety(functions: &[ItemFn]) -> Option { + let first_unsafety = functions[0].sig.unsafety; + for func in functions { + if func.sig.unsafety.is_some() != first_unsafety.is_some() { + panic!( + "all overloads must have the same safety (all `unsafe fn`, or all `fn`), \ + got a mix of safe and unsafe overloads for {}", + func.sig.ident + ); + } + } + first_unsafety +} + +/// Wraps `call` in an `unsafe` block if the overloads are unsafe. +/// Safe overloads are left unchanged, to avoid `unused_unsafe` warnings. +fn wrap_unsafe_call( + unsafety: Option, + call: proc_macro2::TokenStream, +) -> proc_macro2::TokenStream { + if unsafety.is_some() { + quote! { unsafe { #call } } + } else { + call + } +} + impl Parse for OverloadInput { /// Parse the input list of functions into different function kind variants. fn parse(input: ParseStream) -> Result { @@ -218,6 +250,10 @@ fn generate_free_functions(functions: Vec) -> TokenStream { let fn_name = check_overload_names(&functions); let trait_name = trait_name_for(fn_name); + // Check all the overloads have the same safety, if not, panic. + let unsafety = check_overload_unsafety(&functions); + let dispatch_call = wrap_unsafe_call(unsafety, quote! { args.call() }); + let mut impls = Vec::new(); for func in &functions { let (arg_types, arg_names, arg_indices) = collect_args(func); @@ -228,7 +264,7 @@ fn generate_free_functions(functions: Vec) -> TokenStream { impls.push(quote! { impl #trait_name for #tuple_ty { type Output = #output_ty; - fn call(self) -> Self::Output { + #unsafety fn call(self) -> Self::Output { #(let #arg_names = #arg_indices;)* #block } @@ -245,13 +281,13 @@ fn generate_free_functions(functions: Vec) -> TokenStream { )] trait #trait_name: std::marker::Tuple { type Output; - fn call(self) -> Self::Output; + #unsafety fn call(self) -> Self::Output; } #(#impls)* - fn #fn_name(#[rustc_splat] args: T) -> T::Output { - args.call() + #unsafety fn #fn_name(#[rustc_splat] args: T) -> T::Output { + #dispatch_call } }; @@ -313,6 +349,9 @@ fn generate_methods(self_ty: syn::Ident, functions: Vec) -> TokenStream // Check all the receiver kinds match, if not, panic. let receiver_kind = check_overload_receiver_kind(&functions); + // Check all the overloads have the same safety, if not, panic. + let unsafety = check_overload_unsafety(&functions); + // Generate fragments used to generate the trait impmentation. // The generic type used for the `this` argument pseudo-receiver. let this_generic_ty = match receiver_kind { @@ -354,18 +393,24 @@ fn generate_methods(self_ty: syn::Ident, functions: Vec) -> TokenStream // The hidden impl method name for the current function. let hidden_name = quote::format_ident!("__{}_impl_{}", fn_name, i); hidden_methods.push(quote! { - fn #hidden_name(#func_receiver #(#arg_names: #arg_types),*) -> #output_ty { + #unsafety fn #hidden_name(#func_receiver #(#arg_names: #arg_types),*) -> #output_ty { #block } }); + // The call to the hidden impl method, which is unsafe if the overloads are unsafe. + let hidden_call = wrap_unsafe_call( + unsafety, + quote! { #caller_receiver #hidden_name(#(#arg_names),*) }, + ); + // The full trait impl for this specific function overload. impls.push(quote! { impl #trait_name<#self_ty> for #tuple_ty { type Output = #output_ty; - fn call(self #this_concrete_ty) -> Self::Output { + #unsafety fn call(self #this_concrete_ty) -> Self::Output { #(let #arg_names = #arg_indices;)* - #caller_receiver #hidden_name(#(#arg_names),*) + #hidden_call } } }); @@ -377,6 +422,7 @@ fn generate_methods(self_ty: syn::Ident, functions: Vec) -> TokenStream Some(FnArg::Receiver(r)) => (quote! { #r }, quote! { , }, quote! { self }), _ => (quote! {}, quote! {}, quote! {}), }; + let dispatch_call = wrap_unsafe_call(unsafety, quote! { args.call(#self_arg) }); let generated = quote! { #[diagnostic::on_unimplemented( @@ -387,7 +433,7 @@ fn generate_methods(self_ty: syn::Ident, functions: Vec) -> TokenStream )] trait #trait_name: std::marker::Tuple { type Output; - fn call(self #this_generic_ty) -> Self::Output; + #unsafety fn call(self #this_generic_ty) -> Self::Output; } #(#impls)* @@ -395,8 +441,8 @@ fn generate_methods(self_ty: syn::Ident, functions: Vec) -> TokenStream impl #self_ty { #(#hidden_methods)* - fn #fn_name>(#receiver #receiver_comma #[rustc_splat] args: T) -> T::Output { - args.call(#self_arg) + #unsafety fn #fn_name>(#receiver #receiver_comma #[rustc_splat] args: T) -> T::Output { + #dispatch_call } } }; From 6a6143b8530bb2f72fd652fcd02d2fbe57debd54 Mon Sep 17 00:00:00 2001 From: teor Date: Wed, 18 Nov 2026 07:01:35 +1000 Subject: [PATCH 2/2] Add comment explaining how mixed unsafe would have to work --- splat-overload/src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/splat-overload/src/lib.rs b/splat-overload/src/lib.rs index 08facf5..7309918 100644 --- a/splat-overload/src/lib.rs +++ b/splat-overload/src/lib.rs @@ -106,6 +106,10 @@ fn check_overload_receiver_kind(functions: &[ItemFn]) -> ReceiverKind { /// /// All the overloads are dispatched through a single function, which can only be safe or unsafe. /// A safe dispatch function would let safe code call the unsafe overloads. +/// +/// FIXME: mixed safe and unsafe overloads can't be supported using the trait-on-receiver-wrapper +/// strategy, because trait method impls can't differ in safety. An ergonomic solution will likely +/// require compiler support. fn check_overload_unsafety(functions: &[ItemFn]) -> Option { let first_unsafety = functions[0].sig.unsafety; for func in functions {