Skip to content

Commit 02bf556

Browse files
authored
Merge pull request #2001 from ehuss/extern-default
More clearly define the default abi of an extern block
2 parents 06ef608 + fefcd8e commit 02bf556

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/glossary.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ The alignment of a value specifies what addresses values are preferred to
1111
start at. Always a power of two. References to a value must be aligned.
1212
[More][alignment].
1313

14+
r[glossary.abi]
15+
### Application binary interface (ABI)
16+
17+
An *application binary interface* (ABI) defines how compiled code interacts with other compiled code. With [`extern` blocks] and [`extern fn`], *ABI strings* affect:
18+
19+
- **Calling convention**: How function arguments are passed, values are returned (e.g., in registers or on the stack), and who is responsible for cleaning up the stack.
20+
- **Unwinding**: Whether stack unwinding is allowed. For example, the `"C-unwind"` ABI allows unwinding across the FFI boundary, while the `"C"` ABI does not.
21+
1422
### Arity
1523

1624
Arity refers to the number of arguments a function or operator takes.
@@ -287,6 +295,8 @@ uninhabited type is "empty" in the sense that there are no values of the type. T
287295
example of an uninhabited type is the [never type] `!`, or an enum with no variants
288296
`enum Never { }`. Opposite of [Inhabited](#inhabited).
289297

298+
[`extern` blocks]: items.extern
299+
[`extern fn`]: items.fn.extern
290300
[alignment]: type-layout.md#size-and-alignment
291301
[associated item]: #associated-item
292302
[attributes]: attributes.md

src/items/external-blocks.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,21 @@ r[items.extern.abi]
9898
## ABI
9999

100100
r[items.extern.abi.intro]
101-
By default external blocks assume that the library they are calling uses the
102-
standard C ABI on the specific platform. Other ABIs may be specified using an
103-
`abi` string, as shown here:
101+
The `extern` keyword can be followed by an optional [ABI] string. The ABI specifies the calling convention of the functions in the block. The calling convention defines a low-level interface for functions, such as how arguments are placed in registers or on the stack, how return values are passed, and who is responsible for cleaning up the stack.
104102

105-
```rust
106-
// Interface to the Windows API
107-
unsafe extern "system" { }
108-
```
103+
> [!EXAMPLE]
104+
> ```rust
105+
> // Interface to the Windows API.
106+
> unsafe extern "system" { /* ... */ }
107+
> ```
108+
109+
r[items.extern.abi.default]
110+
If the ABI string is not specified, it defaults to `"C"`.
111+
112+
> [!NOTE]
113+
> The `extern` syntax without an explicit ABI is being phased out, so it's better to always write the ABI explicitly.
114+
>
115+
> For more details, see [Rust issue #134986](https://github.com/rust-lang/rust/issues/134986).
109116
110117
r[items.extern.abi.standard]
111118
The following ABI strings are supported on all platforms:
@@ -472,6 +479,7 @@ r[items.extern.attributes.fn-parameters]
472479
Attributes on extern function parameters follow the same rules and
473480
restrictions as [regular function parameters].
474481

482+
[ABI]: glossary.abi
475483
[PE Format]: https://learn.microsoft.com/windows/win32/debug/pe-format#import-name-type
476484
[UEFI]: https://uefi.org/specifications
477485
[WebAssembly module]: https://webassembly.github.io/spec/core/syntax/modules.html

0 commit comments

Comments
 (0)