Commit 72b7af5f
72b7af5f40e7Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
Merge pull request #533 from ryanseys/rs-fix-data-define-constant
fix(analyze): recognize Data.define(...) constant assignment
Details
- Commit
- 72b7af5f40e7
- Parent
- 6c7ddb812e8c
- Merge parent
- c8c79f153114
spinel_analyze.rbmodifiedView file
@@ -1019,6 +1019,33 @@
10191019
010201020
end10211021
1022+
# True iff `call_nid` is `<recv_name>.<method_name>(...)` where the1023+
# receiver is a bare ConstantReadNode (no nesting). Used to detect1024+
# `Struct.new(:a, :b)` and `Data.define(:a, :b)` constant-assignment1025+
# shapes that both lower to `collect_struct_class`.1026+
def is_call_on_const(call_nid, recv_name, method_name)1027+
if call_nid < 01028+
return 01029+
end1030+
if @nd_type[call_nid] != "CallNode"1031+
return 01032+
end1033+
if @nd_name[call_nid] != method_name1034+
return 01035+
end1036+
sr = @nd_receiver[call_nid]1037+
if sr < 01038+
return 01039+
end1040+
if @nd_type[sr] != "ConstantReadNode"1041+
return 01042+
end1043+
if @nd_name[sr] != recv_name1044+
return 01045+
end1046+
11047+
end1048+
10221049
def constructor_class_name(recv_nid)10231050
if recv_nid < 010241051
return ""@@ -6783,18 +6810,18 @@
67836810
end67846811
expr_id = @nd_expression[nid]67856812
if expr_id >= 06786-
if @nd_type[expr_id] == "CallNode"6787-
if @nd_name[expr_id] == "new"6788-
sr = @nd_receiver[expr_id]6789-
if sr >= 06790-
if @nd_type[sr] == "ConstantReadNode"6791-
if @nd_name[sr] == "Struct"6792-
collect_struct_class(cname, expr_id)6793-
return6794-
end6795-
end6796-
end6797-
end6813+
# `Foo = Struct.new(:a, :b)` and `Foo = Data.define(:a, :b)` share6814+
# a structural contract: positional symbol args naming the fields.6815+
# Data is keyword-init at runtime (`Foo.new(a: 1, b: 2)`), which6816+
# the existing kwarg constructor path already matches by name.6817+
# Routing both through `collect_struct_class` registers the6818+
# synthetic class so codegen has a `sp_<Foo>` type to emit;6819+
# without this the AST-emitted `obj_<Foo>` reference resolves to6820+
# an undeclared struct, tripping clang `unknown type name6821+
# 'sp_<Foo>'`.6822+
if is_call_on_const(expr_id, "Struct", "new") == 1 || is_call_on_const(expr_id, "Data", "define") == 16823+
collect_struct_class(cname, expr_id)6824+
return67986825
end67996826
end68006827
ct = "int"test/data_define.rbaddedView file
Loading diff…
test/data_define.rb.expectedaddedView file
Loading diff…