@@ -23,34 +23,32 @@ blocks is only allowed in an `unsafe` context.
2323
2424The 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.
30-
3126## Functions
3227
3328Functions within external blocks are declared in the same way as other Rust
3429functions, with the exception that they must not have a body and are instead
3530terminated 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.
31+ [ IDENTIFIER] or ` _ ` may be used. The ` safe ` and ` unsafe ` function qualifiers are
32+ allowed, but other function qualifiers (e.g. ` const ` , ` async ` , ` extern ` ) are
33+ not.
3834
3935Functions within external blocks may be called by Rust code, just like
4036functions defined in Rust. The Rust compiler automatically translates between
4137the Rust ABI and the foreign ABI.
4238
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.
39+ A function declared in an extern block is implicitly ` unsafe ` unless the ` safe `
40+ function qualifier is present.
41+
42+ When coerced to a function pointer, a function declared in an extern block has
43+ type ` extern "abi" for<'l1, ..., 'lm> fn(A1, ..., An) -> R ` , where ` 'l1 ` ,
44+ ... ` 'lm ` are its lifetime parameters, ` A1 ` , ..., ` An ` are the declared types of
45+ its parameters, ` R ` is the declared return type.
4846
4947## Statics
5048
5149Statics within external blocks are declared in the same way as [ statics] outside of external blocks,
5250except 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
51+ Unless a static item declared in an extern block is qualified as ` safe ` , it is ` unsafe ` to access that item , whether or
5452not it's mutable, because there is nothing guaranteeing that the bit pattern at the static's
5553memory is valid for the type it is declared with, since some arbitrary (e.g. C) code is in charge
5654of initializing the static.
@@ -69,34 +67,34 @@ standard C ABI on the specific platform. Other ABIs may be specified using an
6967
7068``` rust
7169// Interface to the Windows API
72- extern " stdcall" { }
70+ unsafe extern " stdcall" { }
7371```
7472
7573There are three ABI strings which are cross-platform, and which all compilers
7674are guaranteed to support:
7775
78- * ` extern "Rust" ` -- The default ABI when you write a normal ` fn foo() ` in any
76+ * ` unsafe extern "Rust"` -- The default ABI when you write a normal ` fn foo() ` in any
7977 Rust code.
80- * ` extern "C" ` -- This is the same as ` extern fn foo() ` ; whatever the default
78+ * ` unsafe extern "C"` -- This is the same as ` extern fn foo() ` ; whatever the default
8179 your C compiler supports.
82- * ` extern "system" ` -- Usually the same as ` extern "C" ` , except on Win32, in
80+ * ` unsafe extern "system"` -- Usually the same as ` extern "C" ` , except on Win32, in
8381 which case it's ` "stdcall" ` , or what you should use to link to the Windows
8482 API itself
8583
8684There are also some platform-specific ABI strings:
8785
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
86+ * ` unsafe extern "cdecl"` -- The default for x86\_ 32 C code.
87+ * ` unsafe extern "stdcall"` -- The default for the Win32 API on x86\_ 32.
88+ * ` unsafe extern "win64"` -- The default for C code on x86\_ 64 Windows.
89+ * ` unsafe extern "sysv64"` -- The default for C code on non-Windows x86\_ 64.
90+ * ` unsafe extern "aapcs"` -- The default for ARM.
91+ * ` unsafe extern "fastcall"` -- The ` fastcall ` ABI -- corresponds to MSVC's
9492 ` __fastcall ` and GCC and clang's ` __attribute__((fastcall)) `
95- * ` extern "vectorcall" ` -- The ` vectorcall ` ABI -- corresponds to MSVC's
93+ * ` unsafe extern "vectorcall"` -- The ` vectorcall ` ABI -- corresponds to MSVC's
9694 ` __vectorcall ` and clang's ` __attribute__((vectorcall)) `
97- * ` extern "thiscall" ` -- The default for C++ member functions on MSVC -- corresponds to MSVC's
95+ * ` unsafe extern "thiscall"` -- The default for C++ member functions on MSVC -- corresponds to MSVC's
9896 ` __thiscall ` and GCC and clang's ` __attribute__((thiscall)) `
99- * ` extern "efiapi" ` -- The ABI used for [ UEFI] functions.
97+ * ` unsafe extern "efiapi"` -- The ABI used for [ UEFI] functions.
10098
10199## Variadic functions
102100
@@ -105,10 +103,10 @@ last argument. The variadic parameter may optionally be specified with an
105103identifier.
106104
107105``` rust
108- extern " C" {
109- fn foo (... );
110- fn bar (x : i32 , ... );
111- fn with_name (format : * const u8 , args : ... );
106+ unsafe extern " C" {
107+ safe fn foo (... );
108+ unsafe fn bar (x : i32 , ... );
109+ unsafe fn with_name (format : * const u8 , args : ... );
112110}
113111```
114112
@@ -152,17 +150,17 @@ not specified.
152150<!-- ignore: requires extern linking -->
153151``` rust,ignore
154152#[link(name = "crypto")]
155- extern {
153+ unsafe extern {
156154 // …
157155}
158156
159157#[link(name = "CoreFoundation", kind = "framework")]
160- extern {
158+ unsafe extern {
161159 // …
162160}
163161
164162#[link(wasm_import_module = "foo")]
165- extern {
163+ unsafe extern {
166164 // …
167165}
168166```
@@ -277,9 +275,9 @@ block to indicate the symbol to import for the given function or static. It
277275uses the [ _ MetaNameValueStr_ ] syntax to specify the name of the symbol.
278276
279277``` rust
280- extern {
278+ unsafe extern {
281279 #[link_name = " actual_symbol_name" ]
282- fn name_in_rust ();
280+ safe fn name_in_rust ();
283281}
284282```
285283
@@ -306,9 +304,9 @@ it, and that assigned ordinal may change between builds of the binary.
306304<!-- ignore: Only works on x86 Windows -->
307305``` rust,ignore
308306#[link(name = "exporter", kind = "raw-dylib")]
309- extern "stdcall" {
307+ unsafe extern "stdcall" {
310308 #[link_ordinal(15)]
311- fn imported_function_stdcall(i: i32);
309+ safe fn imported_function_stdcall(i: i32);
312310}
313311```
314312
0 commit comments