-
Notifications
You must be signed in to change notification settings - Fork 533
Correct errors in the reference of extern functions definitions and declarations #652
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
Changes from 2 commits
8ce3484
a3b91c0
ea9d02e
5aac879
7ebfe25
3d0237b
1f023cc
ace3935
30407d2
8cee694
bca3f6c
dc93dc4
9c93133
27e30fe
c4ef0ee
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 |
---|---|---|
|
@@ -109,33 +109,72 @@ sufficient context to determine the type parameters. For example, | |
|
||
## Extern functions | ||
|
||
Extern functions are part of Rust's foreign function interface, providing the | ||
opposite functionality to [external blocks]. Whereas external | ||
blocks allow Rust code to call foreign code, extern functions with bodies | ||
defined in Rust code _can be called by foreign code_. They are defined in the | ||
same way as any other Rust function, except that they have the `extern` | ||
qualifier. | ||
Extern function _definitions_ allow defining functions that can be called | ||
with a particular ABI: | ||
|
||
```rust,no_run | ||
extern "ABI" fn foo() { ... } | ||
``` | ||
|
||
An extern function _declaration_ via an [external block] can be used to | ||
provide an item for these functions that can be called by Rust code without | ||
providing their definition. | ||
|
||
When `"extern" Abi?*` is omitted from `FunctionQualifiers`, the ABI `"Rust"` is | ||
assigned. For example: | ||
|
||
```rust | ||
// Declares an extern fn, the ABI defaults to "C" | ||
extern fn new_i32() -> i32 { 0 } | ||
fn foo() {} | ||
``` | ||
|
||
is equivalent to: | ||
|
||
```rust | ||
extern "Rust" fn foo() {} | ||
``` | ||
|
||
// Declares an extern fn with "stdcall" ABI | ||
Functions in Rust can be called by foreign code, and using an ABI that | ||
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 is a run-on sentence, or at least feels like one. Also, "Rust's functions". And what does "foreign code" here mean differently than code from other programming languages? 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. Rust code exposed with a C ABI. 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. I'm not sure how to improve this. |
||
differs from Rust allows, for example, to provide functions that can be | ||
called from other programming languages like C: | ||
|
||
```rust | ||
// Declares a function with the "C" ABI | ||
extern "C" fn new_i32() -> i32 { 0 } | ||
|
||
// Declares a function with the "stdcall" ABI | ||
# #[cfg(target_arch = "x86_64")] | ||
extern "stdcall" fn new_i32_stdcall() -> i32 { 0 } | ||
``` | ||
|
||
Unlike normal functions, extern fns have type `extern "ABI" fn()`. This is the | ||
same type as the functions declared in an extern block. | ||
Just as with [external block], when the `extern` keyword is used and the `"ABI` | ||
is omitted, the ABI used defaults to `"C"`. That is, this: | ||
|
||
```rust | ||
extern fn new_i32() -> i32 { 0 } | ||
let fptr: extern fn() -> i32 = new_i32; | ||
``` | ||
|
||
is equivalent to: | ||
|
||
```rust | ||
# extern fn new_i32() -> i32 { 0 } | ||
extern "C" fn new_i32() -> i32 { 0 } | ||
let fptr: extern "C" fn() -> i32 = new_i32; | ||
``` | ||
|
||
As non-Rust calling conventions do not support unwinding, unwinding past the end | ||
of an extern function will cause the process to abort. In LLVM, this is | ||
implemented by executing an illegal instruction. | ||
Functions with an ABI that differs from `"Rust"` do not support unwinding in the | ||
exact same way that Rust does. Therefore, unwinding past the end of functions | ||
with such ABIs causes the process to abort. | ||
|
||
**Non-normative note**: The LLVM backend of the current Rust implementation | ||
gnzlbg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
aborts the process by executing an illegal instruction. | ||
|
||
**Non-normative note**: There are other ABIs available in unstable Rust that are | ||
equivalent to the `"Rust"` ABI, e.g., | ||
gnzlbg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[`"rust-intrinsic"`](https://doc.rust-lang.org/unstable-book/language-features/intrinsics.html?highlight=rust-intrin#intrinsics) | ||
or | ||
[`"platform-intrinsic"`](https://doc.rust-lang.org/unstable-book/language-features/platform-intrinsics.html). | ||
Refer to the [Unstable Book](https://doc.rust-lang.org/unstable-book) for more | ||
information about these. | ||
|
||
## Const functions | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.