Skip to content

== on an enum or Vec with a str payload compares representation bytes: Option[str] == Some("a") is false #2137

Closed

Description

@ehartford

Spec: §11.7 (== is eq(&other)), §11.8 (derive(Eq) is field-by-field equality).

Repro (installed v0.15.3.0, main fd88e9043; the pinned seed nightly-20261003 behaves the same):

@[derive(Eq)]
enum Tok:
    Name(str)
    Num(i32)
fn main:
    let e: Option[str] = Some("a")
    let f: Option[str] = Some("a")
    print(f"{e == f} {e == Some("a")}")                           // false false
    print(f"{Tok.Name("a".to_lower()) == Tok.Name("a")}")         // false
    let v: Vec[str] = ["a"]
    let w: Vec[str] = ["a"]
    print(f"{v == w}")                                            // false
    let o: Option[&str] = Some("a")
    print(f"{o == Some("a")}")                                    // false

Expected true on every line. Option[i32] == Some(3), a tuple ("a", 1) == ("a", 1) and a struct with a str field all compare correctly.

Where: Codegen.compare_aggregate_eq (src/Codegen.w) decides what == means from the LLVM type: a struct is compared member by member, and an enum is {tag, [payload bytes]}, so its payload is compared as raw bytes — a str payload by its pointer, a Vec by pointer/len/cap. That is codegen reconstructing a meaning Sema owns (D65). derive(Eq) on an enum changes nothing.

Tool that stayed silent: examples/c-interop/test/database_test.w asserts rows.column_text(0).map(t => t.to_str_lossy()) == Some("a") and failed.errmsg().map(...) == Some("unable to open database file"); both fail on main, and no gate runs the example projects' own tests (with build :test in examples/c-interop: 2 of 7 fail). There is no behavior test of Option[str] equality.

Fix owed: Sema decides equality per With type (the Eq impl, or the derived variant-by-variant comparison for enums); codegen lowers that and never falls back to bytes for a type holding a str, Vec or other owned payload. examples-tests runs each example's tests.

Activity

  1. added 3 commits that reference this issue on Oct 5, 2026
  2. ehartford commented on Oct 5, 2026

    @ehartford
    ContributorAuthor

    Fixed in #2143 (main 6b0c99d, installed). == on an aggregate is rendered from its With type: Sema records the eq each part is compared by (specialized for a generic part), codegen emits a per-type walk. Option/Result/enum payloads, Vec, Box, tuples and arrays of them, structs holding them, views of aggregates, Vec.contains/in, and == on a generic type with its own eq all compare by value; an owning temporary used as an operand is dropped (it leaked). examples-tests now runs examples/c-interop and its 13 tests. Test: behav_2137_structural_equality; verified on macOS, Linux x86_64 (gate) and Windows x86_64.

  3. added a commit that references this issue on Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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