fix module-scope variables defaulting to double instead of actual type#48
Merged
fix module-scope variables defaulting to double instead of actual type#48
Conversation
d96d95f to
6ee4b63
Compare
6ee4b63 to
2b4ee91
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix module-scope variables defaulting to
doubleinstead of their actual type, causing LLVMoptcrashes.Problem
Top-level variable declarations like
const parts = "hello".match(/re/)crashed during LLVM optimization with type mismatches:The same code worked fine when wrapped in a function.
Root cause
Module-scope variables go through a two-phase process:
generateGlobalVariableDeclarations()emits@name = global <type> <default>allocate()in@mainstores the expression result into the pre-declared globalPhase 1 has an incomplete type classifier (~12 cases) compared to the function-local path which handles 25 cases via
classifyVariable(). When it couldn't determine the type, it fell back todouble— so.match(),.split(), function calls returning strings, etc. all became@name = global double 0.0. Phase 2 then tried tostore i8* %expr, double* @name, causing the type mismatch.Additionally, struct-value globals (
%StringArray,%Array,%Map,%Set) were declared as by-value structs withzeroinitializer, while the function-local path correctly uses pointer types — another source of mismatches.Fix
%StringArray->%StringArray*,%Array->%Array*,%Map->%Map*, etc.)doublefallback with a compile error for unclassified expression types — instead of silently guessing the wrong type and crashing later inopt, the compiler now tells you:cannot determine type of module-scope variable 'x'. Move the declaration inside a function, or add a type annotationconst x = 42) still correctly default todoubleTest plan
tests/fixtures/globals/(module-scope.match(),.split(), function return)