Skip to content

Commit d4b5620

Browse files
committed
Impact Unsafe extern blocks changes
1 parent 8c789ff commit d4b5620

File tree

3 files changed

+37
-36
lines changed

3 files changed

+37
-36
lines changed

src/items/external-blocks.md

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,38 @@ blocks is only allowed in an `unsafe` context.
2323

2424
The external block defines its functions and statics in the [value namespace] of the module or block where it is located.
2525

26-
The `unsafe` keyword is syntactically allowed to appear before the `extern`
27-
keyword, but it is rejected at a semantic level. This allows macros to consume
28-
the syntax and make use of the `unsafe` keyword, before removing it from the
29-
token stream.
26+
Starting in edition 2024, the `unsafe` keyword is required to appear before the
27+
`extern` keyword. In previous editions is accepted but not required.
3028

3129
## Functions
3230

3331
Functions within external blocks are declared in the same way as other Rust
3432
functions, with the exception that they must not have a body and are instead
3533
terminated by a semicolon. Patterns are not allowed in parameters, only
36-
[IDENTIFIER] or `_` may be used. Function qualifiers (`const`, `async`,
37-
`unsafe`, and `extern`) are not allowed.
34+
[IDENTIFIER] or `_` may be used. Only safety funcion qualifiers are allowed
35+
(`unsafe`, `safe`), other function qualifiers (`const`, `async`, and `extern`)
36+
are not allowed.
3837

3938
Functions within external blocks may be called by Rust code, just like
4039
functions defined in Rust. The Rust compiler automatically translates between
4140
the Rust ABI and the foreign ABI.
4241

43-
A function declared in an extern block is implicitly `unsafe`. When coerced to
44-
a function pointer, a function declared in an extern block has type `unsafe
45-
extern "abi" for<'l1, ..., 'lm> fn(A1, ..., An) -> R`, where `'l1`, ... `'lm`
46-
are its lifetime parameters, `A1`, ..., `An` are the declared types of its
47-
parameters and `R` is the declared return type.
42+
A function declared with an explicit safety qualifier (`unsafe`, `safe`) would
43+
take such safety qualification, if no qualifier is present is implicitly
44+
`unsafe`. When coerced to a function pointer, a function declared in an extern
45+
block has type `unsafe extern "abi" for<'l1, ..., 'lm> fn(A1, ..., An) -> R`,
46+
where `'l1`, ... `'lm` are its lifetime parameters, `A1`, ..., `An` are the
47+
declared types of its parameters and `R` is the declared return type.
4848

4949
## Statics
5050

5151
Statics within external blocks are declared in the same way as [statics] outside of external blocks,
5252
except that they do not have an expression initializing their value.
53-
It is `unsafe` to access a static item declared in an extern block, whether or
54-
not it's mutable, because there is nothing guaranteeing that the bit pattern at the static's
55-
memory is valid for the type it is declared with, since some arbitrary (e.g. C) code is in charge
56-
of initializing the static.
53+
It is `unsafe` by default or if the item is declared as `unsafe` to access a static item declared in
54+
an extern block, unless the item was explicitly declared as `safe`.
55+
It does not matter if it's mutable, because there is nothing guaranteeing that the bit pattern at
56+
the static's memory is valid for the type it is declared with, since some arbitrary (e.g. C) code is
57+
in charge of initializing the static.
5758

5859
Extern statics can be either immutable or mutable just like [statics] outside of external blocks.
5960
An immutable static *must* be initialized before any Rust code is executed. It is not enough for
@@ -69,34 +70,34 @@ standard C ABI on the specific platform. Other ABIs may be specified using an
6970

7071
```rust
7172
// Interface to the Windows API
72-
extern "stdcall" { }
73+
unsafe extern "stdcall" { }
7374
```
7475

7576
There are three ABI strings which are cross-platform, and which all compilers
7677
are guaranteed to support:
7778

78-
* `extern "Rust"` -- The default ABI when you write a normal `fn foo()` in any
79+
* `unsafe extern "Rust"` -- The default ABI when you write a normal `fn foo()` in any
7980
Rust code.
80-
* `extern "C"` -- This is the same as `extern fn foo()`; whatever the default
81+
* `unsafe extern "C"` -- This is the same as `extern fn foo()`; whatever the default
8182
your C compiler supports.
82-
* `extern "system"` -- Usually the same as `extern "C"`, except on Win32, in
83+
* `unsafe extern "system"` -- Usually the same as `extern "C"`, except on Win32, in
8384
which case it's `"stdcall"`, or what you should use to link to the Windows
8485
API itself
8586

8687
There are also some platform-specific ABI strings:
8788

88-
* `extern "cdecl"` -- The default for x86\_32 C code.
89-
* `extern "stdcall"` -- The default for the Win32 API on x86\_32.
90-
* `extern "win64"` -- The default for C code on x86\_64 Windows.
91-
* `extern "sysv64"` -- The default for C code on non-Windows x86\_64.
92-
* `extern "aapcs"` -- The default for ARM.
93-
* `extern "fastcall"` -- The `fastcall` ABI -- corresponds to MSVC's
89+
* `unsafe extern "cdecl"` -- The default for x86\_32 C code.
90+
* `unsafe extern "stdcall"` -- The default for the Win32 API on x86\_32.
91+
* `unsafe extern "win64"` -- The default for C code on x86\_64 Windows.
92+
* `unsafe extern "sysv64"` -- The default for C code on non-Windows x86\_64.
93+
* `unsafe extern "aapcs"` -- The default for ARM.
94+
* `unsafe extern "fastcall"` -- The `fastcall` ABI -- corresponds to MSVC's
9495
`__fastcall` and GCC and clang's `__attribute__((fastcall))`
95-
* `extern "vectorcall"` -- The `vectorcall` ABI -- corresponds to MSVC's
96+
* `unsafe extern "vectorcall"` -- The `vectorcall` ABI -- corresponds to MSVC's
9697
`__vectorcall` and clang's `__attribute__((vectorcall))`
97-
* `extern "thiscall"` -- The default for C++ member functions on MSVC -- corresponds to MSVC's
98+
* `unsafe extern "thiscall"` -- The default for C++ member functions on MSVC -- corresponds to MSVC's
9899
`__thiscall` and GCC and clang's `__attribute__((thiscall))`
99-
* `extern "efiapi"` -- The ABI used for [UEFI] functions.
100+
* `unsafe extern "efiapi"` -- The ABI used for [UEFI] functions.
100101

101102
## Variadic functions
102103

@@ -105,7 +106,7 @@ last argument. The variadic parameter may optionally be specified with an
105106
identifier.
106107

107108
```rust
108-
extern "C" {
109+
unsafe extern "C" {
109110
fn foo(...);
110111
fn bar(x: i32, ...);
111112
fn with_name(format: *const u8, args: ...);
@@ -152,17 +153,17 @@ not specified.
152153
<!-- ignore: requires extern linking -->
153154
```rust,ignore
154155
#[link(name = "crypto")]
155-
extern {
156+
unsafe extern {
156157
// …
157158
}
158159
159160
#[link(name = "CoreFoundation", kind = "framework")]
160-
extern {
161+
unsafe extern {
161162
// …
162163
}
163164
164165
#[link(wasm_import_module = "foo")]
165-
extern {
166+
unsafe extern {
166167
// …
167168
}
168169
```
@@ -277,7 +278,7 @@ block to indicate the symbol to import for the given function or static. It
277278
uses the [_MetaNameValueStr_] syntax to specify the name of the symbol.
278279

279280
```rust
280-
extern {
281+
unsafe extern {
281282
#[link_name = "actual_symbol_name"]
282283
fn name_in_rust();
283284
}
@@ -306,7 +307,7 @@ it, and that assigned ordinal may change between builds of the binary.
306307
<!-- ignore: Only works on x86 Windows -->
307308
```rust,ignore
308309
#[link(name = "exporter", kind = "raw-dylib")]
309-
extern "stdcall" {
310+
unsafe extern "stdcall" {
310311
#[link_ordinal(15)]
311312
fn imported_function_stdcall(i: i32);
312313
}

src/items/functions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ their _definition_:
157157

158158
<!-- ignore: fake ABI -->
159159
```rust,ignore
160-
extern "ABI" {
160+
unsafe extern "ABI" {
161161
fn foo(); /* no body */
162162
}
163163
unsafe { foo() }

src/types/never.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn foo() -> ! {
1717
```
1818

1919
```rust
20-
extern "C" {
20+
unsafe extern "C" {
2121
pub fn no_return_extern_func() -> !;
2222
}
2323
```

0 commit comments

Comments
 (0)