Skip to content

checker: Detection of circular type references#26162

Merged
spytheman merged 1 commit into
vlang:masterfrom
fleximus:fix_24511
Dec 27, 2025
Merged

checker: Detection of circular type references#26162
spytheman merged 1 commit into
vlang:masterfrom
fleximus:fix_24511

Conversation

@fleximus

Copy link
Copy Markdown
Member

Issue: V incorrectly permitted circular type references in sum types like:
type Type6 = Type7 | int
type Type7 = Type6 | string

Root Cause: The checker only detected direct self-references (type A = A | int) but not indirect cycles through other sum types.

Fix:

  1. vlib/v/checker/checker.v

Added cycle detection for sum types:

  • Added an else if branch to check when a variant is itself a sum type
  • Added a new helper function sumtype_has_circular_ref() that recursively traverses sum type variants to detect cycles
  • Uses a visited map to prevent infinite recursion during cycle detection
  1. New test files:
  • vlib/v/checker/tests/sumtype_circular_reference_err.vv - Test case with circular reference
  • vlib/v/checker/tests/sumtype_circular_reference_err.out - Expected error output

Verification:

The fix correctly detects:

  • 2-type cycles: Type6 → Type7 → Type6
  • 3-type cycles: TypeA → TypeB → TypeC → TypeA
  • Deeper indirect cycles

Non-circular sum types that reference other sum types still work correctly.

Fixes #24511

@fleximus fleximus changed the title Fix: Detection of circular type references checker: Detection of circular type references Dec 26, 2025
1. vlib/v/checker/checker.v

  Added cycle detection for sum types:
  - Added an else if branch to check when a variant is itself a sum type
  - Added a new helper function sumtype_has_circular_ref() that recursively traverses sum type variants to detect cycles
  - Uses a visited map to prevent infinite recursion during cycle detection

2. New test files:

  - vlib/v/checker/tests/sumtype_circular_reference_err.vv - Test case with circular reference
  - vlib/v/checker/tests/sumtype_circular_reference_err.out - Expected error output
@spytheman

Copy link
Copy Markdown
Contributor

(rebased over current master, to force a recheck in the CI jobs running on windows)

@spytheman spytheman merged commit a640ddc into vlang:master Dec 27, 2025
84 checks passed
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.

The language allows circular type references

2 participants