fix(compiler): expand multi-values in obj:method(...) calls#248
Merged
Conversation
The method-call codegen hardcoded `arg_count = length(args) + 1`, so
`obj:m(unpack(vals))` compiled as a call with a fixed argument count and
discarded every unpacked value after the first. Reference Lua treats
`t:m(unpack(vals))` as sugar for `t.m(t, vals[1], vals[2], ...)`.
Extend `gen_expr(%Expr.MethodCall{...})` to use the same last-argument
classification (`:vararg` / `:multi_call` / `:normal`) that `Expr.Call`
already has, offset by one register for the implicit `self` slot:
- vararg tail → `call(base, -(arg_count + 2), ...)`
- inner call → patch inner result_count to -2; outer
`call(base, {:multi, 1 + fixed_count}, ...)`
- normal → unchanged
No executor change needed: `:call` dispatch operates purely on register
layout regardless of whether the function reached `R[base]` via `:self`
or `:move`.
Fixes #247.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
obj:method(unpack(args))drops all but first unpacked value #247:obj:m(unpack(vals))was dropping every value after the first because the method-call codegen hardcoded a fixedarg_count.gen_expr(%Expr.MethodCall{...})now mirrors the last-argument classification (:vararg/:multi_call/:normal) thatExpr.Callalready had, offset by one register for the implicitselfslot.:calldispatch operates purely on register layout regardless of whether the function reachedR[base]via:selfor:move.The user-visible knock-on from #247 —
("%s,%s,%s"):format(table.unpack(args))raising aMatchErrorinstring.format— is a symptom of the same bug and is resolved by this change. It surfaced in real code throughsay/init.lua(str:format(unpack(strings))), which is whatluassertuses.Test plan
mix test test/language/function_test.exs— 5 new regressions coveringtable.unpack, vararg, inner-call expansion, leading fixed args before an unpack, and thestring:formatknock-onmix test— full suite passes (1889 tests, 0 failures, 30 skipped)obj:method(unpack(args))drops all but first unpacked value #247 produce the documented expected output (n=3,[a,b,c])