fix: erase unresolved generic field type to its bound in field-get assign - #2888
fix: erase unresolved generic field type to its bound in field-get assign#2888obus-globus wants to merge 1 commit into
Conversation
| } | ||
|
|
||
| private ArgType getResultType(ArgType instanceType) { | ||
| ArgType resultGeneric = root.getTypeUtils().replaceClassGenerics(instanceType, initType); |
There was a problem hiding this comment.
Support for missing generic types (raw and wildcard (<?>)) should be done inside TypeUtils.replaceClassGenerics method.
This will resolve many other issues and not only with field types.
But such task can be hard one, you can try to solve it, I also will try to look into this.
There was a problem hiding this comment.
Agreed, replaceClassGenerics is the right home for this in general.
I tried it there (erase to the declared bound when a variable can't be resolved from the instance), but it regresses TestTypeResolver12: a null return there can't tell the class's own in-scope T (WeakReference<T> read inside TestCls<T>, must stay T) from an out-of-scope raw-instance variable that should be erased, so erasing both breaks multi-variable inference. The field-get bound has that context; replaceClassGenerics doesn't.
Not sure how to continue here, let me know if you need something.
NOTE: This is what one may call "AI slop". Feel free to close this PR if it's not up to your quality standards or even if you just aren't interested in reviewing it. I use these changes downstream on an APK and thought I might as well make a PR offering to upstream them, as your CONTRIBUTING.md doesn't say anything discouraging this.
Description
Reading a generic field through a raw instance type and null-checking it emitted
== 0instead of== null(with atype inference failedwarning), for both a type-variable field (T) and arrays of one (T[]).When the field's type variable can't be resolved from the raw instance type,
getResultTypereturned it unchanged, leaving an out-of-scope type variable that type propagation rejects, so the compared0literal kept its unknown type. Erase the unresolved variable to its leftmost bound (like javac), arrays element-wise, falling back toObjectwhen unbounded or when the bound is itself a type variable. This mirrors the extend-type handling inFixTypesVisitor, rather than the straight-to-Objecterasure incheckRawType, so aT extends Numberfield stays usable as aNumber.Includes an integration test.
Fixes #2886