Repository navigation
Give a read of an inherited generic field its superclass's type arguments - #1526
Merged
Merged
Conversation
…ents The IM type of a member access substituted only the type arguments of the receiver's own class. A field which the receiver's class inherits from a generic superclass (class Sub extends G<int>, G declaring T v) therefore had G's type variable T as its type. The translation keeps the branches of a conditional expression in a temporary of the first branch's type, so b ? s.v : 5 left a local of type T in a function where T is not in scope, and Jass translation failed with "Type variable T not eliminated". Lua and the interpreter passed, and a tuple argument (Sub extends G<pa>) passed. ImAttrType.getType(ImMemberAccess) now follows the receiver's type through its superclasses to the class declaring the field and substitutes that class's type arguments, as getType(ImMethodCall) does since #1510. This covers the implicit receiver in a subclass method, a field two generic superclasses up, and a generic subclass passing its own argument to a later type parameter of the superclass.
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. |
…ic-field-type # Conflicts: # de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/GenericsTests.java
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. You're on a roll. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
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
class G<T:>with a fieldT v,class Sub extends G<int>, thenlet z = b ? s.v : 5failed Jass translation with "Type variable T not eliminated".ImAttrType.getType(ImMemberAccess)substituted only the receiver's own class's type arguments into the field's type, so a field declared in a generic superclass keptT. The conditional's temporary is typed from its first branch (ExprTranslation.translate(ExprIfElse)), so it became a local of typeT, whereTis not in scope. The interpreter and Lua passed. #1510 fixed the same shape for method calls (getType(ImMethodCall)withasSuperclass).Change
For an access without its own type arguments,
ImAttrTypenow finds the class that declares the field and walks the receiver's type up to it withasSuperclass, then substitutes that class's arguments, the same rule as the method-call case. If the receiver leaves out its type arguments, it falls back to them as before. An access that carries type arguments is unchanged.Tests
Four tests in
GenericsTests, each run in the interpreter, on Jass and on Lua, and each failing on Jass without the change:inheritedGenericFieldReadInConditional;b ? v : 5in a method ofSub);G<T:>,H<U:> extends G<U>,Sub extends H<int>);G<A:, B:>,Sub<U:> extends G<int, U>, which failed with "Type variable B not eliminated").Windows, bundled Lua 5.3,
-PallBackends: GenericsTests 85, GenericsWithTypeclassesTests 119, ClassesTests 128, LuaBackendAuditTests 285, GenericSpecialisationMatrixTests 384, DeterministicChecks 29, all passing. The four new tests also pass with-PallBackends=full. Two large real maps compile to byte-identical optimised Lua with and without the change. Not done: Linux and macOS.Left alone
T array[3] arrinG<T:>) fails type checking with "Cannot assign integer-literal to T", even with no inheritance, becauseWurstTypeArraydoes not substitute its element type. That is a separate front-end gap.EliminateGenerics,ImAttrTypeandLuaFieldDefaultsread anImMemberAccess's own type arguments differently when the receiver's class is not the declaring class. No probe showed a difference, so this is not changed here.Found by a defect audit of the 19-21 Oct merges.