Skip to content

An integer literal at a distinct-integer demand must coerce: fn none() -> CiStmtId: 0 is refused ("return type mismatch"), so the tree casts 0 as CiStmtId 690 times #2567

Closed

Description

@ehartford

Repro

type Id = distinct i32
fn none() -> Id: 0
fn f(x: Id) -> bool: x != 0
fn main: if f(3 as Id): print("ok")
error: return type mismatch
 --> idcmp2.w:2:18
2 | fn none() -> Id: 0
  |                  ^

The comparison x != 0 is accepted (the literal takes the distinct type from the other operand), but the same literal at a declared return type is refused, and so is 3 at a parameter of type Id.

Expected

The declared return type is the demand, the same way an annotated binding, a parameter or a field is: an integer literal at a distinct integer demand takes that type. Spec §3 (defaults pick representations, never meanings); mission: never write what the compiler already knows. Eric (2026-10-22): a declared return type is exactly the case where the function states the type instead of the body casting.

Cost today

src/CImport.w alone carries 690 casts of the shape 0 as CiStmtId / as CiExprId / as CiTypeId, nearly all return 0 as CiStmtId under a declared -> CiStmtId. The matching compare-side ceremony, (x as i32) != 0 (449 sites across the migrator files), compiles without the cast today and is swept separately.

Fix

Sema's literal typing at a demand: when the demanded type is a distinct integer type whose base accepts the literal, the literal has the distinct type (return position, parameter, binding annotation, field initializer, match arm value). Then sweep the 690 casts with a compiler-driven rewrite and add the pattern to the ceremony census.

Activity

  1. ehartford commented on Oct 23, 2026

    @ehartford
    ContributorAuthor

    Fixed in #2580 (c7fa606), D153: an unsuffixed literal where a distinct type over a number is demanded takes the distinct type (return, binding, parameter, field, negation, float distinct, untyped constant), fit-checked at the underlying width; codegen lowers the constant at the representation. Fixtures behav_2567, err_2567. The 690-cast sweep in src/CImport.w is a follow-up.

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