From fccfe96854ca59b74fb3cd13a2db9cff005510ed Mon Sep 17 00:00:00 2001 From: cpovirk Date: Wed, 24 Jan 2024 08:00:49 -0800 Subject: [PATCH] Make J2CL super-source'd classes pass nullness checking. RELNOTES=n/a PiperOrigin-RevId: 601121536 --- .../com/google/common/collect/DescendingMultiset.java | 1 + .../google/common/collect/ForwardingImmutableMap.java | 4 +++- .../com/google/common/collect/ImmutableSortedMap.java | 6 ++++-- .../com/google/common/collect/ImmutableSortedSet.java | 10 +++++++++- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/DescendingMultiset.java b/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/DescendingMultiset.java index aaecc2d166ca..a5924b88fd3b 100644 --- a/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/DescendingMultiset.java +++ b/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/DescendingMultiset.java @@ -150,6 +150,7 @@ public Iterator iterator() { } @Override + @SuppressWarnings("nullness") // b/192354773 in our checker affects toArray declarations public T[] toArray(T[] array) { return standardToArray(array); } diff --git a/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ForwardingImmutableMap.java b/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ForwardingImmutableMap.java index 8fbed96f069e..1b13ed3ea151 100644 --- a/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ForwardingImmutableMap.java +++ b/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ForwardingImmutableMap.java @@ -92,12 +92,14 @@ public boolean contains(@Nullable Object object) { } @Override + @SuppressWarnings("nullness") // b/192354773 in our checker affects toArray declarations public T[] toArray(T[] array) { T[] result = super.toArray(array); if (size() < result.length) { // It works around a GWT bug where elements after last is not // properly null'ed. - result[size()] = null; + @Nullable Object[] unsoundlyCovariantArray = result; + unsoundlyCovariantArray[size()] = null; } return result; } diff --git a/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ImmutableSortedMap.java b/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ImmutableSortedMap.java index e6f4ce6f20a3..cc43b88a4668 100644 --- a/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ImmutableSortedMap.java +++ b/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ImmutableSortedMap.java @@ -484,7 +484,9 @@ public ImmutableSortedMap tailMap(K fromKey) { return newView(sortedDelegate.tailMap(fromKey)); } - public ImmutableSortedMap tailMap(K fromKey, boolean inclusive) { + public ImmutableSortedMap tailMap(K fromKeyParam, boolean inclusive) { + // Declare a "true" local variable so that the Checker Framework will infer nullness. + K fromKey = fromKeyParam; checkNotNull(fromKey); if (!inclusive) { fromKey = higher(fromKey); @@ -522,6 +524,6 @@ private static SortedMap newModifiableDelegate(Comparator Comparator<@Nullable E> nullAccepting(Comparator comparator) { - return Ordering.from(comparator).nullsFirst(); + return Ordering.from(comparator).nullsFirst(); } } diff --git a/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ImmutableSortedSet.java b/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ImmutableSortedSet.java index 14fae8b9c7a2..1f6e879b43ab 100644 --- a/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ImmutableSortedSet.java +++ b/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/ImmutableSortedSet.java @@ -262,10 +262,18 @@ public UnmodifiableIterator iterator() { @Override public Object[] toArray() { - return ObjectArrays.toArrayImpl(this); + /* + * ObjectArrays.toArrayImpl returns `@Nullable Object[]` rather than `Object[]` but only because + * it can be used with collections that may contain null. This collection is a collection of + * non-null elements, so we can treat it as a plain `Object[]`. + */ + @SuppressWarnings("nullness") + Object[] result = ObjectArrays.toArrayImpl(this); + return result; } @Override + @SuppressWarnings("nullness") // b/192354773 in our checker affects toArray declarations public T[] toArray(T[] other) { return ObjectArrays.toArrayImpl(this, other); }