-
Notifications
You must be signed in to change notification settings - Fork 141
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
libbpf-cargo: enums with duplicated values cause compilation issues #982
Comments
But that's likely only because we don't generate a binding for the enum to begin with, right?
It's a possibility, yeah, but I think there are ergonomic benefits to using a Rust enum. The other possibility is to throw away one variant. It's not great either, but then given that we've only seen this now suggests to me that this is a pretty rare thing to begin with -- and I don't know if penalizing everybody by switching to constants is worth it. |
Indeed, just mentioned this to clarify that this is narrowed down to the other commit.
Rust enums are so much nicer indeed... but throwing away one variant is IMO a big surprise for users, and it's possible that they prefer to the other name for a particular value. Seems like bindgen by default generates constants. |
Just a quick one before logging off for the day 😄 javierhonduco@1582426. libbpf-cargo tests pass, but have not tested it with my project yet or made the whole test suite pass, yet. |
Perhaps constants in a module name after the struct bpf_map_type(u8 /*or whatever*/);
impl bpf_map_type {
const BPF_MAP_TYPE_UNSPEC: bpf_map_type = bpf_map_type(1);
const BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED: bpf_map_type = bpf_map_type(19);
const BPF_MAP_TYPE_CGROUP_STORAGE: bpf_map_type = bpf_map_type(19);
} |
A discussion on the topic of using |
If it's possible to mark |
Enums can't have repeated values as far as I am aware, and there are no escape hatches. |
@javierhonduco we should take a look at the generated skeleton once done, but I'd say that the newtype struct logic mentioned above would be reasonable to implement. Please feel free to open a PR if you agree. |
@danielocfb Sounds good, will submit a PR in the next few days |
Rather than generating Rust enums from C enums, which might not be correct, as for example, C allows enum variants to share a value while Rust does not. This fixes libbpf#982. Test Plan ========= Verified that tests pass in CI in my fork and that my project compiles fine and works will with this change.
Rather than generating Rust enums from C enums, which might not be correct, as for example, C allows enum variants to share a value while Rust does not. This fixes libbpf#982. Test Plan ========= Verified that tests pass in CI in my fork and that my project compiles fine and works will with this change.
Rather than generating Rust enums from C enums, which might not be correct, as for example, C allows enum variants to share a value while Rust does not. This fixes libbpf#982. Test Plan ========= Verified that tests pass in CI in my fork and that my project compiles fine and works well with this change.
Rather than generating Rust enums from C enums, which might not be correct, as for example, C allows enum variants to share a value while Rust does not. This fixes libbpf#982. Test Plan ========= Verified that tests pass in CI in my fork and that my project compiles fine and works well with this change.
Rather than generating Rust enums from C enums, which might not be correct, as for example, C allows enum variants to share a value while Rust does not. This fixes libbpf#982. Test Plan ========= Verified that tests pass in CI in my fork and that my project compiles fine and works well with this change.
Just submitted a PR, I can't add you as a reviewer for some reason 😕. |
Rather than generating Rust enums from C enums, which might not be correct, as for example, C allows enum variants to share a value while Rust does not. This fixes libbpf#982. Test Plan ========= Verified that tests pass in CI in my fork and that my project compiles fine and works well with this change.
Rather than generating Rust enums from C enums, which might not be correct, as for example, C allows enum variants to share a value while Rust does not. This fixes libbpf#982. Test Plan ========= Added regression test where a C defined enum with duplicated values is converted to Rust using BTF and then successfully compiled with Rustc. Verified that tests pass in CI in my fork and that my project compiles fine and works well with this change.
Rather than generating Rust enums from C enums, which might not be correct, as for example, C allows enum variants to share a value while Rust does not. This fixes libbpf#982. Test Plan ========= Added regression test where a C defined enum with duplicated values is converted to Rust using BTF and then successfully compiled with Rustc. Verified that tests pass in CI in my fork and that my project compiles fine and works well with this change.
Rather than generating Rust enums from C enums, which might not be correct, as for example, C allows enum variants to share a value while Rust does not. This fixes #982. Test Plan ========= Added regression test where a C defined enum with duplicated values is converted to Rust using BTF and then successfully compiled with Rustc. Verified that tests pass in CI in my fork and that my project compiles fine and works well with this change.
Hi, I have some problem about this issue. |
You should be able to match like so But if |
In this code, #[derive(Debug, Copy, Clone)]
#[repr(transparent)]
pub struct event_type(u32);
#[allow(non_upper_case_globals)]
impl event_type {
pub const A: event_type = event_type(0);
pub const B: event_type = event_type(1);
}
impl Default for event_type {
fn default() -> Self {
event_type::A
}
} |
I added |
@danielocfb thanks! |
Thanks, Daniel! Could I please ask you for a new release? Very keen to update the version of libbpf-rs we are using 😀 |
We can, but given that it's a breaking release I'd rather wait some more to see if more breaking changes can be included as well. I was going to look at #350 some time not too far out (I hope...), for example, though not sure when that will happen and if we can find a satisfactory solution. |
I get that minimising the number of releases with breaking changes is a good experience, but also the chance of introducing regressions might be smaller? Not sure. I know you care about user experience and minimising disruption, so I understand whatever you decide. In my particular case, a fresh release would allow to publish my crate as typically some crate registries are not happy with git snapshots and only allow published releases. I haven't been able to upgrade libbpf-rs for a while due to this bug and there are other features that I would like to rely on, besides trying to use a newer version of the library (and not the 3+ month old one we run 😬 ). |
We can certainly publish a In this particular instance, I noticed that the logic is still wrong. We can't just unconditionally use |
That works for me, thanks.
Not sure exactly what you mean. The logic for choosing the inner type size did not change as far as I can see libbpf-rs/libbpf-cargo/src/gen/btf.rs Line 813 in 662887e
Makes sense, definitely having to transmute is not ideal, sorry for the oversight. |
Yeah, you are right. Not sure what I was looking at. Will add the logic for "to int" conversion and then cut a beta release. |
FYI, https://crates.io/crates/libbpf-rs/0.25.0-beta.0 is out now |
In 4e62c38, Rust type generation changed from the used types to all types. This is mostly harmless, except in the case were various enum entries share the same value. This is perfectly valid in C and this happens in the
bpf_map_type
enum. The error currently get:I've verified that the parent commit 65094bc does not show this issue. Perhaps rather than emitting Rust enums we could declare them as constants? Opening this issues mostly so I don't forget. I'll be happy to submit a PR if this approach sounds reasonable.
cc @d-e-s-o
The text was updated successfully, but these errors were encountered: