diff --git a/CONVENTIONS.md b/CONVENTIONS.md index 043ff359ccb..126b79eb654 100644 --- a/CONVENTIONS.md +++ b/CONVENTIONS.md @@ -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 +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 +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`) 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` 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 diff --git a/guidelines/design.md b/guidelines/design.md index e3932d0d844..6224fd95df9 100644 --- a/guidelines/design.md +++ b/guidelines/design.md @@ -154,6 +154,63 @@ The reasoning behind the rules in the Nullability section of permits omissions; translating it at the boundary keeps those nulls out of the public API. +#### Annotating existing API + +The rules above are written for new code. Retrofitting JSpecify annotations +onto a class that already ships has traps of its own, and the fix for one of +them tends to break something else. The points below explain the rules on +annotating existing classes in the Nullability section of +[`CONVENTIONS.md`](../CONVENTIONS.md#nullability): + +- **It can be a Kotlin source break.** Kotlin reads JSpecify annotations as + strict (the default since Kotlin 2.1), so marking an existing getter such as + `Element.getParent()` `@Nullable` makes `el.parent.tag` a compile error. + Making a parameter `@Nullable` relaxes the contract for callers, but a Kotlin + subclass whose override still declares that parameter non-null stops + compiling. Null-marking a class breaks the other way round, because every + unannotated type in it becomes non-null for Kotlin: a Kotlin caller passing + `null` to such a parameter stops compiling, and so does a Kotlin override + that declares the parameter nullable. Return types only get stricter: + callers do not notice, but a Kotlin override declaring a nullable return + type stops compiling. Java callers are unaffected unless they run NullAway, + which is why the change looks safe from the Java side. +- **Wildcards clash with overrides.** Changing a parameter of a non-final + method from `SerializableFunction` to + `SerializableFunction` keeps the + erasure but changes the signature, so an existing override with the old + parameter type fails with "name clash: … have the same erasure, yet neither + overrides the other". +- **An annotated type argument is not equivalent.** + `SerializableFunction` avoids the clash and + accepts lambdas, the usual argument, unchanged. Type arguments are invariant, though, so a variable + declared as `SerializableFunction` no longer fits in Kotlin + or under NullAway. Overrides are less affected: a Java override that keeps the + old type argument still compiles and is called, as it does for + `Component.whenAttached`; only a Kotlin or null-marked Java override gets a + nullness mismatch. That rules the annotation out where the argument is usually + such a variable, or where it would change more than the parameter: + `Element.bindText(Signal)` is called with signals declared elsewhere, + and `Signal<@Nullable String>` would also change the returned binding type. + `@NullUnmarked` is then the remaining choice. It turns off checking for every + parameter and the return type of the method, not just the one type argument, + so it stays limited to the methods that need it, and a comment on each says + why, so the next person does not "fix" it by annotating the type argument. +- **Overloads keep their delegation.** If `getProperty(String)` delegates to + `getProperty(String, String)`, a subclass may override either one and rely + on the other going through it. Splitting the two to give them different + nullness changes which override takes effect. Check how callers actually use + an overload before tightening it, too: `getProperty(name, null)` is a common + idiom, so a non-null `defaultValue` contract would not hold. +- **Siblings agree.** `executeJs`, `callJsFunction` and `addJsInitializer` all + take a varargs array of arguments. A caller should not find that `null` is + accepted by one and throws a `NullPointerException` in another. +- **Package-level `@NullMarked` covers every class.** With NullAway's + `OnlyNullMarked`, `@NullMarked` in `package-info.java` makes every class in + the package checked, and for Kotlin every unannotated type in it becomes + non-null without anyone having audited it. Marking class by class, as the + `Table*` and media classes in `com.vaadin.flow.component.html` are, keeps + that to the classes that have been reviewed. + ### Lifecycle and cleanup Resources that outlive a single request — watches, DOM listeners, timers,