Skip to content

Sema: implicit integer narrowing is diagnosed only for an annotated let; sequence/tuple/struct literal elements, call arguments, return values and global initializers narrow silently #1803

Closed

Description

@ehartford

Spec: §4.2 (types.md, "Implicit narrowing is a compile error": let y: u8 = x with x: u32 is an error).

Found while fixing #1773. Only check_binding_annotation (an annotated local let) runs int_narrowing_requires_cast; every other destination that demands an integer type accepts a wider or differently-signed value and truncates it at runtime. Verified on with v0.15.2.1-g99939352f, each with let K: u32 = 70000, type S { a: i16 }, fn take(x: i16), fn give() -> i16: K + 1:

destination program verdict
annotated local let let x: i16 = K + 1 error (correct)
sequence literal element let t: [2]i16 = [0, K + 1] accepted, t[1] reads 4465
Vec literal element let v: Vec[i16] = [0, K + 1] accepted
tuple literal element let t: (i16, i16) = (0, K + 1) accepted
struct literal field S { a: K + 1 } accepted
call argument take(K + 1) accepted
return value fn give() -> i16: K + 1 accepted
annotated global let top-level let c: i16 = K + 1 accepted (check_top_level_let_values never calls check_binding_annotation)

Also noted: let x: u8 = 1 + 2 is an error today (the literal arithmetic defaults to i32 and then "narrows"), so the migrated pcre2 corpus's let coptable: [173]u8 = [..., (1 + 2), ...] (lib/std/re/pcre2_dfa_match.w) only compiles through the sequence-literal gap — closing the gap needs either literal arithmetic to adopt the expected type or the migrator to spell the element cast, and then a re-migration.

Expected: one narrowing rule at the owner (Sema) for every owned-integer demand, per §4.2; the corpora and any tree code that relied on the gap get an explicit as (from the migrator, never by hand for migrated output).

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