rust/tests/ui/asm
Jacob Pratt 575405161f
Rollup merge of #134090 - veluca93:stable-tf11, r=oli-obk
Stabilize target_feature_11

# Stabilization report

This is an updated version of https://github.com/rust-lang/rust/pull/116114, which is itself a redo of https://github.com/rust-lang/rust/pull/99767. Most of this commit and report were copied from those PRs. Thanks ```@LeSeulArtichaut``` and ```@calebzulawski!```

## Summary
Allows for safe functions to be marked with `#[target_feature]` attributes.

Functions marked with `#[target_feature]` are generally considered as unsafe functions: they are unsafe to call, cannot *generally* be assigned to safe function pointers, and don't implement the `Fn*` traits.

However, calling them from other `#[target_feature]` functions with a superset of features is safe.

```rust
// Demonstration function
#[target_feature(enable = "avx2")]
fn avx2() {}

fn foo() {
    // Calling `avx2` here is unsafe, as we must ensure
    // that AVX is available first.
    unsafe {
        avx2();
    }
}

#[target_feature(enable = "avx2")]
fn bar() {
    // Calling `avx2` here is safe.
    avx2();
}
```

Moreover, once https://github.com/rust-lang/rust/pull/135504 is merged, they can be converted to safe function pointers in a context in which calling them is safe:

```rust
// Demonstration function
#[target_feature(enable = "avx2")]
fn avx2() {}

fn foo() -> fn() {
    // Converting `avx2` to fn() is a compilation error here.
    avx2
}

#[target_feature(enable = "avx2")]
fn bar() -> fn() {
    // `avx2` coerces to fn() here
    avx2
}
```

See the section "Closures" below for justification of this behaviour.

## Test cases
Tests for this feature can be found in [`tests/ui/target_feature/`](f6cb952dc1/tests/ui/target-feature).

## Edge cases
### Closures
 * [target-feature 1.1: should closures inherit target-feature annotations? #73631](https://github.com/rust-lang/rust/issues/73631)

Closures defined inside functions marked with #[target_feature] inherit the target features of their parent function. They can still be assigned to safe function pointers and implement the appropriate `Fn*` traits.

```rust
#[target_feature(enable = "avx2")]
fn qux() {
    let my_closure = || avx2(); // this call to `avx2` is safe
    let f: fn() = my_closure;
}
```
This means that in order to call a function with #[target_feature], you must guarantee that the target-feature is available while the function, any closures defined inside it, as well as any safe function pointers obtained from target-feature functions inside it, execute.

This is usually ensured because target features are assumed to never disappear, and:
- on any unsafe call to a `#[target_feature]` function, presence of the target feature is guaranteed by the programmer through the safety requirements of the unsafe call.
- on any safe call, this is guaranteed recursively by the caller.

If you work in an environment where target features can be disabled, it is your responsibility to ensure that no code inside a target feature function (including inside a closure) runs after this (until the feature is enabled again).

**Note:** this has an effect on existing code, as nowadays closures do not inherit features from the enclosing function, and thus this strengthens a safety requirement. It was originally proposed in #73631 to solve this by adding a new type of UB: “taking a target feature away from your process after having run code that uses that target feature is UB” .
This was motivated by userspace code already assuming in a few places that CPU features never disappear from a program during execution (see i.e. 2e29bdf908/crates/std_detect/src/detect/arch/x86.rs); however, concerns were raised in the context of the Linux kernel; thus, we propose to relax that requirement to "causing the set of usable features to be reduced is unsafe; when doing so, the programmer is required to ensure that no closures or safe fn pointers that use removed features are still in scope".

* [Fix #[inline(always)] on closures with target feature 1.1 #111836](https://github.com/rust-lang/rust/pull/111836)

Closures accept `#[inline(always)]`, even within functions marked with `#[target_feature]`. Since these attributes conflict, `#[inline(always)]` wins out to maintain compatibility.

### ABI concerns
* [The extern "C" ABI of SIMD vector types depends on target features #116558](https://github.com/rust-lang/rust/issues/116558)

The ABI of some types can change when compiling a function with different target features. This could have introduced unsoundness with target_feature_11, but recent fixes (#133102, #132173) either make those situations invalid or make the ABI no longer dependent on features. Thus, those issues should no longer occur.

### Special functions
The `#[target_feature]` attribute is forbidden from a variety of special functions, such as main, current and future lang items (e.g. `#[start]`, `#[panic_handler]`), safe default trait implementations and safe trait methods.

This was not disallowed at the time of the first stabilization PR for target_features_11, and resulted in the following issues/PRs:
* [`#[target_feature]` is allowed on `main` #108645](https://github.com/rust-lang/rust/issues/108645)
* [`#[target_feature]` is allowed on default implementations #108646](https://github.com/rust-lang/rust/issues/108646)
* [#[target_feature] is allowed on #[panic_handler] with target_feature 1.1 #109411](https://github.com/rust-lang/rust/issues/109411)
* [Prevent using `#[target_feature]` on lang item functions #115910](https://github.com/rust-lang/rust/pull/115910)

## Documentation
 * Reference: [Document the `target_feature_11` feature reference#1181](https://github.com/rust-lang/reference/pull/1181)
---

cc tracking issue https://github.com/rust-lang/rust/issues/69098
cc ```@workingjubilee```
cc ```@RalfJung```
r? ```@rust-lang/lang```
2025-02-12 20:09:56 -05:00
..
aarch64 Rollup merge of #136239 - folkertdev:show-supported-register-classes, r=SparrowLii,jieyouxu 2025-02-11 18:04:34 +01:00
loongarch tests/ui/asm: Remove uses of rustc_attrs, lang_items, and decl_macro features by using minicore 2024-12-17 01:12:36 +09:00
powerpc tests/ui/asm: Remove uses of rustc_attrs, lang_items, and decl_macro features by using minicore 2024-12-17 01:12:36 +09:00
riscv Remove duplicated code in RISC-V asm bad-reg test 2025-01-28 23:49:02 +09:00
s390x tests/ui/asm: Remove uses of rustc_attrs, lang_items, and decl_macro features by using minicore 2024-12-17 01:12:36 +09:00
sparc tests/ui/asm: Remove uses of rustc_attrs, lang_items, and decl_macro features by using minicore 2024-12-17 01:12:36 +09:00
x86_64 Rollup merge of #134090 - veluca93:stable-tf11, r=oli-obk 2025-02-12 20:09:56 -05:00
arm-low-dreg.rs tests/ui/asm: Remove uses of rustc_attrs, lang_items, and decl_macro features by using minicore 2024-12-17 01:12:36 +09:00
bad-template.aarch64.stderr tests/ui/asm: Remove uses of rustc_attrs, lang_items, and decl_macro features by using minicore 2024-12-17 01:12:36 +09:00
bad-template.rs tests/ui/asm: Remove uses of rustc_attrs, lang_items, and decl_macro features by using minicore 2024-12-17 01:12:36 +09:00
bad-template.x86_64.stderr tests/ui/asm: Remove uses of rustc_attrs, lang_items, and decl_macro features by using minicore 2024-12-17 01:12:36 +09:00
binary_asm_labels.rs add lint for inline asm labels that look like binary 2024-07-09 01:23:49 +00:00
binary_asm_labels.stderr Update the `binary_asm_label` message 2024-07-18 17:00:43 -04:00
binary_asm_labels_allowed.rs Change `binary_asm_labels` to only fire on x86 and x86_64 2024-07-18 15:00:56 -05:00
const-error.rs adjust test 2024-09-29 08:49:37 +02:00
const-refs-to-static.rs Stabilize `const_refs_to_static` 2024-09-26 13:21:15 +02:00
const-refs-to-static.stderr Stabilize `const_refs_to_static` 2024-09-26 13:21:15 +02:00
empty_global_asm.rs Move 100 entries from tests/ui into subdirs 2024-05-20 19:55:59 -07:00
fail-const-eval-issue-121099.rs stabilize `asm_const` 2024-08-13 23:18:31 +02:00
fail-const-eval-issue-121099.stderr stabilize `asm_const` 2024-08-13 23:18:31 +02:00
generic-const.rs stabilize `asm_const` 2024-08-13 23:18:31 +02:00
generic_const_simd_vec_len.rs Clarify why a type is rejected for asm! 2024-12-11 20:17:37 +00:00
generic_const_simd_vec_len.stderr Clarify why a type is rejected for asm! 2024-12-11 20:17:37 +00:00
ice-bad-err-span-in-template-129503.rs Fix error span when arg to asm!() is a macro call 2024-09-27 09:49:15 +05:30
ice-bad-err-span-in-template-129503.stderr Fix error span when arg to asm!() is a macro call 2024-09-27 09:49:15 +05:30
inline-syntax.arm.stderr tests/ui/asm: Remove uses of rustc_attrs, lang_items, and decl_macro features by using minicore 2024-12-17 01:12:36 +09:00
inline-syntax.arm_llvm_18.stderr tests/ui/asm: Remove uses of rustc_attrs, lang_items, and decl_macro features by using minicore 2024-12-17 01:12:36 +09:00
inline-syntax.rs tests/ui/asm: Remove uses of rustc_attrs, lang_items, and decl_macro features by using minicore 2024-12-17 01:12:36 +09:00
inline-syntax.x86_64.stderr tests/ui/asm: Remove uses of rustc_attrs, lang_items, and decl_macro features by using minicore 2024-12-17 01:12:36 +09:00
invalid-const-operand.rs stabilize `asm_const` 2024-08-13 23:18:31 +02:00
invalid-const-operand.stderr Show diff suggestion format on verbose replacement 2025-02-10 20:21:39 +00:00
invalid-sym-operand.rs add `needs-asm-support` to invalid-sym-operand 2024-08-02 19:59:34 +01:00
invalid-sym-operand.stderr add `needs-asm-support` to invalid-sym-operand 2024-08-02 19:59:34 +01:00
issue-72570.rs [AUTO-GENERATED] Migrate ui tests from `//` to `//@` directives 2024-02-16 20:02:50 +00:00
issue-72570.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
issue-85247.rs tests/ui/asm: Remove uses of rustc_attrs, lang_items, and decl_macro features by using minicore 2024-12-17 01:12:36 +09:00
issue-85247.rwpi.stderr tests/ui/asm: Remove uses of rustc_attrs, lang_items, and decl_macro features by using minicore 2024-12-17 01:12:36 +09:00
issue-87802.rs Update test directives for `wasm32-wasip1` 2024-03-11 09:36:35 -07:00
issue-87802.stderr Update test directives for `wasm32-wasip1` 2024-03-11 09:36:35 -07:00
issue-89305.rs [AUTO-GENERATED] Migrate ui tests from `//` to `//@` directives 2024-02-16 20:02:50 +00:00
issue-89305.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-92378.rs tests/ui/asm: Remove uses of rustc_attrs, lang_items, and decl_macro features by using minicore 2024-12-17 01:12:36 +09:00
issue-97490.rs [AUTO-GENERATED] Migrate ui tests from `//` to `//@` directives 2024-02-16 20:02:50 +00:00
issue-99071.rs tests/ui/asm: Remove uses of rustc_attrs, lang_items, and decl_macro features by using minicore 2024-12-17 01:12:36 +09:00
issue-99071.stderr tests/ui/asm: Remove uses of rustc_attrs, lang_items, and decl_macro features by using minicore 2024-12-17 01:12:36 +09:00
issue-99122-2.rs [AUTO-GENERATED] Migrate ui tests from `//` to `//@` directives 2024-02-16 20:02:50 +00:00
issue-99122.rs [AUTO-GENERATED] Migrate ui tests from `//` to `//@` directives 2024-02-16 20:02:50 +00:00
issue-99122.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
issue-113788.rs [AUTO-GENERATED] Migrate ui tests from `//` to `//@` directives 2024-02-16 20:02:50 +00:00
issue-113788.stderr Show number in error message even for one error 2023-11-24 19:15:52 +01:00
may_unwind.rs Add needs-unwind annotations to a couple of tests 2024-03-25 14:19:07 +00:00
naked-asm-outside-naked-fn.rs disallow `naked_asm!` outside of `#[naked]` functions 2024-09-10 15:19:14 +02:00
naked-asm-outside-naked-fn.stderr disallow `naked_asm!` outside of `#[naked]` functions 2024-09-10 15:19:14 +02:00
naked-functions-ffi.rs disallow `asm!` in `#[naked]` functions 2024-10-06 18:12:25 +02:00
naked-functions-ffi.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
naked-functions-inline.rs disallow `asm!` in `#[naked]` functions 2024-10-06 18:12:25 +02:00
naked-functions-inline.stderr switch to an allowlist approach 2024-07-27 12:55:39 +02:00
naked-functions-instruction-set.rs tests/ui/asm: Remove uses of rustc_attrs, lang_items, and decl_macro features by using minicore 2024-12-17 01:12:36 +09:00
naked-functions-testattrs.rs disallow `asm!` in `#[naked]` functions 2024-10-06 18:12:25 +02:00
naked-functions-testattrs.stderr switch to an allowlist approach 2024-07-27 12:55:39 +02:00
naked-functions-unused.aarch64.stderr use `naked_asm!` in naked-function tests 2024-10-06 18:12:25 +02:00
naked-functions-unused.rs disallow `asm!` in `#[naked]` functions 2024-10-06 18:12:25 +02:00
naked-functions-unused.x86_64.stderr use `naked_asm!` in naked-function tests 2024-10-06 18:12:25 +02:00
naked-functions.rs fix: rust-lang/rust#47446 2024-11-15 15:25:19 +01:00
naked-functions.stderr various fixes for `naked_asm!` implementation 2024-10-06 19:00:09 +02:00
naked-invalid-attr.rs disallow `asm!` in `#[naked]` functions 2024-10-06 18:12:25 +02:00
naked-invalid-attr.stderr disallow `asm!` in `#[naked]` functions 2024-10-06 18:12:25 +02:00
naked-with-invalid-repr-attr.rs disallow `asm!` in `#[naked]` functions 2024-10-06 18:12:25 +02:00
naked-with-invalid-repr-attr.stderr disallow `asm!` in `#[naked]` functions 2024-10-06 18:12:25 +02:00
named-asm-labels.rs disallow `asm!` in `#[naked]` functions 2024-10-06 18:12:25 +02:00
named-asm-labels.s Move /src/test to /tests 2023-01-11 09:32:08 +00:00
named-asm-labels.stderr disallow `asm!` in `#[naked]` functions 2024-10-06 18:12:25 +02:00
named_const_simd_vec_len.rs Evaluate constants in SIMD vec lengths before rejecting them 2024-12-11 20:17:37 +00:00
non-const.rs Note def descr in NonConstFunctionCall 2024-12-23 22:15:32 +00:00
non-const.stderr Note def descr in NonConstFunctionCall 2024-12-23 22:15:32 +00:00
noreturn.rs [AUTO-GENERATED] Migrate ui tests from `//` to `//@` directives 2024-02-16 20:02:50 +00:00
parse-error.rs stabilize `asm_const` 2024-08-13 23:18:31 +02:00
parse-error.stderr Show diff suggestion format on verbose replacement 2025-02-10 20:21:39 +00:00
reg-conflict.rs tests/ui/asm: Remove uses of rustc_attrs, lang_items, and decl_macro features by using minicore 2024-12-17 01:12:36 +09:00
reg-conflict.stderr tests/ui/asm: Remove uses of rustc_attrs, lang_items, and decl_macro features by using minicore 2024-12-17 01:12:36 +09:00
simple_global_asm.rs Move 100 entries from tests/ui into subdirs 2024-05-20 19:55:59 -07:00
type-check-1.rs stabilize `asm_const` 2024-08-13 23:18:31 +02:00
type-check-1.stderr stabilize `asm_const` 2024-08-13 23:18:31 +02:00
type-check-4.rs Update test directives for `wasm32-wasip1` 2024-03-11 09:36:35 -07:00
type-check-4.stderr Update test directives for `wasm32-wasip1` 2024-03-11 09:36:35 -07:00
unpretty-expanded.rs [AUTO-GENERATED] Migrate ui tests from `//` to `//@` directives 2024-02-16 20:02:50 +00:00
unpretty-expanded.stdout [AUTO-GENERATED] Migrate ui tests from `//` to `//@` directives 2024-02-16 20:02:50 +00:00
unsupported-option.fixed add `needs-asm-support` to `tests/ui/asm/unsupported-option.rs` 2024-07-27 19:27:20 +02:00
unsupported-option.rs add `needs-asm-support` to `tests/ui/asm/unsupported-option.rs` 2024-07-27 19:27:20 +02:00
unsupported-option.stderr add `needs-asm-support` to `tests/ui/asm/unsupported-option.rs` 2024-07-27 19:27:20 +02:00