Commit 9fd1a3b9

9fd1a3b99993Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified

Description

fix(codegen,analyze): for-loop with multi-variable target unpacks element

for a, b in [[1, 2], [3, 4]] previously left a/b undeclared and emitted a singular-var loop using a synthetic i name -- the generated C referenced lv_a, lv_b, and lv_i that no one declared, so compilation failed.

  • analyze: scan_locals_first_type's ForNode arm now recognises MultiTargetNode and adds each inner LocalVariableTargetNode to the locals list. Per-target type is the element-array's element type (int for int_array / sym_array, string for str_array, float for float_array, poly otherwise).
  • codegen: compile_for_stmt's array path detects MultiTargetNode and emits sp_<Inner>Array *etmp = sp_<Outer>Array_get(coll, i); lv_a = sp_<Inner>Array_get(etmp, 0); lv_b = ...; per target.

Tests: default 452/0/0, promote 452/0/0.

Closes #678

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
9fd1a3b99993
Parent
c41310f8b23f

spinel_analyze.rbmodifiedView file

@@ -25804,23 +25804,23 @@
2580425804 if @nd_type[nid] == "ForNode"
2580525805 tgt = @nd_target[nid]
2580625806 if tgt >= 0
25807+ # Infer element type from collection (the value bound on each iter)
25808+ elem_type = "int"
25809+ coll = @nd_collection[nid]
25810+ if coll >= 0
25811+ ct = infer_type(coll)
25812+ if ct == "str_array"
25813+ elem_type = "string"
25814+ elsif ct == "float_array"
25815+ elem_type = "float"
25816+ elsif is_ptr_array_type(ct) == 1
25817+ elem_type = ptr_array_elem_type(ct)
25818+ end
25819+ end
2580725820 if @nd_type[tgt] == "LocalVariableTargetNode"
2580825821 lname = @nd_name[tgt]
2580925822 if not_in(lname, names) == 1
2581025823 if not_in(lname, params) == 1
25811- # Infer element type from collection
25812- elem_type = "int"
25813- coll = @nd_collection[nid]
25814- if coll >= 0
25815- ct = infer_type(coll)
25816- if ct == "str_array"
25817- elem_type = "string"
25818- elsif ct == "float_array"
25819- elem_type = "float"
25820- elsif is_ptr_array_type(ct) == 1
25821- elem_type = ptr_array_elem_type(ct)
25822- end
25823- end
2582425824 names.push(lname)
2582525825 types.push(elem_type)
2582625826 @scan_literal_flags.push("")
@@ -25829,6 +25829,39 @@
2582925829 end
2583025830 end
2583125831 end
25832+ # `for a, b in coll` destructures each element into multiple LVs.
25833+ # Each LV holds an entry of the element-array, so its type is the
25834+ # element-array's own element type.
25835+ if @nd_type[tgt] == "MultiTargetNode"
25836+ inner_type = "int"
25837+ if elem_type == "int_array" || elem_type == "sym_array"
25838+ inner_type = "int"
25839+ elsif elem_type == "str_array"
25840+ inner_type = "string"
25841+ elsif elem_type == "float_array"
25842+ inner_type = "float"
25843+ else
25844+ inner_type = "poly"
25845+ end
25846+ mt_kids = parse_id_list(@nd_targets[tgt])
25847+ k = 0
25848+ while k < mt_kids.length
25849+ kid = mt_kids[k]
25850+ if @nd_type[kid] == "LocalVariableTargetNode"
25851+ kname = @nd_name[kid]
25852+ if not_in(kname, names) == 1
25853+ if not_in(kname, params) == 1
25854+ names.push(kname)
25855+ types.push(inner_type)
25856+ @scan_literal_flags.push("")
25857+ @scan_empty_flags.push("")
25858+ @scan_empty_hash_flags.push("")
25859+ end
25860+ end
25861+ end
25862+ k = k + 1
25863+ end
25864+ end
2583225865 end
2583325866 end
2583425867 # Rescue reference (=> e) needs to be declared as a local. The