Skip to content

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

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
ia0 opened this issue Dec 9, 2024 · 2 comments
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-edition-2024 Area: The 2024 edition A-proc-macros Area: Procedural macros A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. L-unsafe_attr_outside_unsafe Lint: unsafe_attr_outside_unsafe T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ia0
Copy link
Contributor

ia0 commented Dec 9, 2024

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

@ia0 ia0 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 9, 2024
@jieyouxu
Copy link
Member

jieyouxu commented Dec 9, 2024

I believe this is a duplicate of #133994, thank you for the report.

@jieyouxu jieyouxu closed this as completed Dec 9, 2024
@jieyouxu jieyouxu added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. D-edition Diagnostics: An error or lint that should account for edition differences. A-edition-2024 Area: The 2024 edition T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. L-unsafe_attr_outside_unsafe Lint: unsafe_attr_outside_unsafe A-proc-macros Area: Procedural macros and removed A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. D-edition Diagnostics: An error or lint that should account for edition differences. labels Dec 9, 2024
@ia0
Copy link
Contributor Author

ia0 commented Dec 9, 2024

Indeed, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-edition-2024 Area: The 2024 edition A-proc-macros Area: Procedural macros A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. L-unsafe_attr_outside_unsafe Lint: unsafe_attr_outside_unsafe T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants