Skip to content

Commit

Permalink
docs(refs): Add unsafe to extern while using build scripts in Car…
Browse files Browse the repository at this point in the history
…go Book (#15294)

### What does this PR try to resolve?

Since `rust 1.85.0`, It is no longer sufficient to use the `unsafe`
keyword only at the call site. It must also be applied to the `extern`
statement, hence, Cargo Book must be updated

- closes #15289

and also update some more related content

### How should we test and review this PR?

Run `mdbook build`. The `unsafe extern` should be seen at
`cargo/reference/build-script-examples.html#linking-to-system-libraries`
, `cargo/reference/build-script-examples.html#building-a-native-library`
, `cargo/reference/semver.html#repr-c-shuffle` ,
`cargo/reference/semver.html#repr-c-remove` and
`cargo/reference/semver.html#repr-transparent-remove`

### Additional information
  • Loading branch information
epage authored Mar 11, 2025
2 parents 35cf085 + a255e30 commit 3897c7c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/doc/src/reference/build-script-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ void hello() {
// Note the lack of the `#[link]` attribute. We’re delegating the responsibility
// of selecting what to link over to the build script rather than hard-coding
// it in the source file.
extern { fn hello(); }
unsafe extern { fn hello(); }
fn main() {
unsafe { hello(); }
Expand Down Expand Up @@ -327,7 +327,7 @@ Let's round out the example with a basic FFI binding:
use std::os::raw::{c_uint, c_ulong};
extern "C" {
unsafe extern "C" {
pub fn crc32(crc: c_ulong, buf: *const u8, len: c_uint) -> c_ulong;
}
Expand Down
6 changes: 3 additions & 3 deletions src/doc/src/reference/semver.md
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ pub struct SpecificLayout {
// Example usage that will break.
use updated_crate::SpecificLayout;
extern "C" {
unsafe extern "C" {
// This C function is assuming a specific layout defined in a C header.
fn c_fn_get_b(x: &SpecificLayout) -> u32;
}
Expand Down Expand Up @@ -820,7 +820,7 @@ pub struct SpecificLayout {
// Example usage that will break.
use updated_crate::SpecificLayout;
extern "C" {
unsafe extern "C" {
// This C function is assuming a specific layout defined in a C header.
fn c_fn_get_b(x: &SpecificLayout) -> u32; // Error: is not FFI-safe
}
Expand Down Expand Up @@ -941,7 +941,7 @@ pub struct Transparent<T>(T);
#![deny(improper_ctypes)]
use updated_crate::Transparent;
extern "C" {
unsafe extern "C" {
fn c_fn() -> Transparent<f64>; // Error: is not FFI-safe
}
Expand Down

0 comments on commit 3897c7c

Please sign in to comment.