Commit afb3ee23

afb3ee23dd49Merged

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

Description

feat(parse,codegen): support KeywordRestParameterNode declaration (def f(**kw))

Adds the kwrest slot to method signatures. The collect_pnames / collect_ptypes / collect_defaults walkers now recognize **kw between the rest and block slots and emit a sym_poly_hash typed parameter (matches f(a: 1) keyword-hash construction at call sites). Anonymous ** synthesizes __anon_kwrest so future forwarding desugar (def f(...)) can name the slot deterministically.

Also registers the @nd_keyword_rest and @nd_posts AST slots in the codegen's reader so the new fields the parser emits (folded into this commit) land in the right places.

Body-side methods on a kwrest local (kw[:k], kw.length, kw.key?) are deferred -- the existing sp_SymPolyHash_* runtime already supports them, but plumbing the call sites is its own task. This CL is the declaration-side floor; forwarding (next CL) leans on the slot without reading it.

Test exercises (test/kwargs_param.rb):

  • def f(a, **kw); a + 1; end -- declaration compiles, body ignores kw
  • def f(a, b, **nil) -- explicit kwarg rejection still works post-A1

Tests: 290 pass. Bootstrap: gen2.c == gen3.c.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
afb3ee23dd49
Parent
fea5369dddb0

spinel_codegen.rbmodifiedView file

@@ -68,6 +68,9 @@
6868 @nd_constant_path = []
6969 @nd_superclass = []
7070 @nd_rest = []
71+ # ParametersNode#keyword_rest -- holds a KeywordRestParameterNode
72+ # (def f(**kw)) or NoKeywordsParameterNode (def f(**nil)).
73+ @nd_keyword_rest = []
7174 @nd_rescue_clause = []
7275 @nd_ensure_clause = []
7376 @nd_expression = []
@@ -89,6 +92,11 @@
8992 @nd_exceptions = "".split(",")
9093 @nd_targets = "".split(",")
9194 @nd_rights = "".split(",")
95+ # ParametersNode#posts -- required params after the splat
96+ # (def f(*r, x, y) → posts = [x, y]). Currently unused by codegen
97+ # (post-rest parameters aren't observed in test/), but the parser
98+ # emits the field so future tests get a proper AST.
99+ @nd_posts = "".split(",")
92100
93101 @nd_count = 0
94102 @root_id = 0
@@ -421,6 +429,7 @@
421429 @nd_constant_path.push(-1)
422430 @nd_superclass.push(-1)
423431 @nd_rest.push(-1)
432+ @nd_keyword_rest.push(-1)
424433 @nd_rescue_clause.push(-1)
425434 @nd_ensure_clause.push(-1)
426435 @nd_expression.push(-1)
@@ -440,6 +449,7 @@
440449 @nd_exceptions.push("")
441450 @nd_targets.push("")
442451 @nd_rights.push("")
452+ @nd_posts.push("")
443453 @nd_count = @nd_count + 1
444454 nid
445455 end
@@ -663,6 +673,9 @@
663673 if field == "rest"
664674 @nd_rest[nid] = ref_id
665675 end
676+ if field == "keyword_rest"
677+ @nd_keyword_rest[nid] = ref_id
678+ end
666679 if field == "rescue_clause"
667680 @nd_rescue_clause[nid] = ref_id
668681 end
@@ -744,6 +757,9 @@
744757 if field == "rights"
745758 @nd_rights[nid] = ids_str
746759 end
760+ if field == "posts"
761+ @nd_posts[nid] = ids_str
762+ end
747763 end
748764
749765 # ---- Convenience: get stmts of a body node ----
@@ -6105,6 +6121,36 @@
61056121 result = result + @nd_name[rest]
61066122 end
61076123 end
6124+ # Post-rest required params: `def f(*r, x, y)` -> `x, y` come AFTER
6125+ # rest in the AST's `posts` slot. Same shape as `requireds` (each
6126+ # entry is a RequiredParameterNode); flow them straight through.
6127+ posts = parse_id_list(@nd_posts[params])
6128+ k = 0
6129+ while k < posts.length
6130+ if @nd_type[posts[k]] == "RequiredParameterNode"
6131+ if result != ""
6132+ result = result + ","
6133+ end
6134+ result = result + @nd_name[posts[k]]
6135+ end
6136+ k = k + 1
6137+ end
6138+ # Keyword rest (`**kw`). Anonymous `**` synthesizes `__anon_kwrest`.
6139+ # NoKeywordsParameterNode (`**nil`) is skipped here -- it doesn't
6140+ # carry a slot.
6141+ kwrest = @nd_keyword_rest[params]
6142+ if kwrest >= 0
6143+ if @nd_type[kwrest] == "KeywordRestParameterNode"
6144+ if result != ""
6145+ result = result + ","
6146+ end
6147+ kn = @nd_name[kwrest]
6148+ if kn == ""
6149+ kn = "__anon_kwrest"
6150+ end
6151+ result = result + kn
6152+ end
6153+ end
61086154 # Block parameter (&block)
61096155 blk = @nd_block[params]
61106156 if blk >= 0
@@ -6198,6 +6244,29 @@
61986244 result = result + "int_array"
61996245 end
62006246 end
6247+ # Post-rest required params (`def f(*r, x, y)`).
6248+ posts = parse_id_list(@nd_posts[params])
6249+ k = 0
6250+ while k < posts.length
6251+ if @nd_type[posts[k]] == "RequiredParameterNode"
6252+ if result != ""
6253+ result = result + ","
6254+ end
6255+ result = result + "int"
6256+ end
6257+ k = k + 1
6258+ end
6259+ # Keyword rest (**kw). Spinel kwargs use symbol keys (matches
6260+ # `f(a: 1)` keyword hash construction), so the slot is sym_poly_hash.
6261+ kwrest = @nd_keyword_rest[params]
6262+ if kwrest >= 0
6263+ if @nd_type[kwrest] == "KeywordRestParameterNode"
6264+ if result != ""
6265+ result = result + ","
6266+ end
6267+ result = result + "sym_poly_hash"
6268+ end
6269+ end
62016270 # Block parameter (&block)
62026271 blk = @nd_block[params]
62036272 if blk >= 0
@@ -6264,6 +6333,29 @@
62646333 result = result + "-1"
62656334 end
62666335 end
6336+ # Post-rest required params (`def f(*r, x, y)`) — no defaults.
6337+ posts = parse_id_list(@nd_posts[params])
6338+ k = 0
6339+ while k < posts.length
6340+ if @nd_type[posts[k]] == "RequiredParameterNode"
6341+ if result != ""
6342+ result = result + ","
6343+ end
6344+ result = result + "-1"
6345+ end
6346+ k = k + 1
6347+ end
6348+ # Keyword rest (**kw): no compile-time default, slot stays NULL
6349+ # until the caller provides a hash.
6350+ kwrest = @nd_keyword_rest[params]
6351+ if kwrest >= 0
6352+ if @nd_type[kwrest] == "KeywordRestParameterNode"
6353+ if result != ""
6354+ result = result + ","
6355+ end
6356+ result = result + "-1"
6357+ end
6358+ end
62676359 # Block param
62686360 blk = @nd_block[params]
62696361 if blk >= 0