Skip to content

Comparing two views of a type without an 'lt' method silently compares addresses #1137

Closed

Description

@ehartford

a < b where a and b are &Tag and Tag defines Ord.cmp but no lt method compiles without a diagnostic and lowers to a raw pointer comparison of the two references. §11.7 says comparison operators use the fixed method names (lt, ...) on the concrete type; when the type has none, the operand types are &Tag, and check_binary's comparison fallthrough (src/SemaCheck.w, the lhs_ptr_like branch: TY_REF counts as pointer-like) accepts the operands as pointers. MIR then carries binop(lt, copy _3, copy _7) on the two references and codegen emits cmp x1, x2 (the closure in the repro below disassembles to exactly that). &i32 operands are materialized to values first (D22 contextual Copy), so only non-Copy targets hit it.

Repro (out/bootstrap/bin/with-stage1 run):

use std.builtins.print_i32
type Tag { id: i32, pad: i64 }
impl Ord for Tag:
    fn cmp(other: Tag) -> i32:
        if self.id < other.id: -1 else if self.id > other.id: 1 else: 0
fn order[T: Ord](a: &T, b: &T) -> i32: if a < b: -1 else if a > b: 1 else: 0
fn plain(a: &Tag, b: &Tag) -> i32: if a < b: -1 else if a > b: 1 else: 0
fn main:
    let x = Tag { id: 20, pad: 0 }
    let y = Tag { id: 10, pad: 0 }
    print_i32(order(&x, &y))
    print_i32(order(&y, &x))
    print_i32(plain(&x, &y))
    print_i32(plain(&y, &x))

Prints -1 1 -1 1 (the declaration order of x and y, i.e. their addresses); 1 -1 1 -1 is the value order. Adding if x < y: 1 else: 0 on the owned values fails differently: LLVM function verification failed after MIR cleanup for main (a 16-byte struct compared by value; also silent about which instruction).

Expected under the current spec: a diagnostic that Tag has no lt (an Ord bound does not provide <), never an address comparison of &T; address comparison is for raw pointers (§16). Found while writing the Phase 1 c-algorithms facades (docs/stdlib_sourcing_plan.md): every ordered facade compares &T views inside its comparator, and every "correct" probe result before the disassembly was address-order luck.

Related design question for Eric, surfaced in the Phase 1 report: whether < should be backed by Ord.cmp (one method giving all six comparisons, with other: &Self so views compare without copies) instead of six fixed method names.

Activity

  1. ehartford commented on Sep 24, 2026

    @ehartford
    ContributorAuthor

    Also ==: two []u8 views with equal contents compare false (found by D64's zlib UAT rewrite — output[..n] == input as []u8 is what an application developer writes, and it silently answers by address). Repro: let a: [u8; 3] = [1,2,3]; let b: [u8; 3] = [1,2,3]; let x: []u8 = a[..]; let y: []u8 = b[..]; print(x == y) prints false. The UAT fixtures compare byte by byte until this is fixed.

  2. ehartford commented on Oct 25, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): does not reproduce; 9c48fe2 (#1789, "view compares") compares through cmp. With today's cmp(other: &Tag) signature (D140):

    $ with run main2.w   # order(&x,&y), order(&y,&x), plain(&x,&y), plain(&y,&x); x.id=20, y.id=10
    1
    -1
    1
    -1
    
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