Skip to content

Commit 7fa7bf9

Browse files
authored
Fix printing of array types in JSpecify errors (#1145)
Before we would not print nullability of the top-level array type. Also improve spacing.
1 parent 50cb4ab commit 7fa7bf9

File tree

4 files changed

+21
-17
lines changed

4 files changed

+21
-17
lines changed

nullaway/src/main/java/com/uber/nullaway/generics/GenericTypePrettyPrintingVisitor.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,13 @@ public String visitCapturedType(Type.CapturedType t, Void s) {
7373

7474
@Override
7575
public String visitArrayType(Type.ArrayType t, Void unused) {
76-
// TODO properly print cases like int @Nullable[]
77-
return t.elemtype.accept(this, null) + "[]";
76+
StringBuilder sb = new StringBuilder();
77+
sb.append(t.elemtype.accept(this, null));
78+
for (Attribute.TypeCompound compound : t.getAnnotationMirrors()) {
79+
sb.append(" @");
80+
sb.append(compound.type.accept(this, null));
81+
}
82+
return sb.append(" []").toString();
7883
}
7984

8085
@Override

nullaway/src/test/java/com/uber/nullaway/jspecify/GenericsTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,7 @@ public void genericPrimitiveArrayTypeAssignment() {
10721072
"class Test {",
10731073
" static class A<T extends @Nullable Object> { }",
10741074
" static void testPositive() {",
1075-
" // BUG: Diagnostic contains: Cannot assign from type A<int[]>",
1075+
" // BUG: Diagnostic contains: Cannot assign from type A<int []>",
10761076
" A<int @Nullable[]> x = new A<int[]>();",
10771077
" }",
10781078
" static void testNegative() {",

nullaway/src/test/java/com/uber/nullaway/jspecify/JSpecifyArrayTests.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ public void arraySubtyping() {
250250
" @Nullable Integer[] x2 = nullableIntArr;",
251251
" // legal (covariant array subtypes)",
252252
" x2 = nonnullIntArr;",
253-
" // BUG: Diagnostic contains: Cannot assign from type @Nullable Integer[] to type Integer[]",
253+
" // BUG: Diagnostic contains: Cannot assign from type @Nullable Integer [] to type Integer []",
254254
" x1 = nullableIntArr;",
255255
" }",
256256
"}")
@@ -272,7 +272,7 @@ public void arraySubtypingWithNewExpression() {
272272
" @Nullable Integer[] x2 = new Integer[0];",
273273
" // legal",
274274
" x2 = new @Nullable Integer[0];",
275-
" // BUG: Diagnostic contains: Cannot assign from type @Nullable Integer[] to type Integer[]",
275+
" // BUG: Diagnostic contains: Cannot assign from type @Nullable Integer [] to type Integer []",
276276
" x1 = new @Nullable Integer[0];",
277277
" }",
278278
"}")
@@ -290,7 +290,7 @@ public void arraysAndGenerics() {
290290
"class Test {",
291291
" void foo(List<@Nullable Integer[]> l) {}",
292292
" void testPositive(List<Integer[]> p) {",
293-
" // BUG: Diagnostic contains: Cannot pass parameter of type List<Integer[]>",
293+
" // BUG: Diagnostic contains: Cannot pass parameter of type List<Integer []>",
294294
" foo(p);",
295295
" }",
296296
" void testNegative(List<@Nullable Integer[]> p) {",
@@ -312,7 +312,7 @@ public void genericArraysReturnedAndPassed() {
312312
" static class Bar<T> {",
313313
" Foo<T>[] getFoosPositive() {",
314314
" @Nullable Foo<T>[] result = new Foo[0];",
315-
" // BUG: Diagnostic contains: Cannot return expression of type @Nullable Foo<T>[] from method",
315+
" // BUG: Diagnostic contains: Cannot return expression of type @Nullable Foo<T> [] from method",
316316
" return result;",
317317
" }",
318318
" Foo<T>[] getFoosNegative() {",
@@ -321,7 +321,7 @@ public void genericArraysReturnedAndPassed() {
321321
" }",
322322
" void takeFoos(Foo<T>[] foos) {}",
323323
" void callTakeFoosPositive(@Nullable Foo<T>[] p) {",
324-
" // BUG: Diagnostic contains: Cannot pass parameter of type @Nullable Foo<T>[]",
324+
" // BUG: Diagnostic contains: Cannot pass parameter of type @Nullable Foo<T> []",
325325
" takeFoos(p);",
326326
" }",
327327
" void callTakeFoosNegative(Foo<T>[] p) {",
@@ -331,9 +331,9 @@ public void genericArraysReturnedAndPassed() {
331331
" void callTakeFoosVarargsPositive(@Nullable Foo<T>[] p, Foo<T>[] p2) {",
332332
" // Under the hood, a @Nullable Foo<T>[][] is passed, which is not a subtype",
333333
" // of the formal parameter type Foo<T>[][]",
334-
" // BUG: Diagnostic contains: Cannot pass parameter of type @Nullable Foo<T>[]",
334+
" // BUG: Diagnostic contains: Cannot pass parameter of type @Nullable Foo<T> []",
335335
" takeFoosVarargs(p);",
336-
" // BUG: Diagnostic contains: Cannot pass parameter of type @Nullable Foo<T>[]",
336+
" // BUG: Diagnostic contains: Cannot pass parameter of type @Nullable Foo<T> []",
337337
" takeFoosVarargs(p2, p);",
338338
" }",
339339
" void callTakeFoosVarargsNegative(Foo<T>[] p) {",
@@ -367,7 +367,7 @@ public void overridesReturnType() {
367367
" @Override",
368368
" Integer[] foo() { return new Integer[0]; }",
369369
" @Override",
370-
" // BUG: Diagnostic contains: Method returns @Nullable Integer[], but overridden method returns Integer[]",
370+
" // BUG: Diagnostic contains: Method returns @Nullable Integer [], but overridden method returns Integer []",
371371
" @Nullable Integer[] bar() { return new @Nullable Integer[0]; }",
372372
" }",
373373
"}")
@@ -389,7 +389,7 @@ public void overridesParameterType() {
389389
" }",
390390
" class Sub extends Super {",
391391
" @Override",
392-
" // BUG: Diagnostic contains: Parameter has type Integer[], but overridden method has parameter type @Nullable Integer[]",
392+
" // BUG: Diagnostic contains: Parameter has type Integer [], but overridden method has parameter type @Nullable Integer []",
393393
" void foo(Integer[] p) { }",
394394
" @Override",
395395
" void bar(@Nullable Integer[] p) { }",
@@ -407,7 +407,7 @@ public void ternaryOperator() {
407407
"import org.jspecify.annotations.Nullable;",
408408
"class Test {",
409409
" static Integer[] testPositive(Integer[] p, boolean t) {",
410-
" // BUG: Diagnostic contains: Conditional expression must have type Integer[]",
410+
" // BUG: Diagnostic contains: Conditional expression must have type Integer []",
411411
" Integer[] t1 = t ? new Integer[0] : new @Nullable Integer[0];",
412412
" // BUG: Diagnostic contains: Conditional expression must have type",
413413
" return t ? new @Nullable Integer[0] : new @Nullable Integer[0];",

nullaway/src/test/java/com/uber/nullaway/jspecify/JSpecifyVarargsTests.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -535,14 +535,13 @@ public void varargsOverride() {
535535
" }",
536536
" static class NullableVarargsContentsImpl2 implements NullableVarargsContents {",
537537
" @Override",
538-
" // BUG: Diagnostic contains: Parameter has type Object[], but overridden method",
538+
" // BUG: Diagnostic contains: Parameter has type Object [], but overridden method",
539539
" public void varargs(Object... params) {",
540540
" }",
541541
" }",
542542
" static class NullableVarargsContentsImpl3 implements NullableVarargsContents {",
543543
" @Override",
544-
// TODO open an issue to improve the error message in a follow up
545-
" // BUG: Diagnostic contains: Parameter has type Object[]",
544+
" // BUG: Diagnostic contains: Parameter has type Object @Nullable []",
546545
" public void varargs(Object @Nullable... params) {",
547546
" }",
548547
" }",
@@ -596,7 +595,7 @@ public void varargsOverride() {
596595
" }",
597596
" static class NullableVarargsBothImpl3 implements NullableVarargsBoth {",
598597
" @Override",
599-
" // BUG: Diagnostic contains: Parameter has type Object[]",
598+
" // BUG: Diagnostic contains: Parameter has type Object @Nullable []",
600599
" public void varargs(Object @Nullable... params) {",
601600
" }",
602601
" }",

0 commit comments

Comments
 (0)