Repository navigation
Substitute type arguments into the element type of an array field - #1530
Merged
Merged
Conversation
An instance array field whose element type is the class's type parameter (`T array[3] arr` in `class G<T:>` or `class G<T>`) was accepted, but a read or write through a receiver of a concrete instantiation was checked against the unbound `T`: `g.arr[1] = 3` on a `G<int>` failed with "Cannot assign integer-literal to T", `g.arr[1] + 1` found no operator, and a subclass `Sub extends G<int>` could not use the inherited array with its concrete type. The field's link substitutes the receiver's type arguments into the field's type through `setTypeArgs`, but `WurstTypeArray` did not override it, so the default returned the array type unchanged and the binding never reached the element type. A plain field `T x` and a static `T array` (read inside the class, where `T` stays unbound) were not affected. `WurstTypeArray.setTypeArgs` now substitutes into the element type, as `WurstTypeVararg` already did. The substituted array shares the sizes of the declared one, so its size expression is still evaluated and reported once. The backends needed no change: Jass specialises one set of typed arrays per instantiation, and Lua keeps the erased per-instance table it uses for other type-parameter fields. Tests cover new generics with int, string, class and tuple elements, methods of the class, two instantiations, a subclass, a generic receiver and an element of the wrong type, with and without the optimiser on the interpreter, Jass and Lua, and old generics with int and class elements, a subclass and the wrong element type.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
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.
Problem
In a generic class, an array field whose element type is the type parameter could not be used through a concrete instantiation:
The validator accepts the declaration (its array-field checks are zero dimensions, a missing size and several dimensions), plain
Tfields andstatic T arrayalready work, and the backends handle the field once it is typed. The type checker was inconsistent: a member access substitutes the receiver's type arguments into the field's type withsetTypeArgs(VarLink.withTypeArgBinding), andWurstTypeArraydid not override it, so its element type stayed the unboundT. Old generics<T>had the same gap.Change
WurstTypeArray.setTypeArgssubstitutes into the element type, asWurstTypeVarargalready does, and returnsthiswhen nothing changes. The substituted copy shares the declared array's sizes (sizeSource), so the size expression is evaluated, and any error on it reported, once. No backend change: Jass specialises the arrays per instantiation, and Lua keeps erased per-instance storage.Tests
Each new test failed without the change. All run in the interpreter, on Jass and on Lua.
GenericsWithTypeclassesTests:intelement, also optimised;Greading and writingarr[i]with several instantiations;GenericsTests: old generics withintand class elements, and the wrong element type.Windows, bundled Lua 5.3,
-PallBackends: GenericsTests 103, GenericsWithTypeclassesTests 128 and ArrayTests 20 on master after #1527. Before the rebase: ClassesTests 128, LuaBackendAuditTests 285, GenericSpecialisationMatrixTests 384 and DeterministicChecks 29. The whole suite passed: 3,283 tests. Two large real maps compile to byte-identical optimised Lua, and one also to byte-identical Jass. Not done: Linux and macOS.Found while fixing the defects from the 19-21 Oct audit (#1526).