Skip to content

Commit 0807e68

Browse files
committed
tweak and simplify
1 parent 3f29b09 commit 0807e68

1 file changed

Lines changed: 11 additions & 16 deletions

File tree

src/items/functions.md

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ r[items.fn.params.self-restriction]
8383
Functions with a self parameter may only appear as an [associated function] in a [trait] or [implementation].
8484

8585
r[items.fn.params.varargs]
86-
A parameter with the `...` token indicates a [c-variadic function]. The variadic parameter may have an optional identifier, such as `args: ...`.
86+
A parameter with the `...` token indicates a [c-variadic function], and may only be used as the last parameter. In an [`extern` block] the c-variadic parameter may have an optional identifier, such as `args: ...`, in a [c-variadic function definition] the identifier is mandatory.
8787

8888
r[items.fn.body]
8989
## Function body
@@ -340,43 +340,37 @@ A *c-variadic* function accepts a variable argument list `pat: ...` as its final
340340

341341
```rust
342342
unsafe extern "C" fn example(ap: ...) -> f64 {
343-
unsafe { ap.arg::<f64>() }
343+
unsafe { ap.next_arg::<f64>() }
344344
}
345345
```
346346

347347
This parameter stands in for an arbitrary number of arguments that may be passed by the caller.
348348

349349
> [!WARNING]
350-
> Passing an unexpected number of arguments or arguments of unexpected type to a variadic function may lead to [undefined behavior][undefined].
350+
> Passing an unexpected number of arguments or arguments of unexpected type to a c-variadic function may lead to [undefined behavior][undefined].
351351
352-
r[items.fn.c-variadic.variadic-parameter-type]
352+
r[items.fn.c-variadic.c-variadic-parameter-type]
353353
The type of `pat` in the function body is [`VaList`].
354354

355355
r[items.fn.c-variadic.desugar-brief]
356356
A c-variadic function definition is roughly equivalent to a function operating on a [`VaList`].
357357

358-
359-
360358
```rust
361359
// Source
362360
unsafe extern "C" fn example(mut ap: ...) -> i32 {
363-
unsafe { ap.arg::<i32>() }
361+
unsafe { ap.next_arg::<i32>() }
364362
}
365363
```
366364

367365
is roughly equivalent to:
368366

369367
```rust
370368
# use std::ffi::VaList;
371-
# use std::mem::MaybeUninit;
372-
# fn va_start() {}
373369
// Desugared
374370
unsafe extern "C" fn example() -> i32 {
375-
let mut storage = MaybeUninit::<VaList<'_>>::uninit();
376-
va_start(storage.as_mut_ptr()); // Initializes the VaList.
377-
let mut ap: &mut VaList<'_> = ap.assume_init_mut();
371+
let mut ap: VaList<'_> = /* compiler initializes the VaList */;
378372

379-
unsafe { ap.arg::<i32>() }
373+
unsafe { ap.next_arg::<i32>() }
380374

381375
va_end(ap)
382376
}
@@ -389,7 +383,7 @@ r[items.fn.c-variadic.ffi-compatibility]
389383
The rust [`VaList`] is ABI-compatible with the C `va_list` type.
390384

391385
r[items.fn.c-variadic.abi]
392-
Only `extern "C"` and `extern "C-unwind"` functions can accept a variable argument list.
386+
Only `extern "C"` and `extern "C-unwind"` function defintions can accept a variable argument list.
393387

394388
r[items.fn.c-variadic.safety]
395389
Only `unsafe` functions can accept a variable argument list.
@@ -403,7 +397,7 @@ A c-variadic functions cannot be `const`
403397
r[items.fn.c-variadic.platform-support]
404398
Some ABIs do not support c-variadic function definitions. The compiler errors in this case.
405399

406-
```
400+
```text
407401
error: the `bpfel` target does not support c-variadic functions
408402
--> $DIR/not-supported.rs:23:31
409403
|
@@ -412,7 +406,7 @@ LL | unsafe extern "C" fn variadic(_: ...) {}
412406
```
413407

414408
r[items.fn.c-variadic.dyn-compat]
415-
When a trait method is c-variadic, the trait is no longer dyn-compatible.
409+
When a trait method is c-variadic, the trait is no longer [dyn-compatible].
416410

417411
r[items.fn.attributes]
418412
## Attributes on functions
@@ -510,3 +504,4 @@ fn foo_oof(#[some_inert_attribute] arg: u8) {
510504
[`extern` block]: external-blocks.md
511505
[zero-sized]: glossary.zst
512506
[`VaList`]: std::ffi::VaList
507+
[dyn-compatible]: traits.md#dyn-compatibility

0 commit comments

Comments
 (0)