Skip to content

remove function pointer comparisons #537

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

Merged
merged 1 commit into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion capnp/src/dynamic_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ impl<'a> Builder<'a> {
Ok(())
}
(TypeVariant::Struct(ss), dynamic_value::Reader::Struct(s)) => {
assert_eq!(ss, s.get_schema().raw);
assert!(core::ptr::eq(ss.generic, s.get_schema().raw.generic));
self.builder
.reborrow()
.get_struct_element(index)
Expand Down
35 changes: 28 additions & 7 deletions capnp/src/dynamic_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ impl<'a> Reader<'a> {
}

pub fn get(self, field: Field) -> Result<dynamic_value::Reader<'a>> {
assert_eq!(self.schema.raw, field.parent.raw);
assert!(core::ptr::eq(
self.schema.raw.generic,
field.parent.raw.generic
));
let ty = field.get_type();
match field.get_proto().which()? {
field::Slot(slot) => {
Expand Down Expand Up @@ -198,7 +201,10 @@ impl<'a> Reader<'a> {
/// is active in the union and is not a null pointer. On non-union fields,
/// returns `true` if the field is not a null pointer.
pub fn has(&self, field: Field) -> Result<bool> {
assert_eq!(self.schema.raw, field.parent.raw);
assert!(core::ptr::eq(
self.schema.raw.generic,
field.parent.raw.generic
));
let proto = field.get_proto();
if has_discriminant_value(proto) {
let node::Struct(st) = self.schema.get_proto().which()? else {
Expand Down Expand Up @@ -289,7 +295,10 @@ impl<'a> Builder<'a> {
}

pub fn get(self, field: Field) -> Result<dynamic_value::Builder<'a>> {
assert_eq!(self.schema.raw, field.parent.raw);
assert!(core::ptr::eq(
self.schema.raw.generic,
field.parent.raw.generic
));
let ty = field.get_type();
match field.get_proto().which()? {
field::Slot(slot) => {
Expand Down Expand Up @@ -446,7 +455,10 @@ impl<'a> Builder<'a> {
}

pub fn set(&mut self, field: Field, value: dynamic_value::Reader<'_>) -> Result<()> {
assert_eq!(self.schema.raw, field.parent.raw);
assert!(core::ptr::eq(
self.schema.raw.generic,
field.parent.raw.generic
));
self.set_in_union(field)?;
let ty = field.get_type();
match field.get_proto().which()? {
Expand Down Expand Up @@ -586,7 +598,10 @@ impl<'a> Builder<'a> {
}

pub fn init(mut self, field: Field) -> Result<dynamic_value::Builder<'a>> {
assert_eq!(self.schema.raw, field.parent.raw);
assert!(core::ptr::eq(
self.schema.raw.generic,
field.parent.raw.generic
));
self.set_in_union(field)?;
let ty = field.get_type();
match field.get_proto().which()? {
Expand Down Expand Up @@ -627,7 +642,10 @@ impl<'a> Builder<'a> {
}

pub fn initn(mut self, field: Field, size: u32) -> Result<dynamic_value::Builder<'a>> {
assert_eq!(self.schema.raw, field.parent.raw);
assert!(core::ptr::eq(
self.schema.raw.generic,
field.parent.raw.generic
));
self.set_in_union(field)?;
let ty = field.get_type();
match field.get_proto().which()? {
Expand Down Expand Up @@ -680,7 +698,10 @@ impl<'a> Builder<'a> {
/// Clears a field, setting it to its default value. For pointer fields,
/// this makes the field null.
pub fn clear(&mut self, field: Field) -> Result<()> {
assert_eq!(self.schema.raw, field.parent.raw);
assert!(core::ptr::eq(
self.schema.raw.generic,
field.parent.raw.generic
));
self.set_in_union(field)?;
let ty = field.get_type();
match field.get_proto().which()? {
Expand Down
25 changes: 22 additions & 3 deletions capnp/src/introspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,28 @@ pub struct RawBrandedStructSchema {

impl core::cmp::PartialEq for RawBrandedStructSchema {
fn eq(&self, other: &Self) -> bool {
core::ptr::eq(self.generic, other.generic) && self.field_types == other.field_types
// don't need to compare annotation_types.
// that field is equal iff field_types is.
// In capnproto-c++, this method is implemented as a single pointer
// comparison. That's possible because C++ guarantees unique addresses
// for static template instantiations. A rough equivalent in Rust would be
// to compare the function pointers `self.field_types` and
// `other.field_types`, but doing so triggers a warning:
// > function pointer comparisons do not produce meaningful results
// > since their addresses are not guaranteed to be unique

if core::ptr::eq(self.generic, other.generic) {
return false;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this return true?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! It should be negated, like this:

        if !core::ptr::eq(self.generic, other.generic) {
            return false;
        }

See #550.

}

// Check that all member field types match. This makes sure that
// type parameters have been instantiated the same.
for idx in self.generic.members_by_name {
if (self.field_types)(*idx) != (other.field_types)(*idx) {
return false;
}
}
// To be extra strict, we would need to compare the annotations too.
// Does anyone want that?
true
}
}

Expand Down
Loading