@@ -69,19 +69,45 @@ 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
7575r[ attributes.codegen.target_feature.arch]
7676Each [ target architecture] has a set of features that may be enabled. It is an
7777error to specify a feature for a target architecture that the crate is not
7878being 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+
8084r[ attributes.codegen.target_feature.target-ub]
8185It is [ undefined behavior] to call a function that is compiled with a feature
8286that is not supported on the current platform the code is running on, * except*
8387if the platform explicitly documents this to be safe.
8488
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+
85111r[ attributes.codegen.target_feature.inline]
86112Functions marked with ` target_feature ` are not inlined into a context that
87113does not support the given features. The ` #[inline(always)] ` attribute may not
@@ -98,8 +124,8 @@ r[attributes.codegen.target_feature.x86]
98124
99125
100126Executing 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 ] .
103129
104130Feature | Implicitly Enables | Description
105131------------|--------------------|-------------------
@@ -166,8 +192,8 @@ r[attributes.codegen.target_feature.aarch64]
166192#### ` aarch64 `
167193
168194
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 ] .
171197
172198Further documentation on these features can be found in the [ ARM Architecture
173199Reference Manual] , or elsewhere on [ developer.arm.com] .
@@ -231,8 +257,8 @@ r[attributes.codegen.target_feature.riscv]
231257#### ` riscv32 ` or ` riscv64 `
232258
233259
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 ] .
236262
237263Further documentation on these features can be found in their respective
238264specification. Many specifications are described in the [ RISC-V ISA Manual] or
@@ -293,12 +319,11 @@ r[attributes.codegen.target_feature.wasm]
293319#### ` wasm32 ` or ` wasm64 `
294320
295321
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.
302327
303328Feature | Implicitly Enables | Description
304329----------------------|---------------------|-------------------
0 commit comments