Commit b0ba3d43
b0ba3d43fddbMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
fix(codegen): detect circular inheritance instead of hanging
class A < B; class B < A; end; end (and the trivial self form
class A < A) hung spinel_codegen at 100% CPU. Every parent-walking
helper — cls_find_method, cls_method_return, cls_ivar_type,
is_class_or_ancestor — recurses through @cls_parents, and a cycle
makes that recursion run forever instead of erroring out the way
CRuby does (NameError at parse).
Add a detect_circular_inheritance pass right after class collection.
For each class, walk the parent chain via @cls_parents /
find_class_idx, and bail with Error: circular inheritance ... if
the same name reappears. An unresolved parent stops the walk cleanly
(it's a separate diagnostic handled elsewhere).
Closes #106
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- b0ba3d43fddb
- Parent
- 305e2f6539f2
spinel_codegen.rbmodifiedView file
end # ---- Collection pass ---- # Walk every class's parent chain. A cycle anywhere on the chain is # a fatal program error: bail with a clear message instead of letting # the recursive helpers loop forever. Self-inheritance (`class A < A`) # is detected as the trivial 1-step cycle. def detect_circular_inheritance i = 0 while i < @cls_names.length visited = "".split(",") visited.push(@cls_names[i]) cur = @cls_parents[i] while cur != "" if not_in(cur, visited) == 0 $stderr.puts "Error: circular inheritance involving '" + @cls_names[i] + "' via '" + cur + "'" exit(1) end visited.push(cur) pi = find_class_idx(cur) if pi < 0 # Unresolved parent — stop walking; this is a separate issue # (the parent lookup falls through cleanly elsewhere). break end cur = @cls_parents[pi] end i = i + 1 end end def collect_all root = @root_id if @nd_type[root] != "ProgramNode" collect_class(sid) end } # Pass 1.5: reject circular inheritance (`class A < B; class B < A`). # Every parent-walking helper (cls_find_method, cls_ivar_type, # is_class_or_ancestor, …) recurses through @cls_parents; a cycle # would loop forever and hang the codegen instead of erroring out # like CRuby. Issue #106. detect_circular_inheritance # Pass 2: top-level methods, constants, define_method stmts.each { |sid|