Skip to content

Enhance raw_eq intrinsic to handle more complex cases #666

@Stevengre

Description

@Stevengre

Current State

The raw_eq intrinsic implementation (#665) currently handles the simple case where References point to values of the same type. The implementation dereferences References and uses K's built-in equality operator for comparison.

Enhancement Needed

The intrinsic needs to be extended to handle more complex scenarios:

1. Different Types with Same Memory Representation

  • References to i32 and u32 (same bit pattern, different interpretation)
  • References to different but memory-compatible types
  • Need to determine the correct semantics for cross-type comparison

2. Composite Types

  • Structs: Need to compare field-by-field or as raw bytes
  • Arrays: Element-wise or byte-wise comparison
  • Enums: Discriminant and payload comparison
  • Tuples: Component-wise comparison

3. Memory Layout Considerations

  • Different alignments between types
  • Padding bytes in structs
  • Endianness (if relevant)

Proposed Solution

Implement a byte-level comparison mechanism:

  1. Add a #toBytes function that converts any Value to its byte representation
  2. Compare the byte arrays for equality
  3. Handle size mismatches appropriately

Test Cases Needed

  • Same primitive type (already done: raw_eq_simple)
  • Different integer types with same size
  • Structs with same layout
  • Structs with padding
  • Arrays of primitives
  • Nested composite types
  • References with different sizes (should return false)
  • Zero-sized types

Implementation Notes

The Rust documentation for raw_eq states it performs "typed, but unsized equality comparison" which suggests:

  • Type information is used to determine size
  • The actual comparison is byte-wise
  • Padding bytes may or may not be compared (implementation-defined)

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions