Description
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
D41: Ord.cmp(&self, &other) backs <, <=, >, >=; Eq.eq(&self, &other) …
stdlib sourcing Phase 1: the c-algorithms corpus, its upstream tests,…
C algorithms phase1 (#1142)
ehartford commentedon Sep 24, 2026
Also
==: two[]u8views with equal contents compare false (found by D64's zlib UAT rewrite —output[..n] == input as []u8is 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.emit-c: == / != and the orderings on &str views compare the text (#1560)
C backend and modeled-C fixes: map loops, generic structs, view compa…
ehartford commentedon Oct 25, 2026
Triage 2026-10-25,
with v0.15.3.0(main a261b65): does not reproduce; 9c48fe2 (#1789, "view compares") compares throughcmp. With today'scmp(other: &Tag)signature (D140):