From 0f229ab19dd7cbdbc4d5525bf945d74771da942a Mon Sep 17 00:00:00 2001 From: Luca Versari Date: Fri, 24 Jan 2025 11:24:03 +0100 Subject: [PATCH] Update reference for target_feature_11. --- src/attributes/codegen.md | 45 ++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/src/attributes/codegen.md b/src/attributes/codegen.md index ab9295c3b..109c9c3f9 100644 --- a/src/attributes/codegen.md +++ b/src/attributes/codegen.md @@ -69,7 +69,7 @@ features. It uses the [_MetaListNameValueStr_] syntax with a single key of ```rust # #[cfg(target_feature = "avx2")] #[target_feature(enable = "avx2")] -unsafe fn foo_avx2() {} +fn foo_avx2() {} ``` r[attributes.codegen.target_feature.arch] @@ -77,11 +77,34 @@ Each [target architecture] has a set of features that may be enabled. It is an error to specify a feature for a target architecture that the crate is not being compiled for. +r[attributes.codegen.target_feature.closures] +Closures defined within a `target_feature`-annotated function inherit the +attribute from the enclosing function. + r[attributes.codegen.target_feature.target-ub] It is [undefined behavior] to call a function that is compiled with a feature that is not supported on the current platform the code is running on, *except* if the platform explicitly documents this to be safe. +r[attributes.codegen.target_feature.safety] +Because of this, on many platforms safe functions and closures marked with +`target_feature` do not implement Fn traits, cannot be coerced to safe `fn` +pointers (but can be coerced to `unsafe fn` pointers) and require `unsafe` +to be called. These restrictions are lifted in contexts such that the set of +features enabled in the callee is a subset of the set of features enabled in +the caller. + +r[attributes.codegen.target_feature.restrictions] +Note that, since Rust needs to be able to check the usage of `#[target_feature]` +functions at callsites to ensure safety, safe functions for which this check +would not be possible cannot be annotated with this attribute. This includes: + +- `main` +- other special functions that allow safe functions such as `#[panic_handler]` +- safe trait methods +- safe default functions in traits + + r[attributes.codegen.target_feature.inline] Functions marked with `target_feature` are not inlined into a context that does not support the given features. The `#[inline(always)]` attribute may not @@ -98,8 +121,7 @@ r[attributes.codegen.target_feature.x86] Executing code with unsupported features is undefined behavior on this platform. -Hence this platform requires that `#[target_feature]` is only applied to [`unsafe` -functions][unsafe function]. +Hence on this platform usage of `#[target_feature]` functions is usually unsafe. Feature | Implicitly Enables | Description ------------|--------------------|------------------- @@ -166,8 +188,7 @@ r[attributes.codegen.target_feature.aarch64] #### `aarch64` -This platform requires that `#[target_feature]` is only applied to [`unsafe` -functions][unsafe function]. +On this platform usage of `#[target_feature]` functions is usually unsafe. Further documentation on these features can be found in the [ARM Architecture Reference Manual], or elsewhere on [developer.arm.com]. @@ -231,8 +252,7 @@ r[attributes.codegen.target_feature.riscv] #### `riscv32` or `riscv64` -This platform requires that `#[target_feature]` is only applied to [`unsafe` -functions][unsafe function]. +On this platform usage of `#[target_feature]` functions is usually unsafe. Further documentation on these features can be found in their respective specification. Many specifications are described in the [RISC-V ISA Manual] or @@ -293,12 +313,11 @@ r[attributes.codegen.target_feature.wasm] #### `wasm32` or `wasm64` -`#[target_feature]` may be used with both safe and -[`unsafe` functions][unsafe function] on Wasm platforms. It is impossible to -cause undefined behavior via the `#[target_feature]` attribute because -attempting to use instructions unsupported by the Wasm engine will fail at load -time without the risk of being interpreted in a way different from what the -compiler expected. +Safe `#[target_feature]` functions may always be used in safe contexts on Wasm +platforms. It is impossible to cause undefined behavior via the +`#[target_feature]` attribute because attempting to use instructions +unsupported by the Wasm engine will fail at load time without the risk of being +interpreted in a way different from what the compiler expected. Feature | Implicitly Enables | Description ----------------------|---------------------|-------------------