Skip to content

u8 sign-extends on implicit widening and as i64 when it comes from a materialized local or a str index (spec §1323: unsigned operands zero-extend) #1017

Closed

Description

@ehartford

Found 2026-09-03 while landing the tree-wide byte_at(i) → s[i] rewrite: the self-hosted compiler and the seed disagree on a comptime byte comparison, and the cause is a pre-existing widening bug present in the seed (v0.15.1.7-g120331037) too.

Repro

use std.builtins.print_i64
fn take(x: i64) -> i64: x
fn main:
    var v: Vec[u8] = Vec.new()
    v.push(255 as u8)
    let b: u8 = v[0]
    let w: i64 = b
    print_i64(w)            // 255   correct
    print_i64(take(b))      // -1    WRONG (sign-extended)
    print_i64(take(v[0]))   // 255   correct (element place used directly)
    let s = "\xff"
    print_i64(take(s[0]))   // -1    WRONG
    print_i64(s[0] as i64)  // -1    WRONG
    let sb = s[0]
    print_i64(take(sb))     // -1    WRONG
    let e = v[0]
    print_i64(take(e))      // 255   correct (Vec[u8] element view)
    let c: u8 = 200 as u8
    print_i64(take(c))      // -56   WRONG

Identical output from the seed, from stage1 built by the seed, and from the self-built release binary, so it is not a self-hosting divergence: coerce_int (src/Codegen.w:2422) defaults is_unsigned to false and sign-extends any narrower integer, and the call-argument / as paths reach it without the source Sema type's signedness (mir_coerce_value_to_sema_type only passes src_unsigned when a caller supplies it). Widening through a let w: i64 = b binding is correct, so one path knows the source type and the others do not.

Spec §1323: "Unsigned operands zero-extend; signed …". byte_at returned an i32 that the runtime zero-extended, which is why this never showed on byte scanning code until the rewrite spelled it as s[i].

Expected

Every widening of an unsigned source zero-extends regardless of how the value is spelled: element place, view binding, materialized u8 local, str index, call argument, as cast. The single ABI-descriptor discipline applies here too: one place decides signedness from the source Sema type; no per-path guess.

Consequence

The byte_at → s[i] sweep (uncast-sweep branch) is blocked on this: a byte ≥ 128 compared after implicit widening flips. The comptime evaluator's Vec[u8].get (recv_value.text[index] as i64) is the site that broke test/selfhost build-w-toolfs-ok.

Activity

  1. ehartford commented on Sep 4, 2026

    @ehartford
    ContributorAuthor

    Fixed on main: af7db8c — every widening takes signedness from the operand's Sema type (call arguments, cached locals, loaded places, casts, views), and s[i] is typed u8 in Sema/MIR/codegen/comptime (it was tabled i32, byte_at's old return type, which also made let b = s[i] a &i32 view over one byte). Pin: test/behavior/behav_u8_widening_zero_extends.w. Battery green, reseeded at af7db8c.

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