Skip to content

Sema: an unannotated const whose initializer is a cast or a wrapping operation is typed i64 (const B = (0 as u32) reads as i64) #1836

Closed

Description

@ehartford

Spec: §4.6 type inference / §17 const (a binding's type is its initializer's type).

Found while fixing #1820/#1803. Verified on the fix-emitc stage1 (a1f82be6) and on the fix-literals stage1:

const A = 5u32
const B = (0 as u32)
const C = 1u32 +% 2u32
let D = (0 as u32) -% 1
fn main:
    let a: u32 = A   // ok
    let b: u32 = B   // error: implicit integer narrowing or sign change (B is i64)
    let c: u32 = C   // error: same (C is i64)
    let d: u32 = D   // ok

Expected: B and C are u32, as the let D is. The const's type comes from somewhere other than the checked initializer's type (check_top_level_let_values records u32 for the value), so a cast or a wrapping operation in a const initializer loses its type.

Workaround used: lib/std/zlib.w spells const ZLIB_MAX_CHUNK: c_uint = (0 as c_uint) -% 1 (the #1803 rule now refuses the i64 in if left > ZLIB_MAX_CHUNK: ZLIB_MAX_CHUNK else: left as c_uint under the u32 field demand); remove the annotation when this is fixed.

Also noted: check_top_level_let_values (src/ComptimeEval.w) returns at the first global-initializer error (if self.diags.has_errors(): return), leaving every later global untyped; a single error in one module's global produced "missing return" and "bitwise operator requires integer operands" in unrelated modules (src/compiler/Frontend.w, src/CImport.w) during the #1803 sweep.

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