ros2_rust icon indicating copy to clipboard operation
ros2_rust copied to clipboard

Upgrade to Rust 1.78

Open maspe36 opened this issue 1 year ago • 9 comments

We are currently on Rust 1.74, and now as of Rust 1.78, the u128 type is now FFI safe. Lets go ahead and upgrade.

Doing this upgrade will help us resolve https://github.com/ros2-rust/ros2_rust/issues/320 once and for all. As part of this upgrade, we should also remove the #![allow(improper_ctypes)] and #![allow(improper_ctypes_definitions)] lines from our rcl bindings as we no longer need to ignore these lints.

maspe36 avatar May 13 '24 12:05 maspe36

I'm currently working on. Is there a reason why you have set the rust version in cargo.toml for rclrs?

Guelakais avatar Jul 01 '24 16:07 Guelakais

That rust version is supposed to be the minimum supported version of rust that rclrs would work with. However, our CI does not use this same version so there is a chance that its actually misleading now.

I think with this upgrade to at least Rust 1.78 we'd want to upgrade rclrs to 1.78 in the cargo.toml as well since we would depend on u128 FFI safe functions.

maspe36 avatar Jul 14 '24 18:07 maspe36

Something that we should probably think about is what ROS 2 / Ubuntu version we target and what Rust compiler is shipped with it. The rustc shipped in Noble is 1.75, so having anything higher than that as a minimum supported version might put us at risk of not being able to build the packages on a stock Ubuntu distro that only relies on Debian packages and doesn't boostrap rustc from rustup.

luca-della-vedova avatar Oct 25 '24 05:10 luca-della-vedova

@luca-della-vedova That's a good point. Since Noble is an LTS we should expect that the build farm will be targeting it for quite a while now, and any build farm integration that we hope to accomplish before 26.04 will probably be forced to rely on the version of Rust that shipped with Noble.

It seems the only motivation to go as high as 1.78 is this FFI safety for u128, but even with version 1.78, it's not guaranteed to be safe across all targets. Maybe it's worth putting some time into researching whether there's a more sound way to achieve FFI safety. Especially something that can work with version 1.75.

mxgrey avatar Oct 25 '24 07:10 mxgrey

Yeah, I think tying our minimum Rust version to what is shipped with Noble is reasonable if it helps reduce friction to get into the build farm.

boostrap rustc from rustup.

FWIW, rustup is also shipped in 24.04. All hope is not lost for moving beyond 1.75. Perhaps as the project matures and we are integrated into the build farm we can look at how to unblock Rust upgrades. The hope is still to have a single version of rclrs target many different ROS distros.

maspe36 avatar Oct 26 '24 17:10 maspe36

It turns out the FFI issue is thornier than we originally realized.

The real origin of the problem is that rosidl_dynamic_typesupport introduces float128 support, even though float128 is not a primitive type that's normally supported by rosidl.

This becomes a problem because bindgen does not properly support long double (float128). These uses of long double in the C API are quietly converted into u128 by bindgen, which is immediately a big problem for us, regardless of the u128 alignment issue that triggers the warning that we've been suppressing.

Currently our dyn_msg module is extremely minimal and doesn't include much of anything to actually inspect messages. I considered whether we can just filter out float128 from all the bindings that we generate to sidestep the error, but this indispensable struct contains a member that references float128, so the warnings would persist even if we try to filter.

My recommendation would be this:

  • We rename this issue to something like "dyn_msg float128 support needs bindgen to fix long double"
  • We disregard the v1.78 concern because it doesn't address the real problem
  • We keep the improper_ctypes warning suppression, perhaps adding a comment placing the blame on the lack of long double support in bindgen
  • We don't worry about this problem until someone starts fleshing out the dyn_msg module to really do type inspection
  • We probably shouldn't support float128 at all until bindgen fixes long double because we shouldn't take accountability for the ABI issues that could arise

mxgrey avatar Oct 28 '24 08:10 mxgrey

After doing even more research on long double, I've reached the conclusion that bindgen shouldn't support interpreting long doubles from a C API, now or ever, because the binary representation of a long double is ambiguous.

Moreover I believe ROS should not support transmitting long doubles for the same reason, and I've opened an issue ticket upstream recommended that they be removed from dynamic type support: https://github.com/ros2/rosidl_dynamic_typesupport/issues/11

If that goes through, the following step would be to update the ROS IDL design doc to explicitly exclude long double from the subset of IDL that ROS supports (it's already not present in the ordinary builtin primitive types).

If that all goes according to plan, we'll be able to just remove the improper_ctypes suppression in the future and forget that float128 was ever a concern.

mxgrey avatar Oct 29 '24 05:10 mxgrey

@mxgrey thank you so much for doing all this research, it'd be great to constraint rosidl_dynamic_typesupport and so we could remove improper_ctypes. Would you like to wait for https://github.com/ros2/rosidl_dynamic_typesupport/issues/11 to be addressed before we update the version of Rust in our CI or would you rather update it now and then remove improper_ctypes in the future?

esteve avatar Oct 30 '24 11:10 esteve

I think we should not increase the minimal version to 1.78 no matter what because it will leave us incompatible with the build farm which will be limited to 1.75 for the next two years or so.

In the PR I've increased it to 1.75 to reflect our need to maintain support for 1.75 for our build farm aspirations, but I don't mind reverting it to 1.74 and putting in a comment to remind us not to go past 1.75.

mxgrey avatar Oct 30 '24 11:10 mxgrey