Skip to content
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

#[no_mangle] wrapped in unsafe produces strange syntax error #138049

Closed
fidelicura opened this issue Mar 5, 2025 · 2 comments
Closed

#[no_mangle] wrapped in unsafe produces strange syntax error #138049

fidelicura opened this issue Mar 5, 2025 · 2 comments

Comments

@fidelicura
Copy link

Sorry, I couldn't think of a more appropriate name for this issue, and it should be renamed.

Reproducible Example

  • Edition: 2021
  • Version: 1.85.0
  1. create a library using cargo new --lib cool
  2. add a abi_stable crate using cargo add abi_stable:
`Cargo.toml`
[package]
name = "cool"
version = "0.1.0"
edition = "2021"

[dependencies]
abi_stable = "0.11.3"
  1. paste following code:
#[abi_stable::sabi_extern_fn]
#[no_mangle]
fn something() -> i32 {
    52
}
It compiles successfully.
$ cargo build
    Updating crates.io index
     Locking 51 packages to latest compatible versions
   Compiling proc-macro2 v1.0.94
   Compiling unicode-ident v1.0.18
   Compiling syn v1.0.109
   Compiling cfg-if v1.0.0
   Compiling libc v0.2.170
   Compiling autocfg v1.4.0
   Compiling serde v1.0.218
   Compiling parking_lot_core v0.9.10
   Compiling crossbeam-utils v0.8.21
   Compiling core_extensions v1.5.3
   Compiling smallvec v1.14.0
   Compiling serde_json v1.0.140
   Compiling core_extensions_proc_macros v1.5.3
   Compiling paste v1.0.15
   Compiling tstr_proc_macros v0.2.2
   Compiling scopeguard v1.2.0
   Compiling ryu v1.0.20
   Compiling itoa v1.0.15
   Compiling memchr v2.7.4
   Compiling typed-arena v2.0.2
   Compiling generational-arena v0.2.9
   Compiling libloading v0.7.4
   Compiling const_panic v0.2.12
   Compiling abi_stable_shared v0.11.0
   Compiling lock_api v0.4.12
   Compiling tstr v0.2.4
   Compiling repr_offset v0.2.2
   Compiling crossbeam-channel v0.5.14
   Compiling quote v1.0.39
   Compiling syn v2.0.99
   Compiling parking_lot v0.12.3
   Compiling serde_derive v1.0.218
   Compiling as_derive_utils v0.11.0
   Compiling abi_stable_derive v0.11.3
   Compiling abi_stable v0.11.3
   Compiling cool v0.1.0 (/home/kbezuglyi/cool)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 10.43s
  1. try to update to 2024 edition:
`cargo fix --edition --allow-dirty`
   Migrating Cargo.toml from 2021 edition to 2024
    Updating crates.io index
     Locking 51 packages to latest compatible versions
   Compiling proc-macro2 v1.0.94
   Compiling unicode-ident v1.0.18
   Compiling autocfg v1.4.0
   Compiling syn v1.0.109
    Checking cfg-if v1.0.0
   Compiling libc v0.2.170
   Compiling serde v1.0.218
   Compiling core_extensions v1.5.3
   Compiling parking_lot_core v0.9.10
   Compiling crossbeam-utils v0.8.21
   Compiling paste v1.0.15
   Compiling serde_json v1.0.140
    Checking smallvec v1.14.0
   Compiling core_extensions_proc_macros v1.5.3
   Compiling tstr_proc_macros v0.2.2
    Checking scopeguard v1.2.0
    Checking itoa v1.0.15
    Checking ryu v1.0.20
    Checking memchr v2.7.4
   Compiling typed-arena v2.0.2
    Checking libloading v0.7.4
    Checking generational-arena v0.2.9
    Checking const_panic v0.2.12
   Compiling abi_stable_shared v0.11.0
   Compiling lock_api v0.4.12
    Checking tstr v0.2.4
    Checking repr_offset v0.2.2
    Checking crossbeam-channel v0.5.14
   Compiling quote v1.0.39
   Compiling syn v2.0.99
    Checking parking_lot v0.12.3
   Compiling serde_derive v1.0.218
   Compiling as_derive_utils v0.11.0
   Compiling abi_stable_derive v0.11.3
    Checking abi_stable v0.11.3
    Checking cool v0.1.0 (/home/kbezuglyi/cool)
   Migrating src/lib.rs from 2021 edition to 2024
warning: failed to automatically apply fixes suggested by rustc to crate `cool`

after fixes were automatically applied the compiler reported errors within these files:

  * src/lib.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error: expected path
 --> src/lib.rs:3:3
  |
3 | #[unsafe(no_mangle)]
  |   ^^^^^^

error: aborting due to 1 previous error

Original diagnostics will follow.

warning: unsafe attribute used without unsafe
 --> src/lib.rs:3:3
  |
3 | #[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: `--force-warn unsafe-attr-outside-unsafe` implied by `--force-warn rust-2024-compatibility`
help: wrap the attribute in `unsafe(...)`
  |
3 | #[unsafe(no_mangle)]
  |   +++++++         +

warning: `cool` (lib) generated 1 warning (run `cargo fix --lib -p cool` to apply 1 suggestion)
warning: failed to automatically apply fixes suggested by rustc to crate `cool`

after fixes were automatically applied the compiler reported errors within these files:

  * src/lib.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error: expected path
 --> src/lib.rs:3:3
  |
3 | #[unsafe(no_mangle)]
  |   ^^^^^^

error: aborting due to 1 previous error

Original diagnostics will follow.

warning: `cool` (lib test) generated 1 warning (1 duplicate)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 9.85s
  1. manual edit didn't work either:
#[abi_stable::sabi_extern_fn]
#[unsafe(no_mangle)]
fn something() -> i32 {
    52
}
`cargo build`
error: expected path
 --> src/lib.rs:2:3
  |
2 | #[unsafe(no_mangle)]
  |   ^^^^^^

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

Additional Information

`cargo expand` on non-unsafe no-mangle attribute
#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
#[no_mangle]
extern "C" fn something() -> i32 {
    #[allow(clippy::redundant_closure_call)]
    {
        {
            let aborter_guard = {
                use ::abi_stable::utils::{AbortBomb, PanicInfo};
                #[allow(dead_code)]
                const BOMB: AbortBomb = AbortBomb {
                    fuse: &PanicInfo {
                        file: "src/lib.rs",
                        line: 1u32,
                    },
                };
                BOMB
            };
            let res = {
                let a = ::abi_stable::marker_type::NotCopyNotClone;
                (move || {
                    { a };
                    { 52 }
                })()
            };
            ::std::mem::forget(aborter_guard);
            res
        }
    }
}
@fidelicura fidelicura added the C-bug Category: This is a bug. label Mar 5, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 5, 2025
@bjorn3
Copy link
Member

bjorn3 commented Mar 5, 2025

Fair chance that abi-stable is at fault here. In any case it looks like the abi-stable crate hasn't been updated in 2 years, so you may want to use another crate instead.

@fidelicura
Copy link
Author

fidelicura commented Mar 5, 2025

Fair chance that abi-stable is at fault here. In any case it looks like the abi-stable crate hasn't been updated in 2 years, so you may want to use another crate instead.

Yes, probably. I think this is because of incorrect logic of handling syn tokens on a new edition. Sadly, but maybe it is really the case I will change my dependency from abi_stable to something else. Thanks.

@jieyouxu jieyouxu removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Mar 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants