@@ -69,19 +69,42 @@ features. It uses the [_MetaListNameValueStr_] syntax with a single key of
69
69
``` rust
70
70
# #[cfg(target_feature = " avx2" )]
71
71
#[target_feature(enable = " avx2" )]
72
- unsafe fn foo_avx2 () {}
72
+ fn foo_avx2 () {}
73
73
```
74
74
75
75
r[ attributes.codegen.target_feature.arch]
76
76
Each [ target architecture] has a set of features that may be enabled. It is an
77
77
error to specify a feature for a target architecture that the crate is not
78
78
being compiled for.
79
79
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
+
80
84
r[ attributes.codegen.target_feature.target-ub]
81
85
It is [ undefined behavior] to call a function that is compiled with a feature
82
86
that is not supported on the current platform the code is running on, * except*
83
87
if the platform explicitly documents this to be safe.
84
88
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
+
85
108
r[ attributes.codegen.target_feature.inline]
86
109
Functions marked with ` target_feature ` are not inlined into a context that
87
110
does not support the given features. The ` #[inline(always)] ` attribute may not
@@ -98,8 +121,7 @@ r[attributes.codegen.target_feature.x86]
98
121
99
122
100
123
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.
103
125
104
126
Feature | Implicitly Enables | Description
105
127
------------|--------------------|-------------------
@@ -166,8 +188,7 @@ r[attributes.codegen.target_feature.aarch64]
166
188
#### ` aarch64 `
167
189
168
190
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.
171
192
172
193
Further documentation on these features can be found in the [ ARM Architecture
173
194
Reference Manual] , or elsewhere on [ developer.arm.com] .
@@ -231,8 +252,7 @@ r[attributes.codegen.target_feature.riscv]
231
252
#### ` riscv32 ` or ` riscv64 `
232
253
233
254
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.
236
256
237
257
Further documentation on these features can be found in their respective
238
258
specification. Many specifications are described in the [ RISC-V ISA Manual] or
@@ -293,12 +313,11 @@ r[attributes.codegen.target_feature.wasm]
293
313
#### ` wasm32 ` or ` wasm64 `
294
314
295
315
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.
302
321
303
322
Feature | Implicitly Enables | Description
304
323
----------------------|---------------------|-------------------
0 commit comments