Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions CONVENTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Copy link
Copy Markdown
Collaborator

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 in design.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 · conventions

Copy link
Copy Markdown
Contributor Author

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 of CONVENTIONS.md and 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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 The new class-by-class @NullMarked rule contradicts the unqualified package-level rule a few paragraphs above it.

The section still opens with "Apply @NullMarked (JSpecify) at the package level", with no scope limiting it to new code. For the common case of a new class in an existing, unaudited package the two rules point opposite ways, and the file that "decides" offers no resolution — the scoping ("the rules above are written for new code") exists only in the chapter, which by this file's own division cannot carry rules.

Scoping the first rule in CONVENTIONS.md itself would settle it.

CONVENTIONS.md:164 · documentation

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, CONVENTIONS.md should settle this itself. The opening rule now applies package-level @NullMarked only to a new package. A new class in a package that is not null-marked yet gets @NullMarked on the class. That matches the class-by-class rule for existing packages.

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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 ! marker.

How a breaking change is declared is already defined in the commit-and-pr skill that this file delegates PR shape to: a ! in the title and a > [!WARNING] block in the description. The new rule instead says to list such a change as breaking "in the pull request and the release notes", which names no mechanism this repo has — the release digest classifies commits by conventional-commit type, so a Kotlin break mentioned only in prose never reaches it.

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 · conventions

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. The rule now uses the repo's existing markers: a ! in the commit and PR title and a > [!WARNING] block in the description, pointing to the skill linked from Commit & PR Hygiene. It still says that a Kotlin-only source break counts as breaking, even though Java callers without NullAway compile unchanged. The "release notes" wording is gone.

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
Expand Down
57 changes: 57 additions & 0 deletions guidelines/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<UI, Registration>` to
`SerializableFunction<UI, ? extends @Nullable Registration>` 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<UI, @Nullable Registration>` avoids the clash and
accepts lambdas, the usual argument, unchanged. Type arguments are invariant, though, so a variable
declared as `SerializableFunction<UI, Registration>` 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<String>)` 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,
Expand Down
Loading