-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Experimentally add ffi_const
and ffi_pure
extern fn attributes
#58327
Closed
Closed
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d8344ff
Add ffi_const and ffi_pure extern fn attributes
gnzlbg bf149c0
Use the term foreign function in errors
gnzlbg 945cbe4
Use pattern to match attributes
gnzlbg 736c437
Fix attribute check
gnzlbg 3adeddd
Fix tidy
gnzlbg effaf14
Update doc of `ffi_const`
Centril 26eb8b0
Document in unstable book; check that const functions are not pure, r…
gnzlbg c11a27f
Fix tests
gnzlbg d663711
Tidy book
gnzlbg a3d0f3c
Fix typos
gnzlbg 3770725
Do not emit incorrect IR on error
gnzlbg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
src/doc/unstable-book/src/language-features/c_ffi_const.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# `c_ffi_const` | ||
|
||
The `#[c_ffi_const]` attribute applies clang's `const` attribute to foreign | ||
functions declarations. | ||
|
||
That is, `#[c_ffi_const]` functions shall have no effects except for its return | ||
value, which can only depend on the values of the function parameters, and is | ||
not affected by changes to the observable state of the program. | ||
|
||
The behavior of calling a `#[c_ffi_const]` function that violates these | ||
requirements is undefined. | ||
|
||
This attribute enables Rust to perform common optimizations, like sub-expression | ||
elimination, and it can avoid emitting some calls in repeated invocations of the | ||
function with the same argument values regardless of other operations being | ||
performed in between these functions calls (as opposed to `#[c_ffi_pure]` | ||
functions). | ||
|
||
## Pitfals | ||
|
||
A `#[c_ffi_const]` function can only read global memory that would not affect | ||
its return value for the whole execution of the program (e.g. immutable global | ||
memory). `#[c_ffi_const]` functions are referentially-transparent and therefore | ||
more strict than `#[c_ffi_pure]` functions. | ||
|
||
A common pitfall involves applying the `#[c_ffi_const]` attribute to a | ||
function that reads memory through pointer arguments which do not necessarily | ||
point to immutable global memory. | ||
|
||
A `#[c_ffi_const]` function that returns unit has no effect on the abstract | ||
machine's state, and a `#[c_ffi_const]` function cannot be `#[c_ffi_pure]`. | ||
|
||
A diverging and C or C++ `const` function is unreachable. Diverging via a | ||
side-effect (e.g. a call to `abort`) violates `const` pre-conditions. Divergence | ||
without side-effects is undefined behavior in C++ and not possible in C. In C++, | ||
the behavior of infinite loops without side-effects is undefined, while in C | ||
these loops can be assumed to terminate. This would cause a diverging function | ||
to return, invoking undefined behavior. | ||
|
||
When translating C headers to Rust FFI, it is worth verifying for which targets | ||
the `const` attribute is enabled in those headers, and using the appropriate | ||
`cfg` macros in the Rust side to match those definitions. While the semantics of | ||
`const` are implemented identically by many C and C++ compilers, e.g., clang, | ||
[GCC], [ARM C/C++ compiler], [IBM ILE C/C++], etc. they are not necessarily | ||
implemented in this way on all of them. It is therefore also worth verifying | ||
that the semantics of the C toolchain used to compile the binary being linked | ||
against are compatible with those of the `#[c_ffi_const]`. | ||
|
||
[ARM C/C++ compiler]: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0491c/Cacgigch.html | ||
[GCC]: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-const-function-attribute | ||
[IBM ILE C/C++]: https://www.ibm.com/support/knowledgecenter/fr/ssw_ibm_i_71/rzarg/fn_attrib_const.htm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,55 @@ | ||||||
# `c_ffi_pure` | ||||||
|
||||||
The `#[c_ffi_pure]` attribute applies clang's `pure` attribute to foreign | ||||||
functions declarations. | ||||||
|
||||||
That is, `#[c_ffi_pure]` functions shall have no effects except for its return | ||||||
value, which shall not change across two consecutive function calls and can only | ||||||
depend on the values of the function parameters and/or global memory. | ||||||
|
||||||
The behavior of calling a `#[c_ffi_pure]` function that violates these | ||||||
requirements is undefined. | ||||||
|
||||||
This attribute enables Rust to perform common optimizations, like sub-expression | ||||||
elimination and loop optimizations. Some common examples of pure functions are | ||||||
`strlen` or `memcmp`. | ||||||
|
||||||
These optimizations only apply across successive invocations of the function, | ||||||
since any other function could modify global memory read by `#[c_ffi_pure]` | ||||||
functions, altering their result. The `#[c_ffi_const]` attribute allows | ||||||
sub-expression elimination regardless of any operations in between the function | ||||||
calls. | ||||||
|
||||||
## Pitfals | ||||||
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.
Suggested change
|
||||||
|
||||||
A `#[c_ffi_pure]` function can read global memory through the function | ||||||
parameters (e.g. pointers), globals, etc. `#[c_ffi_pure]` functions are not | ||||||
referentially-transparent, and are therefore more relaxed than `#[c_ffi_const]` | ||||||
functions. | ||||||
|
||||||
However, accesing global memory through volatile or atomic reads can violate the | ||||||
requirement that two consecutive function calls shall return the same value. | ||||||
|
||||||
A `pure` function that returns unit has no effect on the abstract machine's | ||||||
state. | ||||||
|
||||||
A diverging and `pure` C or C++ function is unreachable. Diverging via a | ||||||
side-effect (e.g. a call to `abort`) violates `pure` requirements. Divergence | ||||||
without side-effects is undefined behavior in C++ and not possible in C. In C++, | ||||||
the behavior of infinite loops without side-effects is undefined, while in C | ||||||
these loops can be assumed to terminate. This would cause a diverging function | ||||||
to return, invoking undefined behavior. | ||||||
|
||||||
When translating C headers to Rust FFI, it is worth verifying for which targets | ||||||
the `pure` attribute is enabled in those headers, and using the appropriate | ||||||
`cfg` macros in the Rust side to match those definitions. While the semantics of | ||||||
`pure` are implemented identically by many C and C++ compilers, e.g., clang, | ||||||
[GCC], [ARM C/C++ compiler], [IBM ILE C/C++], etc. they are not necessarily | ||||||
implemented in this way on all of them. It is therefore also worth verifying | ||||||
that the semantics of the C toolchain used to compile the binary being linked | ||||||
against are compatible with those of the `#[c_ffi_pure]`. | ||||||
|
||||||
|
||||||
[ARM C/C++ compiler]: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0491c/Cacigdac.html | ||||||
[GCC]: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-pure-function-attribute | ||||||
[IBM ILE C/C++]: https://www.ibm.com/support/knowledgecenter/fr/ssw_ibm_i_71/rzarg/fn_attrib_pure.htm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// compile-flags: -C no-prepopulate-passes | ||
#![crate_type = "lib"] | ||
#![feature(c_ffi_const)] | ||
|
||
pub fn bar() { unsafe { foo() } } | ||
|
||
extern { | ||
// CHECK-LABEL: declare void @foo() | ||
// CHECK-SAME: [[ATTRS:#[0-9]+]] | ||
// CHECK-DAG: attributes [[ATTRS]] = { {{.*}}readnone{{.*}} } | ||
#[c_ffi_const] pub fn foo(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// compile-flags: -C no-prepopulate-passes | ||
#![crate_type = "lib"] | ||
#![feature(c_ffi_pure)] | ||
|
||
pub fn bar() { unsafe { foo() } } | ||
|
||
extern { | ||
// CHECK-LABEL: declare void @foo() | ||
// CHECK-SAME: [[ATTRS:#[0-9]+]] | ||
// CHECK-DAG: attributes [[ATTRS]] = { {{.*}}readonly{{.*}} } | ||
#[c_ffi_pure] pub fn foo(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// ignore-tidy-linelength | ||
#![feature(c_ffi_const, c_ffi_pure)] | ||
#![crate_type = "lib"] | ||
|
||
#[c_ffi_const] //~ ERROR `#[c_ffi_const]` may only be used on foreign functions [E0725] | ||
pub fn foo() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
error[E0725]: `#[c_ffi_const]` may only be used on foreign functions | ||
--> $DIR/c_ffi_const.rs:5:1 | ||
| | ||
LL | #[c_ffi_const] //~ ERROR `#[c_ffi_const]` may only be used on foreign functions [E0725] | ||
| ^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0725`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// ignore-tidy-linelength | ||
#![feature(c_ffi_const, c_ffi_pure)] | ||
|
||
extern { | ||
#[c_ffi_pure] //~ ERROR `#[c_ffi_const]` function cannot be`#[c_ffi_pure]` [E0726] | ||
#[c_ffi_const] | ||
pub fn baz(); | ||
} | ||
|
||
fn main() { | ||
unsafe { baz() }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
error[E0726]: `#[c_ffi_const]` function cannot be`#[c_ffi_pure]` | ||
--> $DIR/c_ffi_const2.rs:5:5 | ||
| | ||
LL | #[c_ffi_pure] //~ ERROR `#[c_ffi_const]` function cannot be`#[c_ffi_pure]` [E0726] | ||
| ^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0726`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// ignore-tidy-linelength | ||
#![feature(c_ffi_pure)] | ||
#![crate_type = "lib"] | ||
|
||
#[c_ffi_pure] //~ ERROR `#[c_ffi_pure]` may only be used on foreign functions [E0724] | ||
pub fn foo() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
error[E0724]: `#[c_ffi_pure]` may only be used on foreign functions | ||
--> $DIR/c_ffi_pure.rs:5:1 | ||
| | ||
LL | #[c_ffi_pure] //~ ERROR `#[c_ffi_pure]` may only be used on foreign functions [E0724] | ||
| ^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0724`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// ignore-tidy-linelength | ||
#![crate_type = "lib"] | ||
|
||
extern { | ||
#[c_ffi_const] //~ ERROR the `#[c_ffi_const]` attribute is an experimental feature (see issue #58328) | ||
pub fn foo(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
error[E0658]: the `#[c_ffi_const]` attribute is an experimental feature (see issue #58328) | ||
--> $DIR/feature-gate-c_ffi_const.rs:5:5 | ||
| | ||
LL | #[c_ffi_const] //~ ERROR the `#[c_ffi_const]` attribute is an experimental feature (see issue #58328) | ||
| ^^^^^^^^^^^^^^ | ||
| | ||
= help: add #![feature(c_ffi_const)] to the crate attributes to enable | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0658`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// ignore-tidy-linelength | ||
#![crate_type = "lib"] | ||
|
||
extern { | ||
#[c_ffi_pure] //~ ERROR the `#[c_ffi_pure]` attribute is an experimental feature (see issue #58329) | ||
pub fn foo(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
error[E0658]: the `#[c_ffi_pure]` attribute is an experimental feature (see issue #58329) | ||
--> $DIR/feature-gate-c_ffi_pure.rs:5:5 | ||
| | ||
LL | #[c_ffi_pure] //~ ERROR the `#[c_ffi_pure]` attribute is an experimental feature (see issue #58329) | ||
| ^^^^^^^^^^^^^ | ||
| | ||
= help: add #![feature(c_ffi_pure)] to the crate attributes to enable | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0658`. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.