Skip to content

LLVM backend: a global's initializer folds with silent wrapping; the same expression panics at run time #1773

Closed

Description

@ehartford

Spec: §4.2.3 (checked arithmetic panics in all builds unless configured), §9.1c (global initializers are ordinary expressions that run before main).

let K: u32 = 1
let T: [2]i16 = [0, (0 - K)]
fn main:
    print(T[1])
    let t: [2]i16 = [0, (0 - K)]
    print(t[1])

with run (base 76cf0bf stage1): prints -1, then panics integer overflow: u32 subtraction wrapped below zero. The global's initializer is folded by the LLVM backend's constant evaluator (CodegenTraits try_eval_const_llvm) with wrapping arithmetic; the identical expression evaluated as an expression panics.

Expected: the global's initializer overflows exactly as the expression does (a compile-time error if the folder evaluates it, or the startup panic).

Found by #1484: the C backend now runs such initializers as ordinary expressions and panics in std.re's escapes table (26 (0 - ESC_*) on c_uint, see the migrator issue filed beside this one), which this folder hides.

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