Skip to content

Accept a closure passed to a generic parameter again - #1514

Merged
Frotty merged 1 commit into
masterfrom
fix/closure-generic-param-expected-type
Oct 21, 2026
Merged

Frotty merged 1 commit into
masterfrom
fix/closure-generic-param-expected-type

Conversation

@Frotty

@Frotty Frotty commented Oct 21, 2026

Copy link
Copy Markdown
Member

Problem

Since #1502, a closure passed directly to a generic parameter is rejected:

interface Listener
    function fire()

let listeners = new ArrayList<Listener>()
listeners.add(() -> doSomething())

fails with "Closures can only be used when a interface or class type is given, but at this position a Listener is expected." This applies to old (<T>) and new (<T:>) generics, with or without vararg (the stdlib's ArrayList.add(vararg T elems) is the common case). Compiler 2.0.2 accepts the code.

Cause

#1502 stopped normalising attrExpectedTypAfterOverloading, so that the Lua translation of a generic argument keeps the bound type parameter and its toIndex conversion. The closure validation and ClosureTranslator read the same attribute, and a bound type parameter is neither a WurstTypeClass nor a WurstTypeInterface.

Bisected: the new tests compile at #1502's parent (d8948c6) and fail at #1502 (6e96771).

Fix

AttrExprExpectedType.afterOverloading (the attribute) is normalised again, as before #1502. The Lua argument translation calls the new rawAfterOverloading, which keeps #1502's raw parameter type.

Tests

New in ClosureTests, each executed on Jass, the interpreter and Lua:

  • closurePassedToNewGenericVarargParameter
  • closurePassedToNewGenericParameter
  • closurePassedToOldGenericParameter

All three fail on master with the error above. ClosureTests (68) and GenericsTests (73, including #1502's nestedOldGenericConversionsCancel*) pass locally on Windows. The change is platform-independent attribute logic; Linux and macOS were not run locally.

#1502 stopped normalising attrExpectedTypAfterOverloading so that the Lua
translation of a generic argument keeps its bound type parameter and with it
the toIndex conversion. The closure checks and the closure translation read
the same attribute, and a bound type parameter is neither a class nor an
interface type, so a closure passed to a generic parameter was rejected:
listeners.add(() -> ...) on an ArrayList<Listener> failed with "Closures can
only be used when a interface or class type is given, but at this position a
Listener is expected", for old and new generics, vararg or not.

The attribute is normalised again, as before #1502; the translation asks for
the raw parameter type by its own method.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-21T07:48:53.577016Z a11009c PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@Frotty
Frotty merged commit 01f93f1 into master Oct 21, 2026
2 checks passed
@Frotty
Frotty deleted the fix/closure-generic-param-expected-type branch November 25, 2026 08:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant