Skip to content

Upstream PR candidate: a missing union member is nil only for a param that rejects nil (fork a5c21324) #40

Open

Description

@mareklangiewicz

Part of #14. Found while measuring #37 (its first results comment). Marek said go ahead with the port on 2026-11-17.

Repro (runs as written)

.emmyrc.json = {}, then emmylua_check .:

---@class Opts
---@field title string|nil

---@param a string?
local function f3(a) return a end

---@param opts Opts|nil
local function g(opts)
  opts = opts or {}
  f3(opts.title)
end
return g
build result
0.25.1 clean
upstream main 4136671 (and 2c400ee) param-type-mismatch "expected string? but found nil" on line 10
fork 14c7537 (installed) clean

Cause

Upstream's check_arg_pairs (param_type_check.rs) has a "missing union member" rule: when the argument is p.k and some member of p's union lacks k, it reports found nil. It never asks whether the parameter accepts nil. After opts = opts or {}, opts is (Opts|table), {} lacks title, and the string? parameter takes nil anyway. The rule arrived with upstream 3474c99 ("refactor project"), after 0.25.1.

Narrowing on upstream (all clean on the fork):

  • the minimal trigger is ---@param opts Opts|table then f3(opts.title); the or {} only builds that union;
  • passing it through a local (local t = opts.title; f3(t)) is clean, because the rule only looks at index-expression arguments;
  • the field's own type doesn't matter: a field declared string still reads as found nil;
  • opts = opts or ({} --[[@as Opts]]) still fires.

The fork fix

a5c2132 (2026-10-24, topic fix-required-module-field-arg), re-ported onto upstream's checker in 8e93a52: one guard, && !is_compatible(semantic_model, &LuaType::Nil, &param_ty), plus test_union_missing_member_accepted_by_nullable_param (a nullable param stays clean, a string param still reports).

Before a draft (AGENTS.md "Porting a fork fix upstream")

  • Branch off upstream main in a scratch worktree; cherry-pick the guard and the test.
  • The test fails on main's source and passes with the guard.
  • Corpus diff with upstream main as the base, --against a same-day 0.25.1 run.
  • gndx emmylua lint on the branch.
  • Duplicate search: upstream issue titles ("missing union member nil", "found nil optional param", "or {} param-type-mismatch", "expected but found nil") find nothing; no open upstream PR diff touches member_missing_in_union. Upstream Incorrect interaction between unions containing nil and optional class fields EmmyLuaLs/emmylua-analyzer-rust#1195 is a different bug (an optional field typed by an alias, in table-literal checking).

Opening the draft is Marek's call.

Activity

  1. mareklangiewicz commented on Nov 17, 2026

    @mareklangiewicz
    OwnerAuthor

    Upstream branch prepared (2026-11-17), every checklist item done

    Branch fix-missing-member-nullable-param, worktree ~/code/rust/emmylua-analyzer-rust-worktrees/nilparam, one commit 0286155e on upstream main 41366714. Local only, no tracking set. The commit message mentions no fork issues.

    The fork's test doesn't carry over. a5c2132's test_union_missing_member_accepted_by_nullable_param uses inline table types (---@param opts? { id?: any, label?: string }). On upstream main that shape never reaches the missing-member rule: the test's clean case passes without the guard, and its "must report" case (need(opts.label), label: string) fails both with and without it. So the branch has a new test of the same name, built on the class repro above. Its rows are (field type → param type): string|nil → string?, string|nil → any, string → string? must be clean; string → string must still report. It counts only param-type-mismatch, because opts = opts or {} with a required title is also, correctly, an assign-type-mismatch.

    Red/green: without the guard the test fails with upstream's exact message (string|nil -> string?: ParamTypeMismatch "expected string?but foundnil."); with it, it passes. Full emmylua_code_analysis: 1911 passed, 26 ignored. gndx emmylua lint passes (fmt, clippy 0.1.100).

    Repro variants: the branch binary matches the installed fork on all 14 narrowing variants (scratch files, not kept).

    Corpus: corpus diff up-41366714-1117 nilparam-0286155e-1117 --against v0251-1117 → totals 58021 → 57804, 216 changed lines: 211 agree, 5 differ, 0 uncompared.

    • param-type-mismatch +0 −218, every line agrees (0.25.1 doesn't report it either). The removed hits name params that take nil: unknown, any, T?, string?, boolean?, ("split"|"stacked"|"unified")?.
    • assign-type-mismatch +5 −4, all differs, is run-to-run noise in upstream itself, not the guard. Checked by running each binary 3 times on the four trees: upstream main alone reports nui tree/init.lua line 240 in one run and 239 in two, drops blink.cmp config/sources.lua:80 in two of three, and varies diffview config.lua 334/339 and lspconfig perlls.lua 24–26. Same flip as Quirk: which failing constructor field gets reported flips between runs #9 (which failing constructor field gets reported).
    • NOT COMPARABLE only because of the same 5 refused trees as Upstream PR candidate: bare ? type literal lowers to nil (LuaLS T|?), fork 14c7537a #37: async.nvim, neotest, neovim-runtime and tuis.nvim crash on both sides; mason.nvim is the excluded blowup.

    Opening the draft is Marek's call.

  2. mareklangiewicz commented on Nov 17, 2026

    @mareklangiewicz
    OwnerAuthor

    Opened as upstream draft 1423 (2026-11-17), branch fix-missing-member-nullable-param at 0286155e, pushed to the fork remote. Stays open until upstream merges or closes it. Marking it ready is Marek's call.

  3. mareklangiewicz commented on Nov 20, 2026

    @mareklangiewicz
    OwnerAuthor

    Experiment: "missing member ⇒ nil" moved into the read type (X1, #43 ranking item 3), 2026-11-20

    Branch x1-union-missing-nil d9fbad90 (fork 4912ed4 + 10/−16, parked, not installed):

    • In the read layer, guarded adds nil to a member read when member_missing_in_union holds.
    • The checker's special case in param_type_check.rs is deleted.

    Call arguments re-enter the read layer, so param-type-mismatch sees the nil too.

    Corpus merge-7d6084b4-1120 → x1nil-1120 (--against v0251-1120; mason.nvim was refused because the binary had uncommitted changes): 54763 → 54837, 151 changed lines.

    code + −
    need-check-nil 95 0
    param-type-mismatch 17 39
    unnecessary-assert 0 2
    assign-type-mismatch 2 0
    unnecessary-if 1 0

    All 40 removed lines agree with 0.25.1, but only 7 of the 111 added lines do.

    The added lines are mostly false and come from one class: module unions. A required module reads as (M|{…}), and the table-literal component lacks most keys. Examples: function vim.iter may be nil (neovim _core/editor.lua:1139), plenary function a.it may be nil (×~50 in tests/plenary/async/*), oil M.parse_url, chunk:match. The checker's rule hid this class twice over: it fired only on call arguments, and only for params that reject nil.

    Verdict: parked. Before unifying, member_missing_in_union must not count a module's literal component as a missing member. That's the #48 / A3 family (a module on an unresolved require). Re-run this experiment after that.

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

    needs-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions