Repository navigation
Add positive test cases for annotations on generic method return types - #1704
Conversation
nonNullInReturnTypeArg asserted only the negative direction: that a @nonnull type argument restored onto a method's return type is accepted where a non-null type argument is expected. The error-reporting direction was untested for method returns, although nonNullInFieldTypeArg covers it for field reads. Adds an asNullable() counterpart and asserts that passing or assigning its result where a non-null type argument is expected is reported. This is the case reported in uber#1201, which was already fixed by uber#1371: the repro from the issue reports no error at 6dd3976 and is correctly flagged at 938a897, and guarding out the restoreExplicitNullabilityAnnotations call that uber#1371 added makes both new assertions fail on current master. Resolves uber#1201
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
WalkthroughAdded Possibly related PRs
Suggested labels: Suggested reviewers: Merge Risk: ⚪ Minimal · up to This change only expands test coverage for existing annotation behavior and does not alter production code; no actionable merge-blocking risk remains after normal checks and review. 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1704 +/- ##
=========================================
Coverage 87.83% 87.83%
Complexity 3184 3184
=========================================
Files 109 109
Lines 10815 10815
Branches 2186 2186
=========================================
Hits 9499 9499
Misses 622 622
Partials 694 694 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Summary
#1201 reports that no error is issued when a method's return type carries a nullability annotation on a
type variable that is instantiated from the receiver's type argument:
Rather than assume the issue described current behavior, I ran the example verbatim as a test first. On
current
masterNullAway does report the expected error:This was fixed by
938a8972(#1371, "Handle annotations on type variables in return and field types"),which was filed against #1354 and resolved this issue as a side effect. Confirmed by building both sides:
the repro reports nothing at
6dd3976f, the commit immediately preceding it, and is correctly flagged at938a8972.That matches the mechanism. #1371 added
restoreExplicitNullabilityAnnotationsto theMethodInvocationTreeandMemberSelectTreebranches ofGenericsChecks.getTreeType. That call has sincemoved into the
getMethodTypeForInvocationpath but is still what makes this case work: the receiver'stype is used to compute the method's type as a member of it via
TypeSubstitutionUtils.memberType,substituting
T := Objectinto the declared return typeInner<@Nullable T>to yieldInner<@Nullable Object>, and the explicit annotation is then restored onto the result. The assignmentcheck then sees the mismatch against the declared
Inner<Object>.So no production change is needed here. While confirming that, though, I found a gap in the test coverage,
which is what this PR closes.
nonNullInReturnTypeArg— the test #1371 added for method return types — asserts only the negativedirection, that a restored
@NonNulltype argument is accepted where a non-null type argument isexpected. The error-reporting direction is untested for method returns, although
nonNullInFieldTypeArgcovers it for field reads. That missing direction is exactly what #1201 reports, so it seems worth pinning
down.
This PR adds an
asNullable()counterpart to the existingasNonNull()and asserts both directions.MaybeNull<String> m2 = nonNull.asNullable();is structurally the case from the issue: the receiversubstitutes
T := Stringinto the declared return typeMaybeNull<@Nullable T>, and the assignment isflagged. No production code changes.
Testing
nonNullInReturnTypeArginGenericsTests.javawith two positive cases (argument passing andassignment), keeping an unannotated negative case alongside them so the test pins down both directions
and doesn't just assert that we over-report.
master: guarding out therestoreExplicitNullabilityAnnotationscall thatHandle annotations on type variables in return and field types #1371 added makes both new assertions fail. It also flips the pre-existing negative case
accept(nullable.asNonNull())into a false positive, since without the restore the receiver's typeargument is echoed back in place of the declared annotation.
6dd3976fand passes at938a8972../gradlew :nullaway:test— 918 tests, 0 failures.Resolves #1201
AI usage disclosure
Summary by CodeRabbit
New Features
MaybeNullvalues to a nullable generic form.Tests