Skip to content

Silent wrong value: &i64 view + integer literal is computed at i32 (truncates; checked op panics); print(view + 1) fails codegen #1477

Closed

Description

@ehartford

Summary

Arithmetic between a &i64 view and an unsuffixed integer literal is computed at i32: the i64 operand is truncated, the result is wrong (or the checked op panics). Silent wrong value.

Repro (installed nightly v0.15.2.1; also the pinned seed; origin/main ba2d5d1 stage1)

fn f(p: &i64) -> i64: p + 1

fn main:
    let v: Vec[i64] = Vec.new()
    v.push(3000000000)
    let p = v[0]
    let q = p + 1
    print(q)
    print(f(3000000000))

Prints -1294967295 twice; expected 3000000001 twice.

Matrix (each with the element 3000000000):

form result
let p = v[0]; let q = p + 1 -1294967295
let q: i64 = p + 1 (annotated) -1294967295
let p = v.get(0); p + 1 -1294967295
v[0] + 1 -1294967295
fn f(p: &i64) -> i64: p + 1 -1294967295
p * 2 panic: integer overflow: i32 multiplication out of range
2147483647 element, p + 1 panic: integer overflow: i32 addition out of range
for e in v: e + 1 3000000001 (correct)
let one: i64 = 1; p + one 3000000001 (correct)

So the literal takes i32 instead of the pointee's type when the other operand is a &i64 view, and the operation is emitted at i32.

Separate but adjacent: print(p + 1) with p = v[0] fails code generation: error: wrong argument type actual=i64 expected=ptr fn=main.

Expected

§4.6 / D22: value arithmetic sees through a &T view to T; an unsuffixed literal takes the other operand's type. p + 1 is an i64 addition.

Workaround used

src/SemaDecl.w (discriminant auto-increment, #1451): let prev_disc: i64 = disc_vals[vi - 1] — an annotated owned binding instead of a view. Found while fixing #1451; the compiler panicked computing prev + 1 for an i64 discriminant at 2147483647.

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