Commit c8c79f15

c8c79f153114Merged

Ryan Seys <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(analyze): recognize Data.define(...) constant assignment

Foo = Data.define(:a, :b) has the same structural contract as Foo = Struct.new(:a, :b) -- positional symbol args naming the fields -- but the constant-assignment path in spinel_analyze only matched Struct.new. Without recognition the synthetic sp_<Foo> type never registers and codegen emits obj_<Foo> against an undeclared C struct, tripping clang unknown type name 'sp_<Foo>'.

Both checks now share a new predicate is_call_on_const(call_nid, recv_name, method_name) that collapses the five-level nested ifs into a single conditional. The dispatch becomes:

if is_call_on_const(expr_id, "Struct", "new") == 1 ||
   is_call_on_const(expr_id, "Data", "define") == 1
  collect_struct_class(cname, expr_id)
  return
end

Data's keyword-init constructor (Foo.new(a: 1, b: 2)) already matches the existing kwarg path by name, so no runtime changes are needed -- only the analyzer's class registration.

Details

Commit
c8c79f153114
Parent
992c82eada39

spinel_analyze.rbmodifiedView file

@@ -1019,6 +1019,33 @@
10191019 0
10201020 end
10211021
1022+ # True iff `call_nid` is `<recv_name>.<method_name>(...)` where the
1023+ # receiver is a bare ConstantReadNode (no nesting). Used to detect
1024+ # `Struct.new(:a, :b)` and `Data.define(:a, :b)` constant-assignment
1025+ # shapes that both lower to `collect_struct_class`.
1026+ def is_call_on_const(call_nid, recv_name, method_name)
1027+ if call_nid < 0
1028+ return 0
1029+ end
1030+ if @nd_type[call_nid] != "CallNode"
1031+ return 0
1032+ end
1033+ if @nd_name[call_nid] != method_name
1034+ return 0
1035+ end
1036+ sr = @nd_receiver[call_nid]
1037+ if sr < 0
1038+ return 0
1039+ end
1040+ if @nd_type[sr] != "ConstantReadNode"
1041+ return 0
1042+ end
1043+ if @nd_name[sr] != recv_name
1044+ return 0
1045+ end
1046+ 1
1047+ end
1048+
10221049 def constructor_class_name(recv_nid)
10231050 if recv_nid < 0
10241051 return ""
@@ -6783,18 +6810,18 @@
67836810 end
67846811 expr_id = @nd_expression[nid]
67856812 if expr_id >= 0
6786- if @nd_type[expr_id] == "CallNode"
6787- if @nd_name[expr_id] == "new"
6788- sr = @nd_receiver[expr_id]
6789- if sr >= 0
6790- if @nd_type[sr] == "ConstantReadNode"
6791- if @nd_name[sr] == "Struct"
6792- collect_struct_class(cname, expr_id)
6793- return
6794- end
6795- end
6796- end
6797- end
6813+ # `Foo = Struct.new(:a, :b)` and `Foo = Data.define(:a, :b)` share
6814+ # a structural contract: positional symbol args naming the fields.
6815+ # Data is keyword-init at runtime (`Foo.new(a: 1, b: 2)`), which
6816+ # the existing kwarg constructor path already matches by name.
6817+ # Routing both through `collect_struct_class` registers the
6818+ # synthetic class so codegen has a `sp_<Foo>` type to emit;
6819+ # without this the AST-emitted `obj_<Foo>` reference resolves to
6820+ # an undeclared struct, tripping clang `unknown type name
6821+ # 'sp_<Foo>'`.
6822+ if is_call_on_const(expr_id, "Struct", "new") == 1 || is_call_on_const(expr_id, "Data", "define") == 1
6823+ collect_struct_class(cname, expr_id)
6824+ return
67986825 end
67996826 end
68006827 ct = "int"