@@ -69,19 +69,45 @@ 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-restrictions]
90
+ Because of this, on many platforms the following restrictions apply:
91
+
92
+ - ` #[target_feature] ` functions (and closures that inherit the attribute)
93
+ can only be safely called within caller that enable all the ` target_feature ` s
94
+ that the callee enables.
95
+ - ` #[target_feature] ` functions (and closures that inherit the attribute)
96
+ can only be coerced to * safe* ` fn ` pointers in contexts that enable all the
97
+ ` target_feature ` s that the coercee enables.
98
+ - ` #[target_feature] ` functions * never* implement ` Fn ` traits, although
99
+ closures inheriting features from the enclosing function do.
100
+
101
+ Moreover, since Rust needs to be able to check the usage of ` #[target_feature] `
102
+ functions at callsites to ensure safety, safe functions for which this check
103
+ would not be possible cannot be annotated with this attribute. This includes:
104
+
105
+ - ` main `
106
+ - other special functions that allow safe functions such as ` #[panic_handler] `
107
+ - safe trait methods
108
+ - safe default functions in traits
109
+
110
+
85
111
r[ attributes.codegen.target_feature.inline]
86
112
Functions marked with ` target_feature ` are not inlined into a context that
87
113
does not support the given features. The ` #[inline(always)] ` attribute may not
@@ -98,8 +124,8 @@ r[attributes.codegen.target_feature.x86]
98
124
99
125
100
126
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 ] .
127
+ Hence on this platform usage of ` #[target_feature] ` functions follows the
128
+ [ above restrictions ] [ attributes.codegen.target_feature.safety-restrictions ] .
103
129
104
130
Feature | Implicitly Enables | Description
105
131
------------|--------------------|-------------------
@@ -166,8 +192,8 @@ r[attributes.codegen.target_feature.aarch64]
166
192
#### ` aarch64 `
167
193
168
194
169
- This platform requires that ` #[target_feature] ` is only applied to [ ` unsafe `
170
- functions ] [ unsafe function ] .
195
+ On this platform the usage of ` #[target_feature] ` functions follows the
196
+ [ above restrictions ] [ attributes.codegen.target_feature.safety-restrictions ] .
171
197
172
198
Further documentation on these features can be found in the [ ARM Architecture
173
199
Reference Manual] , or elsewhere on [ developer.arm.com] .
@@ -231,8 +257,8 @@ r[attributes.codegen.target_feature.riscv]
231
257
#### ` riscv32 ` or ` riscv64 `
232
258
233
259
234
- This platform requires that ` #[target_feature] ` is only applied to [ ` unsafe `
235
- functions ] [ unsafe function ] .
260
+ On this platform the usage of ` #[target_feature] ` functions follows the
261
+ [ above restrictions ] [ attributes.codegen.target_feature.safety-restrictions ] .
236
262
237
263
Further documentation on these features can be found in their respective
238
264
specification. Many specifications are described in the [ RISC-V ISA Manual] or
@@ -293,12 +319,11 @@ r[attributes.codegen.target_feature.wasm]
293
319
#### ` wasm32 ` or ` wasm64 `
294
320
295
321
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.
322
+ Safe ` #[target_feature] ` functions may always be used in safe contexts on Wasm
323
+ platforms. It is impossible to cause undefined behavior via the
324
+ ` #[target_feature] ` attribute because attempting to use instructions
325
+ unsupported by the Wasm engine will fail at load time without the risk of being
326
+ interpreted in a way different from what the compiler expected.
302
327
303
328
Feature | Implicitly Enables | Description
304
329
----------------------|---------------------|-------------------
0 commit comments