最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

rust - How to fix "unsafe attribute used without unsafe" in custom attribute macro? - Stack Overflow

programmeradmin2浏览0评论

project:

error code:

program_picorv32ec/project/src/main.rs

#[picorv32asm::entry]
fn main() -> ! {
    loop {}
}

error message:

$ cargo build
   Compiling picorv32asm v0.1.0 (/home/xiaguangbo/linux/project/custom_rv32_rust/program_picorv32ec/picorv32asm)
   Compiling project v0.1.0 (/home/xiaguangbo/linux/project/custom_rv32_rust/program_picorv32ec/project)
   Compiling picorv32entrymacro v0.1.0 (/home/xiaguangbo/linux/project/custom_rv32_rust/program_picorv32ec/picorv32asm/picorv32entrymacro)
error: unsafe attribute used without unsafe
--> src/main.rs:6:1
  |
6 | #[picorv32asm::entry]
  | ^^^^^^^^^^^^^^^^^^^^^ usage of unsafe attribute
  |
  = note: this error originates in the attribute macro `picorv32asm::entry` (in Nightly builds, run with -Z macro-backtrace for more info)
help: wrap the attribute in `unsafe(...)`
  |
6 | unsafe(#[picorv32asm::entry])
  | +++++++                     +

error: could not compile `project` (bin "project") due to 1 previous error

It doesn't work either if you follow the compiler's prompts.

I think other projects can also be written like this, such as wch rust project.

project: https://github/xiaguangbo/picorv32_and_rust

error code:

program_picorv32ec/project/src/main.rs

#[picorv32asm::entry]
fn main() -> ! {
    loop {}
}

error message:

$ cargo build
   Compiling picorv32asm v0.1.0 (/home/xiaguangbo/linux/project/custom_rv32_rust/program_picorv32ec/picorv32asm)
   Compiling project v0.1.0 (/home/xiaguangbo/linux/project/custom_rv32_rust/program_picorv32ec/project)
   Compiling picorv32entrymacro v0.1.0 (/home/xiaguangbo/linux/project/custom_rv32_rust/program_picorv32ec/picorv32asm/picorv32entrymacro)
error: unsafe attribute used without unsafe
--> src/main.rs:6:1
  |
6 | #[picorv32asm::entry]
  | ^^^^^^^^^^^^^^^^^^^^^ usage of unsafe attribute
  |
  = note: this error originates in the attribute macro `picorv32asm::entry` (in Nightly builds, run with -Z macro-backtrace for more info)
help: wrap the attribute in `unsafe(...)`
  |
6 | unsafe(#[picorv32asm::entry])
  | +++++++                     +

error: could not compile `project` (bin "project") due to 1 previous error

It doesn't work either if you follow the compiler's prompts.

I think other projects can also be written like this, such as wch rust project.

Share Improve this question edited Mar 20 at 0:57 kmdreko 61.8k6 gold badges95 silver badges162 bronze badges asked Mar 15 at 7:13 xiaguangboxiaguangbo 1171 silver badge6 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 2

Your #[picorv32asm::entry] macro generates from this:

quote!(
    #[export_name = "main"]
    fn main() -> ! {
        #(#stmts)*
    }
)

In the Rust 2024 edition, certain attributes were marked unsafe and you must wrap them in unsafe(...) to use them. #[export_name = ...] was one of those due to the possibility of global name conflicts. And since macros expand within the local crate, the error tries to suggest a fix there which won't work since you can't modify the macro output (directly).

One solution as you show is to use an earlier edition in the local crate. Deleting edition = ... altogether will mean it defaults to 2015 but you'd likely be better served by 2018 or 2021.

The other option is to update your macro to produce the new syntax.

#[unsafe(export_name = "main")]

delete `edition = 2024` for program_picorv32ec/picorv32asm/picorv32entrymacro/Cargo.toml

发布评论

评论列表(0)

  1. 暂无评论