Skip to content

Commit 8c20f77

Browse files
authored
Merge pull request #1720 from veluca93/tf11
Update reference for target_feature_11.
2 parents 6195dbd + a28ead0 commit 8c20f77

File tree

3 files changed

+72
-15
lines changed

3 files changed

+72
-15
lines changed

Diff for: src/attributes/codegen.md

+66-14
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,73 @@ features. It uses the [_MetaListNameValueStr_] syntax with a single key of
6969
```rust
7070
# #[cfg(target_feature = "avx2")]
7171
#[target_feature(enable = "avx2")]
72-
unsafe fn foo_avx2() {}
72+
fn foo_avx2() {}
7373
```
7474

7575
r[attributes.codegen.target_feature.arch]
7676
Each [target architecture] has a set of features that may be enabled. It is an
7777
error to specify a feature for a target architecture that the crate is not
7878
being compiled for.
7979

80+
r[attributes.codegen.target_feature.closures]
81+
Closures defined within a `target_feature`-annotated function inherit the
82+
attribute from the enclosing function.
83+
8084
r[attributes.codegen.target_feature.target-ub]
8185
It is [undefined behavior] to call a function that is compiled with a feature
8286
that is not supported on the current platform the code is running on, *except*
8387
if the platform explicitly documents this to be safe.
8488

89+
r[attributes.codegen.target_feature.safety-restrictions]
90+
The following restrictions apply unless otherwise specified by the platform rules below:
91+
92+
- Safe `#[target_feature]` functions (and closures that inherit the attribute) can only be safely called within a caller that enables all the `target_feature`s that the callee enables.
93+
This restriction does not apply in an `unsafe` context.
94+
- Safe `#[target_feature]` functions (and closures that inherit the attribute) can only be coerced to *safe* function pointers in contexts that enable all the `target_feature`s that the coercee enables.
95+
This restriction does not apply to `unsafe` function pointers.
96+
97+
Implicitly enabled features are included in this rule. For example an `sse2` function can call ones marked with `sse`.
98+
99+
```rust
100+
# #[cfg(target_feature = "avx2")] {
101+
#[target_feature(enable = "avx")]
102+
fn foo_avx() {}
103+
104+
fn bar() {
105+
// Calling `foo_avx` here is unsafe, as we must ensure that AVX is
106+
// available first, even if `avx` is enabled by default on the target
107+
// platform or manually enabled as compiler flags.
108+
unsafe {
109+
foo_avx();
110+
}
111+
}
112+
113+
#[target_feature(enable = "avx")]
114+
fn bar_avx() {
115+
// Calling `foo_avx` here is safe.
116+
foo_avx();
117+
|| foo_avx();
118+
}
119+
120+
#[target_feature(enable = "avx2")]
121+
fn bar_avx2() {
122+
// Calling `foo_avx` here is safe because `avx2` implies `avx`.
123+
foo_avx();
124+
}
125+
# }
126+
```
127+
128+
r[attributes.codegen.target_feature.fn-traits]
129+
A function with a `#[target_feature]` attribute *never* implements the `Fn` family of traits, although closures inheriting features from the enclosing function do.
130+
131+
r[attributes.codegen.target_feature.allowed-positions]
132+
The `#[target_feature]` attribute is not allowed on the following places:
133+
134+
- [the `main` function][crate.main]
135+
- a [`panic_handler` function][runtime.panic_handler]
136+
- safe trait methods
137+
- safe default functions in traits
138+
85139
r[attributes.codegen.target_feature.inline]
86140
Functions marked with `target_feature` are not inlined into a context that
87141
does not support the given features. The `#[inline(always)]` attribute may not
@@ -98,8 +152,8 @@ r[attributes.codegen.target_feature.x86]
98152

99153

100154
Executing code with unsupported features is undefined behavior on this platform.
101-
Hence this platform requires that `#[target_feature]` is only applied to [`unsafe`
102-
functions][unsafe function].
155+
Hence on this platform usage of `#[target_feature]` functions follows the
156+
[above restrictions][attributes.codegen.target_feature.safety-restrictions].
103157

104158
Feature | Implicitly Enables | Description
105159
------------|--------------------|-------------------
@@ -166,8 +220,8 @@ r[attributes.codegen.target_feature.aarch64]
166220
#### `aarch64`
167221

168222

169-
This platform requires that `#[target_feature]` is only applied to [`unsafe`
170-
functions][unsafe function].
223+
On this platform the usage of `#[target_feature]` functions follows the
224+
[above restrictions][attributes.codegen.target_feature.safety-restrictions].
171225

172226
Further documentation on these features can be found in the [ARM Architecture
173227
Reference Manual], or elsewhere on [developer.arm.com].
@@ -231,8 +285,8 @@ r[attributes.codegen.target_feature.riscv]
231285
#### `riscv32` or `riscv64`
232286

233287

234-
This platform requires that `#[target_feature]` is only applied to [`unsafe`
235-
functions][unsafe function].
288+
On this platform the usage of `#[target_feature]` functions follows the
289+
[above restrictions][attributes.codegen.target_feature.safety-restrictions].
236290

237291
Further documentation on these features can be found in their respective
238292
specification. Many specifications are described in the [RISC-V ISA Manual] or
@@ -293,12 +347,11 @@ r[attributes.codegen.target_feature.wasm]
293347
#### `wasm32` or `wasm64`
294348

295349

296-
`#[target_feature]` may be used with both safe and
297-
[`unsafe` functions][unsafe function] on Wasm platforms. It is impossible to
298-
cause undefined behavior via the `#[target_feature]` attribute because
299-
attempting to use instructions unsupported by the Wasm engine will fail at load
300-
time without the risk of being interpreted in a way different from what the
301-
compiler expected.
350+
Safe `#[target_feature]` functions may always be used in safe contexts on Wasm
351+
platforms. It is impossible to cause undefined behavior via the
352+
`#[target_feature]` attribute because attempting to use instructions
353+
unsupported by the Wasm engine will fail at load time without the risk of being
354+
interpreted in a way different from what the compiler expected.
302355

303356
Feature | Implicitly Enables | Description
304357
----------------------|---------------------|-------------------
@@ -470,7 +523,6 @@ trait object whose methods are attributed.
470523
[target architecture]: ../conditional-compilation.md#target_arch
471524
[trait]: ../items/traits.md
472525
[undefined behavior]: ../behavior-considered-undefined.md
473-
[unsafe function]: ../unsafe-keyword.md
474526
[rust-abi]: ../items/external-blocks.md#abi
475527
[`Location`]: core::panic::Location
476528

Diff for: src/unsafe-keyword.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ r[unsafe.block]
2727
## Unsafe blocks (`unsafe {}`)
2828

2929
r[unsafe.block.intro]
30-
A block of code can be prefixed with the `unsafe` keyword, to permit calling `unsafe` functions or dereferencing raw pointers.
30+
A block of code can be prefixed with the `unsafe` keyword to permit using the unsafe actions as defined in the [Unsafety] chapter, such as calling other unsafe functions or dereferencing raw pointers.
3131

3232
r[unsafe.block.fn-body]
3333
By default, the body of an unsafe function is also considered to be an unsafe block;
@@ -48,6 +48,8 @@ For example, a doubly-linked list is not a tree structure and can only be repres
4848
By using `unsafe` blocks to represent the reverse links as raw pointers, it can be implemented without reference counting.
4949
(See ["Learn Rust With Entirely Too Many Linked Lists"](https://rust-unofficial.github.io/too-many-lists/) for a more in-depth exploration of this particular example.)
5050

51+
[Unsafety]: unsafety.md
52+
5153
r[unsafe.trait]
5254
## Unsafe traits (`unsafe trait`)
5355

Diff for: src/unsafety.md

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ r[safety.unsafe-union-access]
2121
r[safety.unsafe-call]
2222
- Calling an unsafe function (including an intrinsic or foreign function).
2323

24+
r[safety.unsafe-target-feature-call]
25+
- Calling a safe function marked with a [`target_feature`][attributes.codegen.target_feature] from a function that does not have a `target_feature` attribute enabling the same features (see [attributes.codegen.target_feature.safety-restrictions]).
26+
2427
r[safety.unsafe-impl]
2528
- Implementing an [unsafe trait].
2629

0 commit comments

Comments
 (0)