Pre-RFC interest gauging: type aliases in repr

This is something that I noticed by accident that might be useful, so, I figured that I'd gauge people's thoughts here.

Essentially, one thing that I've done in multiple crates before is have a type MyUnit = uN; to make it a bit easier to potentially change N later in the design process. It's also just helpful to be able to put into signatures what the "units" of something are without having to resort to newtypes.

One thing where this fails, however, is #[repr(uN)]. It would be nice if we could do #[repr(MyUnit)] here, resolving type aliases in this attribute.

Of course, this would complicate the language and potentially allow for shadowing (for example, what if you nefariously do type u8 = u32;) and thus would be worth considering those as well.

The reason why I discovered it by accident is by doing a find-and-replace for u8 in a crate and accidentally included repr as well. But, why can't we do this in repr?

9 Likes

This is also relevant for c_int and the other FFI aliases as well. Most of the time FFI enums want repr(C) to match the behavior of C enums, but C23 adds the ability to declare enums with explicit backing type, and it'd be desirable for Rust to be able to utilize the proper alias to mirror the type chosen on the C side if it isn't a fixed size int alias.

7 Likes

This seems reasonable to me. RFC?

1 Like

(I haven't written an RFC yet; mostly posting here to gauge interest. Will probably write up in a few days.)

3 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.