Skip to content

Missing attribute name and corrupted output for unsafe attribute used without unsafe #134078

Closed as duplicate of#133994
@ia0

Description

@ia0

Code

# bar/Cargo.toml
[package]
name = "bar"
version = "0.1.0"
edition = "2024"

[lib]
proc-macro = true

[dependencies]
quote = "1.0.37"
// bar/src/lib.rs
use proc_macro::TokenStream;
use quote::quote;

#[proc_macro]
pub fn bar(input: TokenStream) -> TokenStream {
    assert!(input.is_empty());
    quote! {
        #[no_mangle]
        pub extern "C" fn yolo() {}
    }
    .into()
}
# foo/Cargo.toml
[package]
name = "foo"
version = "0.1.0"
edition = "2021"

[dependencies]
bar.path = "../bar"
// foo/src/lib.rs
bar::bar!();

Current output

Running cargo check from the foo directory:

error: unsafe attribute used without unsafe
 --> src/lib.rs:1:1
  |
1 | bar::bar!();
  | ^^^^^^^^^^^ usage of unsafe attribute
  |
  = note: this error originates in the macro `bar::bar` (in Nightly builds, run with -Z macro-backtrace for more info)
help: wrap the attribute in `unsafe(...)`
  |
1 | baunsafe(r::bar!());
  |   +++++++        +

error: could not compile `foo` (lib) due to 1 previous error

Desired output

error: unsafe attribute used without unsafe
 --> src/lib.rs:1:1
  |
1 | bar::bar!();
  | ^^^^^^^^^^^ usage of unsafe attribute
  |
  = note: this error originates in the macro `bar::bar` (in Nightly builds, run with -Z macro-backtrace for more info)
help: wrap the attribute in `unsafe(...)`
  |
1 | #[unsafe(no_mangle)]
  |   +++++++         +

error: could not compile `foo` (lib) due to 1 previous error

Rationale and extra context

Note that the problem occurs only when bar uses edition 2024. It doesn't matter if foo does too.

Other cases

No response

Rust Version

rustc 1.85.0-nightly (d49be02cf 2024-12-02)
binary: rustc
commit-hash: d49be02cf6d2e2a01264fcdef1e20c826710c0f5
commit-date: 2024-12-02
host: x86_64-unknown-linux-gnu
release: 1.85.0-nightly
LLVM version: 19.1.4

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-edition-2024Area: The 2024 editionA-proc-macrosArea: Procedural macrosA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.L-unsafe_attr_outside_unsafeLint: unsafe_attr_outside_unsafeT-compilerRelevant to the compiler 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