feat: implement full 'for' loop support and strict type narrowing checks#294
Merged
LunaStev merged 1 commit intowavefnd:masterfrom Feb 23, 2026
Conversation
This commit introduces comprehensive support for C-style 'for' loops,
overhauls the parser's error reporting mechanism, and enforces strict
type safety by forbidding implicit integer narrowing.
Changes:
- **For Loop Implementation**:
- **Parser**: Updated `StatementNode::For` to support local variable
declarations in the initialization block (e.g., `for (var i: i32 = 0; ...)`).
- **Codegen**: Implemented `gen_for_ir` in the LLVM backend, including
proper scope management for loop-local variables.
- **Validation**: Added verification passes to ensure correct scoping
and type safety within for-loop headers.
- **Strict Type Safety**:
- **Narrowing Prevention**: Explicitly forbidden implicit integer
narrowing (e.g., assigning `i64` to `i32`) in assignments, binary
operations, and function arguments.
- **Literal Inference**: Enhanced numeric literal type inference to
be context-dependent, relying on hints from the other side of
binary expressions or explicit type context.
- **Parser Improvements**:
- **Result-based Parsing**: Refactored the `parse` function to return
`Result<Vec<ASTNode>, ParseError>` instead of `Option`, enabling
detailed error messages for syntax and semantic failures.
- **Import Handling**: Updated `import.rs` to propagate structured
parsing errors with source labels and helpful hints.
- **Standard Library & Tooling**:
- **Stdlib Refactoring**: Cleaned up the standard library structure by
removing redundant umbrella files and moving to role-based module
imports (e.g., `std::time::clock`).
- **Documentation**: Updated `std/README.md` with a strict dependency
policy regarding `extern(c)` usage.
- **Optimization**: Aligned backend optimization behavior by mapping
`-Ofast` to `-O3` and normalizing flags across the LLVM pass pipeline.
- **Testing**:
- Updated test cases to reflect strict type checking and new standard
library import paths.
These changes significantly improve the language's expressiveness while
ensuring a higher level of compile-time safety.
Signed-off-by: LunaStev <luna@lunastev.org>
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.
This PR significantly enhances the Wave language by introducing full support for C-style
forloops and enforcing stricter type safety across the board. Key improvements include a transition to aResult-based error reporting system in the parser and the prevention of unsafe implicit integer narrowing.Key Changes
1. Full
forLoop Implementationforloop now supports local variable declarations within the initialization block (e.g.,for (var i: i32 = 0; i < 10; i++)).gen_for_irin the LLVM backend with proper scope management. Variables declared in the loop header are automatically cleaned up when the loop exits.2. Strict Type Safety & Narrowing Prevention
i64value to ani32variable) is now a compiler error. This prevents subtle data loss bugs during assignments, binary operations, and function calls.3. Parser Robustness & Error Reporting
Result-based Parsing: Refactored the internal parser API fromOption<T>toResult<T, ParseError>. This allows the compiler to provide detailed diagnostic messages, including source labels, line numbers, and "help" hints.4. Standard Library & Optimization
std::time::clock).-Ofastflag now correctly maps to-O3logic, and all flags are consistently applied through the new LLVM PassBuilder.Example Usage
Standard
forloop with local scope:Benefits
forloops provide a more ergonomic way to handle iterations compared towhileloops.Result-based parsing provides a much better developer experience through clearer error messages.