What to do about warning: `extern` block uses type `u128`, which is not FFI-safe
Asked Answered
D

1

8

I used the bindgen crate to create bindings from Rust to a C library but I get a whole bunch of warnings saying:

warning: `extern` block uses type `u128`, which is not FFI-safe
= note: 128-bit integers don't currently have a known stable ABI

What can I do about this warning? I assume I need to take some action to fix this, or can I ignore it?

Dasie answered 21/8, 2020 at 15:50 Comment(3)
see rust-lang/rust#54341 -- in short, you can't do this safely at the moment.Rumor
I have seen the issues but honestly I don't understand what this means. Does this mean it might work, have unexpected behavior or what exactly?Dasie
It means the behavior is undefined. It might work, it might cause a system exception, it might silently cause incorrect behavior. Additionally, any code that you write might work on one rustc version and not another, or might work differently between platforms. The reason it's not safe to use is because the compiler gives no guarantee that these types will cross the FFI boundary in a way that foreign code understands. In short: if you need to send 128 bits across FFI, do so using some other method for now (e.g. a pair of u64).Rumor
A
-1

Solution

Add #![allow(improper_ctypes)] to suppress the warnings.

Here are some other flags you might want to have if you are working on an FFI crate.

#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(dead_code)]
#![allow(unused_imports)]
#![allow(improper_ctypes)]
Ancylostomiasis answered 10/4 at 16:40 Comment(2)
Eh. The warning is there for a reason; just silencing it doesn't address the underlying issue.Mealy
@CharlesDuffy Actually, since 11 days ago, u128 and i128 are layout-compatible with C. The lint is still there to stay future proof (there is a discussion about lifting it). However, silencing improper_ctypes broadly as suggested here, as well as all other warnings that appear here, is definitely a bad idea.Ewall

© 2022 - 2024 — McMap. All rights reserved.