Skip to content

calling functions with implicit-enabled target features is unsafe #19154

Closed
@usamoi

Description

@usamoi

rust-analyzer version: 0.4.2302-standalone
rustc version: rustc 1.86.0-nightly (ef148cd7e 2025-02-12)
code snippet to reproduce:

#![feature(target_feature_11)]

#[target_feature(enable = "avx")]
fn f() {}

#[target_feature(enable = "avx2")]
fn g() {
    f();
    // it's safe, because enabling `avx2` enables `avx` implicitly, but ra reports that it's unsafe
    // https://github.com/rust-lang/reference/pull/1720 describe it, and nightly rustc agrees with it
}

This should be caused by

// RFC 2396 <https://rust-lang.github.io/rfcs/2396-target-feature-1.1.html>.
let callee_target_features = TargetFeatures::from_attrs(&db.attrs(func.into()));
if !caller_target_features.enabled.is_superset(&callee_target_features.enabled) {
return Unsafety::Unsafe;
}

It should calculate the set of all explicit-enabled and implicit-enabled target features of caller and calee, and then check if's a subset.

The "target feature to implicit-enabled target features" relationship is recorded in https://github.com/rust-lang/rust/blob/master/compiler/rustc_target/src/target_features.rs. (How to get it in ra?)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions