Skip to content

Keep wire-generated Kotlin clean under extra compiler checks - #3704

Open
oldergod wants to merge 1 commit into
masterfrom
bquenaudon/kotlin-2-1-extra-warnings
Open

Keep wire-generated Kotlin clean under extra compiler checks#3704
oldergod wants to merge 1 commit into
masterfrom
bquenaudon/kotlin-2-1-extra-warnings

Conversation

@oldergod

@oldergod oldergod commented Sep 1, 2026

Copy link
Copy Markdown
Member

Kotlin 2.1 added extra compiler checks (-Wextra). Two of them flag wire-generated code. Users cannot fix generated code, so the fix belongs in wire.

Fixes #3700
Fixes #3701

Changes

hashCode() for mutable types (#3701, VARIABLE_INITIALIZER_IS_REDUNDANT). The generator emitted var result = 0, and the next line overwrote the value with unknownFields.hashCode(). The generator now initializes the variable with unknownFields.hashCode() directly. The immutable path is unchanged: its super.hashCode initializer is read by the cache check.

encodedSize() for messages with no fields (#3700, CAN_BE_VAL_DELAYED_INITIALIZATION). The body degenerated to var size = value.unknownFields.size; return size. The generator now emits return value.unknownFields.size with no local variable. KotlinPoet renders this as an expression body.

Regression gate. wire-golden-files now compiles with extraWarnings enabled. Four diagnostics fail the build via -Xwarning-level=<NAME>:error: VARIABLE_INITIALIZER_IS_REDUNDANT, CAN_BE_VAL_DELAYED_INITIALIZATION, CAN_BE_VAL, and CAN_BE_VAL_LATEINIT. Plain allWarningsAsErrors is too broad here: it also fails on repo-wide compiler warnings that are unrelated to the goldens, for example the deprecated -Xjvm-default flag spelling and the deprecated language version 2.0. REDUNDANT_VISIBILITY_MODIFIER is disabled for this module: KotlinPoet emits explicit visibility modifiers on purpose, and consumers with explicit API mode need them. Without this, it fires 984 times on the goldens.

Verification

  • Gate discrimination: with the gate and the old generator, :wire-golden-files:compileKotlin fails with exactly the four expected sites (three in hashCode, one in encodedSize). With the fixed generator, the build passes.
  • The compiler rejects unknown diagnostic names with an error, so a green build proves the four escalated names are valid.
  • :wire-kotlin-generator:test: 75 tests, 0 failures, forced fresh with --rerun-tasks --no-build-cache.
  • spotlessCheck and apiCheck pass. The golden files change shape only. There is no runtime or .api change.
  • All checked-in generated Kotlin lives in wire-golden-files. A repo-wide grep found no other fixture with the old shapes.

Kotlin 2.1 added extra compiler checks (-Wextra). Two of them flag
wire-generated code, which users cannot edit.

VARIABLE_INITIALIZER_IS_REDUNDANT: hashCode() for mutable types
initialized the result variable with 0, then the next line overwrote
it. The variable now starts with unknownFields.hashCode().

CAN_BE_VAL_DELAYED_INITIALIZATION: encodedSize() for a message with no
fields declared a var, never updated it, and returned it. The function
now returns value.unknownFields.size directly.

The golden files module now compiles with extraWarnings enabled, and
these warning families fail the build. REDUNDANT_VISIBILITY_MODIFIER
is disabled there: KotlinPoet emits explicit visibility modifiers on
purpose, because consumers with explicit API mode need them.

Generated code changes shape only. There is no runtime or .api change.

Fixes #3700
Fixes #3701

Co-authored-by: Benoît Quenaudon <benoit@quenaudon.com>
Signed-off-by: Benoît Quenaudon <benoit@quenaudon.com>
@oldergod
oldergod marked this pull request as ready for review September 1, 2026 10:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

VARIABLE_INITIALIZER_IS_REDUNDANT warning for generated hashCode fun CAN_BE_VAL_DELAYED_INITIALIZATION warning for generated encodedSize fun

1 participant