Skip to content

rustdoc-json: #[target_feature] incorrectly causes safe functions to be listed as unsafe #142655

Open
@obi1kenobi

Description

@obi1kenobi

I tried this code:

#[target_feature(enable = "sse2")]
pub fn compute(x: u32) -> u32 {
    x + 1
}

I expected to see this happen: rustdoc JSON should contain an entry for compute whose header field says:

"header": {
    "is_const": false,
    "is_unsafe": false,
    "is_async": false,
    "abi": "Rust"
},

Instead, this happened: the rustdoc JSON entry for compute incorrectly lists it as unsafe. Here's its entire item entry from a format_version: 46 rustdoc JSON file:

"0": {
    "id": 0,
    "crate_id": 0,
    "name": "compute",
    "span": {
        "filename": "src/lib.rs",
        "begin": [
            2,
            1
        ],
        "end": [
            4,
            2
        ]
    },
    "visibility": "public",
    "docs": null,
    "links": {},
    "attrs": [
        "#[target_feature(enable = \"sse2\")]"
    ],
    "deprecation": null,
    "inner": {
        "function": {
            "sig": {
                "inputs": [
                    [
                        "x",
                        {
                            "primitive": "u32"
                        }
                    ]
                ],
                "output": {
                    "primitive": "u32"
                },
                "is_c_variadic": false
            },
            "generics": {
                "params": [],
                "where_predicates": []
            },
            "header": {
                "is_const": false,
                "is_unsafe": true,  # <--- BUG!
                "is_async": false,
                "abi": "Rust"
            },
            "has_body": true
        }
    }
},

Meta

rustc --version --verbose:

rustc 1.89.0-nightly (45acf54ee 2025-06-16)
binary: rustc
commit-hash: 45acf54eea118ed27927282b5e0bfdcd80b7987c
commit-date: 2025-06-16
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5

@rustbot label +A-rustdoc-json

cc @aDotInTheVoid

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-rustdoc-jsonArea: Rustdoc JSON backendA-target-featureArea: Enabling/disabling target features like AVX, Neon, etc.C-bugCategory: This is a bug.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-rustdocRelevant to the rustdoc team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions