Skip to content

codegen: infc panics on duplicate function-local name across sibling blocks (type-checker gap) #217

@0xGeorgii

Description

@0xGeorgii

Summary

infc panics (compiler crash) during codegen when the same function-local name is declared in two disjoint sibling blocks. The type-checker accepts the program; codegen then hits an internal assertion.

thread 'main' panicked at core/wasm-codegen/src/compiler.rs:1280:21:
local `x` collides with an existing entry in locals_map; the type-checker should have rejected shadowing

Reproduction

pub fn f(c: bool) -> i64 {
    if c { let x: i64 = 1; return x; }
    if !c { let x: i64 = 2; return x; }   // same name, disjoint block
    let z: i64 = 0;
    return z;
}
$ infc repro.inf
Parsed: repro.inf
Analyzed: repro.inf
thread 'main' panicked at core/wasm-codegen/src/compiler.rs:1280:21:
local `x` collides with an existing entry in locals_map; the type-checker should have rejected shadowing

Contrast — same-scope redeclaration IS caught cleanly

pub fn f() -> i64 { let x: i64 = 1; let x: i64 = 2; return x; }
Type checking failed: 3:5: error registering variable `x`: Variable `x` already declared in this scope

Analysis

The type-checker rejects re-declaration per lexical block, but codegen flattens all locals of a function into one locals_map. Two same-named lets in disjoint blocks therefore pass type-checking but collide in codegen, which panic!s — and the panic message itself notes the type-checker should have rejected it. A normal program pattern (reusing a name in non-overlapping branches) crashes the compiler instead of producing a diagnostic.

It was hit in practice when a function declared the same temporary (dir) in three disjoint material branches; the workaround is to give every local a unique name within a function.

Suggested fix

Either:

  • make the type-checker treat function-local names as a flat namespace (reject any duplicate let name within a function, with a proper diagnostic), or
  • make codegen scope/rename locals per block so disjoint shadowing is legal.

Related: #167 (same pre_scan_locals / locals subsystem, future-desync risk), #172.


Environment: infc 0.0.1 (commit df45600, ABI 1.0), Inferara/inference @ d82d935 (wasm-linker). Found while implementing a fixed-point ray tracer entirely in Inference (scratch/raytracing-in-one-weekend).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcodegenBytecode emitting

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions