Skip to content

Codegen: a module-level let whose initializer has control flow (or / and / if) leaves an unterminated block in __with_init_const_* #1483

Closed

Description

@ehartford

Found while fixing #1417 (winapifamily.h's WINAPI_PARTITION_* macros are C predicates over constants). Reproduces on the installed compiler (v0.15.2.1) and on stage1 of main f1e3bd2.

A module-level let whose initializer has control flow — or, and, or an if expression — fails code generation:

let Y: i32 = 100
let Z: bool = (Y == 3) or (Y == 100)
fn main:
    print(f"{Z}")
LLVM verify error
Basic Block in function '__with_init_const_Z' does not have terminator!
label %mir.default.unreachable

error: code generation failed

Same for let X: i32 = if Y == 100: 1 else: 0 (__with_init_const_X). A straight-line initializer works (let Z: bool = Y == 100 prints true). The global need not be referenced: an unused let Z = (Y == 3) or (Y == 100) fails the same way.

Expected: the initializer function's blocks are all terminated, and each program prints its value (true, 1). The mir.default.unreachable block of the lowered switch is emitted without a terminator in the const-init function (not in ordinary fn bodies, where the same expression compiles).

Impact on c_import: a predicate macro that does not fold (#define WINAPI_PARTITION_PC_APP ((WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP) || (WINAPI_FAMILY == WINAPI_FAMILY_PC_APP)) when an operand is not a known constant) now imports as ((...) or (...)) as c_int (#1417) and would hit this. #1417 folds every such macro whose operands are constants, which covers winapifamily.h.

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