Commit 8f40500
authored
JSpecify: Improve error messages for type incompatibility at pseudo-assignments (#1279)
Here is the new test:
```java
@test
public void assignmentIncompatibilityViaExtendsErrorMessage() {
makeHelper()
.addSourceLines(
"Test.java",
"import org.jspecify.annotations.*;",
"@NullMarked",
"class Test {",
" class A<T extends @nullable Object> {}",
" class B<T extends @nullable Object> extends A<@nullable T> {}",
" class C<T extends @nullable Object> extends A<@nonnull T> {}",
" void test() {",
" // BUG: Diagnostic contains: incompatible types: Test.B<Object> cannot be converted to Test.A<Object> (Test.B<Object> is a subtype of Test.A<@nullable Object>)",
" A<Object> a = new B<Object>();",
" // BUG: Diagnostic contains: incompatible types: Test.C<@nullable Object> cannot be converted to Test.A<@nullable Object> (Test.C<@nullable Object> is a subtype of Test.A<@nonnull Object>)",
" A<@nullable Object> a2 = new C<@nullable Object>();",
" }",
"}")
.doTest();
}
```
Before, the first error message would be something like:
```
Cannot assign from type Test.B<Object> to type Test.A<Object> due to
mismatched nullability of type parameters.
```
This message is confusing, since it looks from the message like the type arguments have the same nullability. Here we change the message to:
```
incompatible types: Test.B<Object> cannot be converted to Test.A<Object>
(Test.B<Object> is a subtype of Test.A<@nullable Object>)
```
The "incompatible types" and "cannot be converted to" verbiage is taken from `javac`, so hopefully it's familiar to developers. The final part of the message showing that `B<Object> <: A<@nullable Object>` should hopefully help developers figure out the root cause of the issue. (Interestingly, for an equivalent example in Swift, only `A<@nullable Object>` is shown in the error message, not `B<Object>`.) The final part of the message is only shown if the base types involved are not the same.
We also make the error messages for parameter passing, returns, and assignments the same (matching `javac`), and update a whole bunch of tests to expect the new error messages.
<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit
* New Features
* Standardized diagnostics for generic nullability/type incompatibilities across assignments, parameter passing, and returns.
* Messages now use “incompatible types: <rhs> cannot be converted to <lhs>” and may include helpful subtype hints where applicable.
* No changes to public APIs.
* Refactor
* Centralized construction of error messages for consistency.
* Tests
* Updated expected diagnostics across generics and array tests to match the new format.
* Added a test validating extends-based incompatibility messages.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->1 parent 9272d8b commit 8f40500
5 files changed
Lines changed: 116 additions & 84 deletions
File tree
- nullaway/src
- main/java/com/uber/nullaway/generics
- test/java/com/uber/nullaway/jspecify
Lines changed: 30 additions & 19 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
253 | 253 | | |
254 | 254 | | |
255 | 255 | | |
| 256 | + | |
256 | 257 | | |
257 | | - | |
258 | | - | |
259 | | - | |
260 | | - | |
261 | | - | |
262 | | - | |
263 | | - | |
264 | | - | |
| 258 | + | |
265 | 259 | | |
266 | 260 | | |
267 | 261 | | |
268 | 262 | | |
269 | 263 | | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
270 | 289 | | |
271 | 290 | | |
272 | 291 | | |
273 | 292 | | |
274 | 293 | | |
275 | 294 | | |
276 | | - | |
277 | | - | |
278 | | - | |
279 | | - | |
280 | | - | |
281 | | - | |
| 295 | + | |
282 | 296 | | |
283 | 297 | | |
284 | 298 | | |
| |||
310 | 324 | | |
311 | 325 | | |
312 | 326 | | |
313 | | - | |
314 | | - | |
315 | | - | |
316 | | - | |
317 | | - | |
| 327 | + | |
| 328 | + | |
318 | 329 | | |
319 | 330 | | |
320 | 331 | | |
| |||
Lines changed: 5 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
67 | | - | |
| 67 | + | |
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
| |||
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
89 | | - | |
| 89 | + | |
90 | 90 | | |
91 | | - | |
| 91 | + | |
92 | 92 | | |
93 | 93 | | |
94 | 94 | | |
| |||
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
116 | | - | |
| 116 | + | |
117 | 117 | | |
118 | | - | |
| 118 | + | |
119 | 119 | | |
120 | 120 | | |
121 | 121 | | |
| |||
Lines changed: 12 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
190 | 190 | | |
191 | 191 | | |
192 | 192 | | |
193 | | - | |
| 193 | + | |
194 | 194 | | |
195 | 195 | | |
196 | 196 | | |
| |||
241 | 241 | | |
242 | 242 | | |
243 | 243 | | |
244 | | - | |
| 244 | + | |
245 | 245 | | |
246 | 246 | | |
247 | 247 | | |
| |||
324 | 324 | | |
325 | 325 | | |
326 | 326 | | |
327 | | - | |
| 327 | + | |
328 | 328 | | |
329 | | - | |
| 329 | + | |
330 | 330 | | |
331 | 331 | | |
332 | 332 | | |
| |||
366 | 366 | | |
367 | 367 | | |
368 | 368 | | |
369 | | - | |
| 369 | + | |
370 | 370 | | |
371 | 371 | | |
372 | 372 | | |
| |||
378 | 378 | | |
379 | 379 | | |
380 | 380 | | |
381 | | - | |
| 381 | + | |
382 | 382 | | |
383 | 383 | | |
384 | 384 | | |
| |||
400 | 400 | | |
401 | 401 | | |
402 | 402 | | |
403 | | - | |
| 403 | + | |
404 | 404 | | |
405 | 405 | | |
406 | 406 | | |
| |||
477 | 477 | | |
478 | 478 | | |
479 | 479 | | |
480 | | - | |
| 480 | + | |
481 | 481 | | |
482 | 482 | | |
483 | 483 | | |
| |||
628 | 628 | | |
629 | 629 | | |
630 | 630 | | |
631 | | - | |
| 631 | + | |
632 | 632 | | |
633 | 633 | | |
634 | 634 | | |
| |||
668 | 668 | | |
669 | 669 | | |
670 | 670 | | |
671 | | - | |
| 671 | + | |
672 | 672 | | |
673 | 673 | | |
674 | 674 | | |
| |||
685 | 685 | | |
686 | 686 | | |
687 | 687 | | |
688 | | - | |
| 688 | + | |
689 | 689 | | |
690 | 690 | | |
691 | 691 | | |
| |||
944 | 944 | | |
945 | 945 | | |
946 | 946 | | |
947 | | - | |
| 947 | + | |
948 | 948 | | |
949 | 949 | | |
950 | 950 | | |
| |||
0 commit comments