gop icon indicating copy to clipboard operation
gop copied to clipboard

feat(cl): add infrastructure for deferred type variables (#2467)

Open niupilot[bot] opened this issue 2 months ago • 1 comments

Summary

Implements the foundational infrastructure for deferred type variables as described in issue #2467.

This PR adds the core data structures and tracking mechanisms needed to support deferred type inference, where variables with untyped literals remain unresolved until their first use.

Changes

Core Infrastructure

  • deferredVar structure (cl/compile.go:364-368): Tracks variables awaiting type resolution

    • Stores variable object, declaration position, and initial expression
  • blockCtx extension (cl/compile.go:430): Added deferredVars map[string]*deferredVar field

    • Initialized in blockCtx creation for both XGo and Go files

Helper Functions (cl/compile.go:370-404)

  • isUntypedLiteral(): Detects untyped literal expressions (BasicLit, empty SliceLit, empty map CompositeLit)
  • addDeferredVar(): Adds variable to deferred resolution queue
  • resolveDeferredVar(): Placeholder for future type resolution logic

Variable Loading (cl/compile.go:1699-1755)

  • Modified loadVars() to detect single-variable declarations with untyped literals
  • Marks eligible variables for deferred type inference
  • Tracks them in the deferredVars map for later resolution

Test Cases

  • cl/_testgop/deferred-int/: Tests basic integer literal deferred inference ✅
  • cl/_testgop/deferred-slice/: Tests empty slice - infrastructure ready for resolution logic

Test Results

=== RUN   TestTestgop/deferred-int
--- PASS: TestTestgop/deferred-int (0.00s)

The deferred-int test passes, confirming the infrastructure correctly tracks deferred variables. The deferred-slice test reveals that the type resolution mechanism (step 2) is needed to properly infer []int from usage context.

Scope & Next Steps

This PR focuses on infrastructure only as specified in the issue requirements:

  • ✅ New deferredVar structure
  • ✅ Deferred type tracking in blockCtx
  • ✅ Symbol table extension
  • ✅ Test cases for simple scenarios

Not included (for future PRs):

  • Actual type resolution logic on first use
  • Detection and handling of variable usage patterns
  • Type inference from context (e.g., x <- 100[]int)

Related

  • Closes #2467
  • Part of #2441 (Untyped Literal Extension - Phase 1: Foundation)

🤖 Generated with codeagent

niupilot[bot] avatar Oct 11 '25 10:10 niupilot[bot]

Codecov Report

:x: Patch coverage is 79.06977% with 9 lines in your changes missing coverage. Please review. :white_check_mark: Project coverage is 93.27%. Comparing base (89b94e1) to head (6e42b8d).

Files with missing lines Patch % Lines
cl/compile.go 79.06% 8 Missing and 1 partial :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2468      +/-   ##
==========================================
- Coverage   93.33%   93.27%   -0.06%     
==========================================
  Files          34       34              
  Lines       11608    11651      +43     
==========================================
+ Hits        10834    10868      +34     
- Misses        609      617       +8     
- Partials      165      166       +1     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov[bot] avatar Oct 11 '25 10:10 codecov[bot]