@@ -53,9 +53,8 @@ features. It uses the [_MetaListNameValueStr_] syntax with a single key of
53
53
` enable ` whose value is a string of comma-separated feature names to enable.
54
54
55
55
``` rust
56
- # #[cfg(target_feature = " avx2" )]
57
56
#[target_feature(enable = " avx2" )]
58
- unsafe fn foo_avx2 () {}
57
+ fn foo_avx2 () {}
59
58
```
60
59
61
60
Each [ target architecture] has a set of features that may be enabled. It is an
@@ -66,20 +65,39 @@ It is [undefined behavior] to call a function that is compiled with a feature
66
65
that is not supported on the current platform the code is running on, * except*
67
66
if the platform explicitly documents this to be safe.
68
67
69
- Functions marked with ` target_feature ` are not inlined into a context that
70
- does not support the given features. The ` #[inline(always)] ` attribute may not
71
- be used with a ` target_feature ` attribute.
68
+ For this reason, a function marked with ` target_feature ` is unsafe, except in
69
+ a context that supports the given features. For example:
70
+
71
+ ``` rust
72
+ fn bar () {
73
+ // Calling `foo_avx2` here is unsafe, as we must ensure
74
+ // that AVX is available first.
75
+ unsafe {
76
+ foo_avx2 ();
77
+ }
78
+ }
79
+
80
+ #[target_feature(enable = " avx2" )]
81
+ fn bar_avx2 () {
82
+ // Calling `foo_avx2` here is safe.
83
+ foo_avx2 ();
84
+ || foo_avx2 ();
85
+ }
86
+ ```
87
+
88
+ Like unsafe functions, functions marked with ` target_feature ` cannot be
89
+ assigned to a safe function pointer and do not implement ` FnOnce ` .
90
+
91
+ Functions marked with ` target_feature ` are not inlined into a context unless
92
+ it supports the given features. The ` #[inline(always)] ` attribute may not
93
+ be used with ` target_feature ` .
72
94
73
95
### Available features
74
96
75
97
The following is a list of the available feature names.
76
98
77
99
#### ` x86 ` or ` x86_64 `
78
100
79
- Executing code with unsupported features is undefined behavior on this platform.
80
- Hence this platform requires that ` #[target_feature] ` is only applied to [ ` unsafe `
81
- functions] [ unsafe function ] .
82
-
83
101
Feature | Implicitly Enables | Description
84
102
------------|--------------------|-------------------
85
103
` aes ` | ` sse2 ` | [ AES] — Advanced Encryption Standard
@@ -135,9 +153,6 @@ Feature | Implicitly Enables | Description
135
153
136
154
#### ` aarch64 `
137
155
138
- This platform requires that ` #[target_feature] ` is only applied to [ ` unsafe `
139
- functions] [ unsafe function ] .
140
-
141
156
Further documentation on these features can be found in the [ ARM Architecture
142
157
Reference Manual] , or elsewhere on [ developer.arm.com] .
143
158
@@ -200,9 +215,8 @@ Feature | Implicitly Enables | Feature Name
200
215
201
216
#### ` wasm32 ` or ` wasm64 `
202
217
203
- ` #[target_feature] ` may be used with both safe and
204
- [ ` unsafe ` functions] [ unsafe function ] on Wasm platforms. It is impossible to
205
- cause undefined behavior via the ` #[target_feature] ` attribute because
218
+ ` #[target_feature] ` may be called from a safe context on Wasm platforms. It is
219
+ impossible to cause undefined behavior via the ` #[target_feature] ` attribute because
206
220
attempting to use instructions unsupported by the Wasm engine will fail at load
207
221
time without the risk of being interpreted in a way different from what the
208
222
compiler expected.
0 commit comments