Skip to content

Commit 0f229ab

Browse files
committed
Update reference for target_feature_11.
1 parent 6cb7fc4 commit 0f229ab

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

src/attributes/codegen.md

+32-13
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,42 @@ 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]
90+
Because of this, on many platforms safe functions and closures marked with
91+
`target_feature` do not implement Fn traits, cannot be coerced to safe `fn`
92+
pointers (but can be coerced to `unsafe fn` pointers) and require `unsafe`
93+
to be called. These restrictions are lifted in contexts such that the set of
94+
features enabled in the callee is a subset of the set of features enabled in
95+
the caller.
96+
97+
r[attributes.codegen.target_feature.restrictions]
98+
Note that, since Rust needs to be able to check the usage of `#[target_feature]`
99+
functions at callsites to ensure safety, safe functions for which this check
100+
would not be possible cannot be annotated with this attribute. This includes:
101+
102+
- `main`
103+
- other special functions that allow safe functions such as `#[panic_handler]`
104+
- safe trait methods
105+
- safe default functions in traits
106+
107+
85108
r[attributes.codegen.target_feature.inline]
86109
Functions marked with `target_feature` are not inlined into a context that
87110
does not support the given features. The `#[inline(always)]` attribute may not
@@ -98,8 +121,7 @@ r[attributes.codegen.target_feature.x86]
98121

99122

100123
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].
124+
Hence on this platform usage of `#[target_feature]` functions is usually unsafe.
103125

104126
Feature | Implicitly Enables | Description
105127
------------|--------------------|-------------------
@@ -166,8 +188,7 @@ r[attributes.codegen.target_feature.aarch64]
166188
#### `aarch64`
167189

168190

169-
This platform requires that `#[target_feature]` is only applied to [`unsafe`
170-
functions][unsafe function].
191+
On this platform usage of `#[target_feature]` functions is usually unsafe.
171192

172193
Further documentation on these features can be found in the [ARM Architecture
173194
Reference Manual], or elsewhere on [developer.arm.com].
@@ -231,8 +252,7 @@ r[attributes.codegen.target_feature.riscv]
231252
#### `riscv32` or `riscv64`
232253

233254

234-
This platform requires that `#[target_feature]` is only applied to [`unsafe`
235-
functions][unsafe function].
255+
On this platform usage of `#[target_feature]` functions is usually unsafe.
236256

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

295315

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.
316+
Safe `#[target_feature]` functions may always be used in safe contexts on Wasm
317+
platforms. It is impossible to cause undefined behavior via the
318+
`#[target_feature]` attribute because attempting to use instructions
319+
unsupported by the Wasm engine will fail at load time without the risk of being
320+
interpreted in a way different from what the compiler expected.
302321

303322
Feature | Implicitly Enables | Description
304323
----------------------|---------------------|-------------------

0 commit comments

Comments
 (0)