-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
Stabilize c-variadic function definitions #155697
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -189,8 +189,6 @@ crate::cfg_select! { | |
| /// is automatically initialized (equivalent to calling `va_start` in C). | ||
| /// | ||
| /// ``` | ||
| /// #![feature(c_variadic)] | ||
| /// | ||
| /// use std::ffi::VaList; | ||
| /// | ||
| /// /// # Safety | ||
|
|
@@ -224,11 +222,13 @@ crate::cfg_select! { | |
| /// terms of layout and ABI. | ||
| #[repr(transparent)] | ||
| #[lang = "va_list"] | ||
| #[stable(feature = "c_variadic", since = "CURRENT_RUSTC_VERSION")] | ||
| pub struct VaList<'a> { | ||
| inner: VaListInner, | ||
| _marker: PhantomCovariantLifetime<'a>, | ||
| } | ||
|
|
||
| #[stable(feature = "c_variadic", since = "CURRENT_RUSTC_VERSION")] | ||
| impl fmt::Debug for VaList<'_> { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| // No need to include `_marker` in debug output. | ||
|
|
@@ -243,6 +243,7 @@ impl VaList<'_> { | |
| } | ||
| } | ||
|
|
||
| #[stable(feature = "c_variadic", since = "CURRENT_RUSTC_VERSION")] | ||
| #[rustc_const_unstable(feature = "const_c_variadic", issue = "151787")] | ||
| impl<'f> const Clone for VaList<'f> { | ||
| #[inline] // Avoid codegen when not used to help backends that don't support VaList. | ||
|
|
@@ -255,6 +256,7 @@ impl<'f> const Clone for VaList<'f> { | |
| } | ||
| } | ||
|
|
||
| #[stable(feature = "c_variadic", since = "CURRENT_RUSTC_VERSION")] | ||
| #[rustc_const_unstable(feature = "const_c_variadic", issue = "151787")] | ||
| impl<'f> const Drop for VaList<'f> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This The stabilization report says that “The
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #155821 adds some clarification that the implementation here calls the rust |
||
| #[inline] // Avoid codegen when not used to help backends that don't support VaList. | ||
|
|
@@ -264,6 +266,7 @@ impl<'f> const Drop for VaList<'f> { | |
| } | ||
| } | ||
|
|
||
| #[unstable(feature = "c_variadic_internals", issue = "none")] | ||
| mod sealed { | ||
| pub trait Sealed {} | ||
|
|
||
|
|
@@ -318,6 +321,7 @@ mod sealed { | |
| // types with an alignment larger than 8, or with a non-scalar layout. Inline assembly can be used | ||
| // to accept unsupported types in the meantime. | ||
| #[lang = "va_arg_safe"] | ||
| #[stable(feature = "c_variadic", since = "CURRENT_RUSTC_VERSION")] | ||
| pub unsafe trait VaArgSafe: sealed::Sealed {} | ||
|
|
||
| crate::cfg_select! { | ||
|
|
@@ -326,7 +330,9 @@ crate::cfg_select! { | |
| // | ||
| // - i8 is implicitly promoted to c_int in C, and cannot implement `VaArgSafe`. | ||
| // - u8 is implicitly promoted to c_uint in C, and cannot implement `VaArgSafe`. | ||
| #[stable(feature = "c_variadic", since = "CURRENT_RUSTC_VERSION")] | ||
| unsafe impl VaArgSafe for i16 {} | ||
| #[stable(feature = "c_variadic", since = "CURRENT_RUSTC_VERSION")] | ||
| unsafe impl VaArgSafe for u16 {} | ||
| } | ||
| _ => { | ||
|
|
@@ -340,6 +346,7 @@ crate::cfg_select! { | |
| crate::cfg_select! { | ||
| target_arch = "avr" => { | ||
| // c_double is f32 on this target. | ||
| #[stable(feature = "c_variadic", since = "CURRENT_RUSTC_VERSION")] | ||
| unsafe impl VaArgSafe for f32 {} | ||
| } | ||
| _ => { | ||
|
|
@@ -349,17 +356,26 @@ crate::cfg_select! { | |
| } | ||
| } | ||
|
|
||
| #[stable(feature = "c_variadic", since = "CURRENT_RUSTC_VERSION")] | ||
| unsafe impl VaArgSafe for i32 {} | ||
| #[stable(feature = "c_variadic", since = "CURRENT_RUSTC_VERSION")] | ||
| unsafe impl VaArgSafe for i64 {} | ||
| #[stable(feature = "c_variadic", since = "CURRENT_RUSTC_VERSION")] | ||
| unsafe impl VaArgSafe for isize {} | ||
|
|
||
| #[stable(feature = "c_variadic", since = "CURRENT_RUSTC_VERSION")] | ||
| unsafe impl VaArgSafe for u32 {} | ||
| #[stable(feature = "c_variadic", since = "CURRENT_RUSTC_VERSION")] | ||
| unsafe impl VaArgSafe for u64 {} | ||
| #[stable(feature = "c_variadic", since = "CURRENT_RUSTC_VERSION")] | ||
| unsafe impl VaArgSafe for usize {} | ||
|
|
||
| #[stable(feature = "c_variadic", since = "CURRENT_RUSTC_VERSION")] | ||
| unsafe impl VaArgSafe for f64 {} | ||
|
|
||
| #[stable(feature = "c_variadic", since = "CURRENT_RUSTC_VERSION")] | ||
| unsafe impl<T> VaArgSafe for *mut T {} | ||
| #[stable(feature = "c_variadic", since = "CURRENT_RUSTC_VERSION")] | ||
| unsafe impl<T> VaArgSafe for *const T {} | ||
|
|
||
| // Check that relevant `core::ffi` types implement `VaArgSafe`. | ||
|
|
@@ -390,6 +406,7 @@ impl<'f> VaList<'f> { | |
| /// Calling this function with an incompatible type, an invalid value, or when there | ||
| /// are no more variable arguments, is unsound. | ||
| #[inline] // Avoid codegen when not used to help backends that don't support VaList. | ||
| #[stable(feature = "c_variadic", since = "CURRENT_RUSTC_VERSION")] | ||
| #[rustc_const_unstable(feature = "const_c_variadic", issue = "151787")] | ||
| pub const unsafe fn next_arg<T: VaArgSafe>(&mut self) -> T { | ||
| // SAFETY: the caller must uphold the safety contract for `va_arg`. | ||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,3 @@ | ||
| #![feature(c_variadic)] | ||
|
|
||
| unsafe extern "C" fn helper(_: i32, _: ...) {} | ||
|
|
||
| fn main() { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,3 @@ | ||
| #![feature(c_variadic)] | ||
|
|
||
| unsafe extern "C" fn helper(_: i32, _: ...) {} | ||
|
|
||
| fn main() { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,3 @@ | ||
| #![feature(c_variadic)] | ||
|
|
||
| use std::ffi::{CStr, VaList, c_char, c_double, c_int, c_long}; | ||
|
|
||
| fn ignores_arguments() { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,6 @@ | |
| // See issue #58853. | ||
|
|
||
| //@ pp-exact | ||
| #![feature(c_variadic)] | ||
|
|
||
| extern "C" { | ||
| pub fn foo(x: i32, ...); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Using a random file for a thread)
I wonder, is this too strict? And does this restriction apply across FFI?
I'm thinking about a
printfimplementation where it is common and valid (as far as I know) to use%xwith both signed and unsigned integers.View changes since the review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is too strict, and the reference PR does not state it this strongly.
There is a difference here between what C says (in section
7.16.1.1):So signedness is irrelevant, and my interpretation of the other rules is that any pointer type is compatible with any other pointer type (in the same address space).
That is not what Miri currently implements, it instead uses strict type equality because @RalfJung did not have much appetite for (from memory, the third, but in any case) another notion of type equivalence. I went with the more restrictive formulation because we can always relax it later, the inverse is not true.
Perhaps T-opsem has suggestions for a better way to phrase this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we can relax the signedness requirements to match C then I think that is preferable, so we don't need to worry about soundness across FFI. It makes sense that Miri should match up with whatever behavior is decided, pinging @rust-lang/miri for thoughts here.
Is "compatible types" in the context of varargs defined anywhere? My read is that if they mean va-compatible then you could consider
int *andunsigned *to be compatible, and you could considervoid *andchar *to be compatible, but you couldn't considerint *andlong *to be compatible. Though I don't think Ive ever seen anybody cast pointers to void before using%p.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it does mean va-compatible then
unsafe impl<T> VaArgSafe for *mut T {}may not be correctThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm no, I think your read is correct.
Based on the last rule I think
*mut Talwaysbut then the only valid value you can provide there is the NULL pointer... So then
unsafe impl<T: VaArgSafe> VaArgSafe for *mut T {}might be more accurate.That's really cumbersome, and I've similarly never seen people cast to
void *when using%p. I don't really see a practical reason for it either.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah those are fair concerns. This will be messy to document but, well, that comes with the subject matter I guess.
If we want to allow such mismatch, that should be done in a separate PR which updates the docs, makes Miri perform the right checks, and also adds a Miri test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In progress in #155832
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that the va compatible rules are such that you can pass an integer with the arithmetic value 0..=i32::MAX to a function expecting u32 or i32. that is, cases where the arithmetic value can be represented in both types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, based on the standard you can go from signed to unsigned and vice versa, so long as the value is representable in both types. #155832 implements that check.
As the author of a c-variadic funtion you have to forward this requirement to your caller, and then in practice it seems much easier to have your caller do the conversion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh wow this is value-dependent? That's... gnarly. :/