Skip to content

Add for loop type inference support#58

Merged
dak2 merged 1 commit intomainfrom
add-for-loop-support
Mar 14, 2026
Merged

Add for loop type inference support#58
dak2 merged 1 commit intomainfrom
add-for-loop-support

Conversation

@dak2
Copy link
Owner

@dak2 dak2 commented Mar 14, 2026

Summary

  • Add process_for_node in loops.rs to handle ForNode AST nodes, extracting element types from Array[T] / Range[T] collection literals and registering loop variables in the current scope
  • Add ForNode dispatch in install.rs so for loops are no longer silently ignored
  • Add Rust unit tests (5 cases: return type, variable typing, scope persistence, empty body, method calls) and Ruby integration tests (no-error and type-error detection)

Why

ForNode was completely unhandled in the AST installer — for x in [1, 2, 3] would silently skip type analysis, leaving loop variables untyped. This meant method calls like item.upcase inside for loops could not be validated, creating a gap in type coverage for Ruby code that uses for iteration.

Design decisions

  • Returns nil (consistent with while/until) rather than the collection, since the return value of for is rarely used in practice
  • No new scopefor in Ruby does not create a block scope, so the loop variable correctly persists after the loop
  • Immediate edge propagation (genv.add_edge) for loop variable types, since they are determined at install time from literal collections

Test plan

  • Rust unit tests: test_for_returns_nil, test_for_variable_type_from_array, test_for_variable_persists_after_loop, test_for_empty_body, test_for_with_method_call_in_body
  • Ruby integration tests: test_for_loop_basic_no_error, test_for_loop_detects_type_error

🤖 Generated with Claude Code

ForNode was unhandled in install_node, causing `for x in [1,2,3]`
loop variables to resolve as untyped. This meant method calls on
loop variables (e.g., `item.upcase`) could not be validated, leaving
a gap in type coverage for idiomatic Ruby iteration patterns.

Extract element type from Array[T] / Range[T] collection literals
and register the loop variable in the current scope (no new scope,
matching Ruby's for semantics). Return nil consistent with
while/until handling.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@dak2 dak2 merged commit 74ce1cd into main Mar 14, 2026
2 checks passed
@dak2 dak2 deleted the add-for-loop-support branch March 14, 2026 08:29
@dak2 dak2 mentioned this pull request Mar 15, 2026
4 tasks
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