Skip to content

Parenthesised call/vararg should adjust to a single value (§3.4) #254

Closed

Description

@davydog187

Problem

Per Lua 5.3 §3.4: "function calls and vararg expressions, when used inside parentheses, are adjusted to one result". We currently strip parens at parse time, so (f()) and f() are indistinguishable in the AST. constructs.lua is fully deferred on this.

Repro

local function f() return 1,2,3 end
local a, b, c = (f())
return a, b, c
-- expected: 1, nil, nil
-- actual:   1, 2, 3

Suggested approach

  1. Add %Lua.AST.Expr.Paren{inner, meta} to lib/lua/ast/expr.ex. Add it to the union type.
  2. In lib/lua/parser.ex, parse_paren_expr/1 currently returns the inner expression directly. Wrap only when the inner is Call, MethodCall, or Vararg (the multi-value cases). Other parenthesised expressions can stay unwrapped — there is no semantic difference.
  3. In lib/lua/compiler/scope.ex, add a passthrough resolve_expr(%Expr.Paren{inner: e}, state) that recurses.
  4. In lib/lua/compiler/codegen.ex:
    • gen_expr(%Expr.Paren{inner: %Expr.Call{}}, ctx) and the MethodCall variant emit a single-result call (force result_count: 1).
    • gen_expr(%Expr.Paren{inner: %Expr.Vararg{}}, ctx) emits a get_vararg that materialises only the first value.
    • For other inners, just forward to gen_expr(inner, ctx).
  5. New test file pinning the spec example plus parens in return, multi-assign, and table-constructor positions.

Acceptance

  • New regression test in test/lua/vm/ covering the four positions where multi-value expansion happens (last arg, last RHS, return, last field of {...}).
  • constructs.lua either passes or progresses to a different failure. Replace its :all with a narrower range if anything remains.

Risk

  • Compiler IR changes touch several files. Verify nothing in the existing test suite (esp. test/lua/compiler/) pins the old "parens are transparent" behaviour for calls.

Activity

  1. added
    bugSomething isn't working
    direction:ADirection A: suite triage to 0.5.0
    kind:suiteLua 5.3 official test suite work
    on May 28, 2026
  2. added this to the 1.0.0 milestone on May 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdirection:ADirection A: suite triage to 0.5.0kind:suiteLua 5.3 official test suite work

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions