Repository navigation
docs: describe how to annotate existing API for nullability #26793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
8d466ba
7561706
5a6f433
1dec795
76f2892
c440946
b3eec39
dcc1bd2
4eb277f
dc6b259
0b0358c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -140,8 +140,9 @@ then say what is different about it. See `guidelines/design.md`. | |
|
|
||
| ## Nullability | ||
|
|
||
| Apply `@NullMarked` (JSpecify) at the package level and annotate only what | ||
| genuinely may be null with `@Nullable`. | ||
| Apply `@NullMarked` (JSpecify) at the package level for a new package, and | ||
| on the class for a new class in a package that is not null-marked yet. | ||
| Annotate only what genuinely may be null with `@Nullable`. | ||
|
|
||
| Return an empty collection, array, map or stream — never `null` — when there | ||
| are no values. | ||
|
|
@@ -161,6 +162,38 @@ Put `@Nullable` on the declared type (`ValueSignal<@Nullable X>`). NullAway | |
| infers it for the constructor, so no repeated type argument or type witness is | ||
| needed. | ||
|
|
||
| When annotating existing classes, put `@NullMarked` on each class you have | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 The new class-by-class The section still opens with "Apply Scoping the first rule in CONVENTIONS.md:164 ·
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, |
||
| audited. Add it at the package level (in `package-info.java`) only once every | ||
| class in the package has been audited, because it opts the whole package in. | ||
| See `guidelines/design.md`. | ||
|
|
||
| Adding `@Nullable` or `@NullMarked` to existing public API can break Kotlin | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 The breaking-change rule asks for something the repo marks differently, and never names the How a breaking change is declared is already defined in the Pointing the rule at the existing marker would make it actionable; the genuinely new information (a Kotlin-only source break counts as breaking) survives either way. CONVENTIONS.md:169 ·
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. The rule now uses the repo's existing markers: a |
||
| code, which treats JSpecify annotations as strict. Mark such a change as | ||
| breaking even though Java callers without NullAway compile unchanged: a `!` in | ||
| the commit and pull request title and a ⚠️ Breaking change entry in the | ||
| description's risks, as the skill linked from the Commit & PR Hygiene section | ||
| describes. See `guidelines/design.md`. | ||
|
|
||
| Do not introduce a wildcard such as `? extends @Nullable T` into a parameter | ||
| type of an existing overridable method: overrides with the old signature stop | ||
| compiling. See `guidelines/design.md`. | ||
|
|
||
| Keep the existing delegation between overloads when annotating them. Do not | ||
| split overloads to give them different nullness. See `guidelines/design.md`. | ||
|
|
||
| When a generic parameter of an existing method needs a nullable type argument, | ||
| annotate the type argument (`SerializableFunction<UI, @Nullable | ||
| Registration>`) if callers usually pass a lambda. If they usually pass a | ||
| variable of the unannotated type, or the annotation would change another part | ||
| of the signature, as with `Signal<String>` and the returned binding type in | ||
| `Element.bindText`, use `@NullUnmarked` on the method, with a short comment | ||
| saying which value may be null and why the type argument is not annotated. See | ||
| `guidelines/design.md`. | ||
|
|
||
| Give the same parameter the same nullness across sibling methods: all | ||
| `executeJs`-style varargs methods either accept a `null` array or reject it. | ||
| See `guidelines/design.md`. | ||
|
|
||
| ## `toString()` | ||
|
|
||
| Keep `toString()` for people — logs, debugger views, exception messages — and | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Four of the five new rules omit the chapter pointer the conventions file requires.
The Conventions and Guidelines section says to "end it with "See
guidelines/<chapter>.md." when the reasoning needs more room than a sentence". Each new rule has a full bullet of reasoning indesign.md, yet only the Kotlin one carries the pointer.The same section also says a chapter "links to the rule it explains instead of restating it"; the new bullets end by repeating their rule ("give it the same nullness", "point to a single tracking issue", "mark each audited class … once all of them are done").
CONVENTIONS.md:164 ·
conventionsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Every new rule now ends with "See
guidelines/design.md." The chapter subsection links to the Nullability section ofCONVENTIONS.mdand gives only the reasoning; the bullets no longer repeat the rules. The Kotlin rule also dropped the details that now live only in the chapter.