Add type tracking for variables in compiler where possible #819
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.
This PR implements type tracking for variables in the Ferret compiler, addressing the TODO comment in the codebase and enabling better compile-time type information where it can be determined.
Problem
Previously, all variables in the compiler were assigned
TypeUnknown
, even when the type could be determined at compile time from literal assignments. This made it impossible to leverage type information for optimization or better error reporting.Solution
Added AST-based type detection for literal variable assignments by:
Variable
struct instead of a simple mapRuntimeTypeToValueType
utility function to convert between runtime types and compilerValueType
enumExamples
The compiler now correctly tracks types for literal assignments:
Implementation Details
The implementation follows the principle of "where it's possible" - only detecting types when they can be reliably determined at compile time from the AST, avoiding any runtime overhead or complex type inference.
Testing
All existing tests pass, and new tests validate:
TypeUnknown
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.