Skip to content

JS backend silently merges distinct defs whose names mangle to the same identifier (C lane refuses the same program) #790

Closed

Description

@docxology

Summary

js_sat (comp.ts:2895) mangles def names to JS identifiers by replacing only [./~] with $. Distinct names can therefore collide — e.g. a user def helper.map.0 and a minted template instance helper.map~0 both become $helper$map$0$. The C lane detects exactly this and refuses to build ("two names mangle to", comp.ts:2751); js_lib has no equivalent check, so the two defs silently share one function and the later declaration wins.

What you did

# g3c.bend
import Base

def inc(x: Nat) -> Nat:
  1n + x

def helper.map(~f: Nat -> Nat, x: Nat) -> Nat:
  f(x)

def caller() -> Nat:
  helper.map(~inc, 1n)

def helper.map.0(f: Nat -> Nat, x: Nat) -> Nat:
  999n

def main() -> IO(Unit):
  do IO<Unit>:
    IO.print(Nat.show(caller()))
    IO.print(Nat.show(helper.map.0(inc, 1n)))

Build for both backends and run.

What happened

  • C backend: prints 2 then 999 — correct, the two defs stay distinct.
  • JS backend: prints 2, then crashes with TypeError: Invalid mix of BigInt and other type in division. — one function now serves two different defs, so one of the call sites executes the wrong body.

Also reproducible with module names (net/irc vs net.irc, bend.ts canonical names) and any user def whose name matches template~N.

Expected

Either run the same duplicate detection in js_lib (die on collision, mirroring compile_tables) or make js_sat injective (encode separators positionally).

Version / system

  • bend 2.0.5 (main @ e6676b0)
  • Darwin arm64

Activity

  1. VictorTaelin commented on Sep 18, 2026

    @VictorTaelin
    Contributor

    Fixed in 2.0.6: the JS emitter now refuses two defs whose names mangle to the same identifier ("two names mangle to $helper$map$0$"), mirroring the C lane's check. Your helper.map.0 vs the minted helper.map~0 was exactly the collision.

    Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.

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