Skip to content

fix: emit compile error for global-scope map instead of segfaulting#202

Merged
cs01 merged 1 commit intomainfrom
fix/global-map-segfault
Mar 11, 2026
Merged

fix: emit compile error for global-scope map instead of segfaulting#202
cs01 merged 1 commit intomainfrom
fix/global-map-segfault

Conversation

@cs01
Copy link
Copy Markdown
Owner

@cs01 cs01 commented Mar 11, 2026

Problem

const m = new Map<string, string>(); m.set("k", "v"); at the top level (global scope) segfaults at runtime. This is because global maps are stored as global %StringMap* null (pointer-to-pointer), but m.set() treats them as direct struct pointers (like local alloca %StringMap). The calling convention mismatch causes memory corruption.

Maps inside functions work fine. Sets at global scope also work fine (different allocation path).

Fix

Emit a clear compile error instead of silently generating wrong code:

error: Map operations at the top level are not supported — wrap in a function
  = help: function main() { const m = new Map(...); ... } main();

This is the safe approach — a proper fix would require changing how global map variables are stored and loaded (using an indirection layer), which is a larger change.

Test plan

  • New fixture: map-global-scope-error.ts (expects compile error)
  • All 460 tests pass

@cs01 cs01 merged commit 136fd72 into main Mar 11, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant