Skip to content

Add nullability annotations to IndexSearcher APIs#14132

Open
Edarke wants to merge 2 commits intoapache:mainfrom
Edarke:add-nullable-annotations
Open

Add nullability annotations to IndexSearcher APIs#14132
Edarke wants to merge 2 commits intoapache:mainfrom
Edarke:add-nullable-annotations

Conversation

@Edarke
Copy link
Copy Markdown

@Edarke Edarke commented Jan 10, 2025

Annotate public APIs with an internal definition of @Nullable where appropriate.

This proposal is a follow up from the discussion here. This change adds an internal copy of JSpecify's @Nullable to lucene-util and uses Intellij's "infer nullability" feature to automatically apply the annotation to IndexSearcher and its dependencies. If this looks good, there will be a follow up PR to automatically annotate other APIs.

The motivation for this change is to increase the null safety of downstream projects using Lucene by allowing users to configure a javac plugin to lint their build. This change was tested with NullAway, which recognizes any annotation with the simple class name @Nullable

@github-actions
Copy link
Copy Markdown
Contributor

This PR has not had activity in the past 2 weeks, labeling it as stale. If the PR is waiting for review, notify the dev@lucene.apache.org list. Thank you for your contribution!

@github-actions github-actions Bot added the Stale label Jan 25, 2025
@msokolov
Copy link
Copy Markdown
Contributor

This seems reasonable to me, but it implies a future promise to maintain it, and I don't know how we would ever know if we added some new usage that isn't properly annotated

@rmuir
Copy link
Copy Markdown
Member

rmuir commented Jan 27, 2025

For the java language:

Specifying that something is nullable doesn't provide any value: all types are nullable by default already.

Specifying that something is NOT-nullable would allow for additional smarts: it is the only possible way to do "gradual nullness typing".

I agree too, that we would need linter to verify this stuff in builds. Maybe the errorprone could do it.

@msokolov
Copy link
Copy Markdown
Contributor

Specifying that something is nullable doesn't provide any value: all types are nullable by default already.

I guess these null-checking systems impose their own assumptions and assume that anything not annotated with @nullable is not-nullable. Clearly the language doesn't enforce that, but maybe some build-time processor can check for it? Also, the proposal here seems to have been to annotate everything all at once, not gradual, but I agree it seems more sensible to assume everything can be null, indicate @NotNull and then enforce that.

@rmuir
Copy link
Copy Markdown
Member

rmuir commented Jan 27, 2025

One option we could do for correctness is to turn on ecj's null analysis. ecj is pretty fast and runs as part of gradle checks already, and it is the compiler often using this feature in the IDEs anyway.

https://github.com/apache/lucene/blob/main/gradle/validation/ecj-lint/ecj.javadocs.prefs#L2-L10

@rmuir
Copy link
Copy Markdown
Member

rmuir commented Jan 27, 2025

I will look in on the ECJ side, last time I used it, their null analysis had issues on lucene's enormous codebase. It was many years ago though, maybe it has solidified.

error-prone seems to only support Nullable-style vs NotNullable-style annotations, but maybe just reflects google's choices. It does have some good checks though, I esp like the ones where Nullable is interpreted differently by different tools, e.g.
https://errorprone.info/bugpattern/NullableWildcard
https://errorprone.info/bugpattern/NullableTypeParameter

@github-actions github-actions Bot removed the Stale label Jan 28, 2025
@github-actions
Copy link
Copy Markdown
Contributor

This PR has not had activity in the past 2 weeks, labeling it as stale. If the PR is waiting for review, notify the dev@lucene.apache.org list. Thank you for your contribution!

@github-actions github-actions Bot added the Stale label Feb 11, 2025
@github-actions github-actions Bot removed the Stale label Oct 1, 2025
@github-actions
Copy link
Copy Markdown
Contributor

This PR has not had activity in the past 2 weeks, labeling it as stale. If the PR is waiting for review, notify the dev@lucene.apache.org list. Thank you for your contribution!

@github-actions github-actions Bot added the Stale label Oct 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants