Skip to content

Can't allow unsafe_attr_outside_unsafe in Rust 2015-2021 #140602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
PatchMixolydic opened this issue May 3, 2025 · 2 comments · Fixed by #140617
Closed

Can't allow unsafe_attr_outside_unsafe in Rust 2015-2021 #140602

PatchMixolydic opened this issue May 3, 2025 · 2 comments · Fixed by #140617
Assignees
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. D-edition Diagnostics: An error or lint that should account for edition differences. L-unsafe_attr_outside_unsafe Lint: unsafe_attr_outside_unsafe S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@PatchMixolydic
Copy link
Contributor

PatchMixolydic commented May 3, 2025

I tried this code in Rust 2021 (playground):

#![deny(unsafe_attr_outside_unsafe)]

#[allow(unsafe_attr_outside_unsafe)]
mod generated {
    #[no_mangle]
    fn _generated_foo() {}
}

I expected to see this happen: compiles successfully

Instead, this happened:

error: unsafe attribute used without unsafe
 --> src/lib.rs:5:7
  |
5 |     #[no_mangle]
  |       ^^^^^^^^^ usage of unsafe attribute
  |
  = warning: this is accepted in the current edition (Rust 2021) but is a hard error in Rust 2024!
  = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/unsafe-attributes.html>
note: the lint level is defined here
 --> src/lib.rs:1:9
  |
1 | #![deny(unsafe_attr_outside_unsafe)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^
help: wrap the attribute in `unsafe(...)`
  |
5 |     #[unsafe(no_mangle)]
  |       +++++++         +

Meta

rustc --version --verbose:

rustc 1.85.0 (4d91de4e4 2025-02-17)
binary: rustc
commit-hash: 4d91de4e48198da2e33413efdcd9cd2cc0c46688
commit-date: 2025-02-17
host: x86_64-unknown-linux-gnu
release: 1.85.0
LLVM version: 19.1.7

Reproduces in 1.88.0-nightly (2025-05-01 3350c1eb3fd8fe1bee1e) on the Playground.

@PatchMixolydic PatchMixolydic added the C-bug Category: This is a bug. label May 3, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 3, 2025
@PatchMixolydic
Copy link
Contributor Author

@rustbot labels +A-lints +D-edition +L-unsafe_attr_outside_unsafe

@rustbot rustbot added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. D-edition Diagnostics: An error or lint that should account for edition differences. L-unsafe_attr_outside_unsafe Lint: unsafe_attr_outside_unsafe labels May 3, 2025
@bjorn3
Copy link
Member

bjorn3 commented May 3, 2025

Looks like the lint gets attached to the crate root rather than the item that has the #[no_mangle]:

@jieyouxu jieyouxu self-assigned this May 3, 2025
@jieyouxu jieyouxu added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 3, 2025
@jieyouxu jieyouxu removed their assignment May 3, 2025
@bors bors closed this as completed in 54821b9 May 4, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue May 4, 2025
Rollup merge of rust-lang#140617 - Urgau:unsafe_attr-lint-allow, r=jieyouxu

Report the `unsafe_attr_outside_unsafe` lint at the closest node

This PR have `AstValidation` track a linting node id and then uses it when reporting the `unsafe_attr_outside_unsafe` lint, so that instead of being bound at the crate-root, `#[allow]` of the lint works at any node.

Fixes rust-lang#140602
r? `@jieyouxu`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. D-edition Diagnostics: An error or lint that should account for edition differences. L-unsafe_attr_outside_unsafe Lint: unsafe_attr_outside_unsafe S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants