Skip to content

Codegen: foreign-body helper functions survive export tree-shaking as dead code #274

Description

@Unisay

Problem

Found while linting pslua-generated Lua in a downstream application (OpenResty, with luacheck and lua-language-server). Cosmetic only: the code runs and passes luac -p. Re-confirmed on current main.

pslua correctly narrows a foreign module's exported table to the fields actually used, but the top-level local function helpers in the FFI file body are emitted verbatim even when only the dropped exports referenced them. A module that uses only Data.Int.toNumber emits:

local Data_Int_foreign = (function()
  local function toInt32(n) … end      -- dead: only fromStringAsImpl/toStringAs used it
  local function digitValue(c) … end   -- dead: same
  return { toNumber = function(n) return n end }  -- export table narrowed to the one used field
end)()

The return table is narrowed (good), but toInt32 and digitValue are now unreachable and still shipped. Root cause: export-table narrowing does not drive a DCE pass over the foreign body's own local bindings.

Approach

After narrowing the export table, drop foreign-body locals unreachable from the retained exports.

Prerequisites / Relations

None. Extends the existing foreign export-table narrowing.

Verification / Measurement

The Data.Int example ships without toInt32/digitValue when only toNumber is used; generated output is smaller and still passes luac -p. Low priority: dead code plus a little size bloat, not incorrect.

Metadata

Metadata

Assignees

No one assigned

    Labels

    OptimisationA Compiler Optimisationarea: codegenLua code generation / printingenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions