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
17 changes: 17 additions & 0 deletions wire-golden-files/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,23 @@ wire {
}
}

// The generated code must stay clean under the Kotlin compiler's extra checks.
// See https://github.com/square/wire/issues/3700 and https://github.com/square/wire/issues/3701.
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile::class.java).configureEach {
compilerOptions {
extraWarnings.set(true)
// KotlinPoet emits explicit visibility modifiers on purpose. Consumers which enable
// explicit API mode need them, so this check does not apply to generated code.
freeCompilerArgs.add("-Xwarning-level=REDUNDANT_VISIBILITY_MODIFIER:disabled")
// Fail the build when these checks flag generated code again. Plain allWarningsAsErrors
// is too broad here: it also fails on repo-wide compiler flag deprecation warnings.
freeCompilerArgs.add("-Xwarning-level=VARIABLE_INITIALIZER_IS_REDUNDANT:error")
freeCompilerArgs.add("-Xwarning-level=CAN_BE_VAL_DELAYED_INITIALIZATION:error")
freeCompilerArgs.add("-Xwarning-level=CAN_BE_VAL:error")
freeCompilerArgs.add("-Xwarning-level=CAN_BE_VAL_LATEINIT:error")
}
}

tasks.getByName("spotlessJava").dependsOn("generateMainProtos")
tasks.getByName("spotlessKotlin").dependsOn("generateMainProtos")
tasks.getByName("spotlessSwift").dependsOn("generateMainProtos")
5 changes: 1 addition & 4 deletions wire-golden-files/src/main/kotlin/Field.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ public class Field(
null,
"squareup/wire/hundreds_redacted.proto"
) {
override fun encodedSize(`value`: Field): Int {
var size = value.unknownFields.size
return size
}
override fun encodedSize(`value`: Field): Int = value.unknownFields.size

override fun encode(writer: ProtoWriter, `value`: Field) {
writer.writeBytes(value.unknownFields)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public class MutableHeader(
}

override fun hashCode(): Int {
var result = 0
result = unknownFields.hashCode()
var result = unknownFields.hashCode()
result = result * 37 + (id?.hashCode() ?: 0)
return result
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ public class MutablePacket(
}

override fun hashCode(): Int {
var result = 0
result = unknownFields.hashCode()
var result = unknownFields.hashCode()
result = result * 37 + (header_?.hashCode() ?: 0)
result = result * 37 + payload.hashCode()
return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ public class MutablePayload(
}

override fun hashCode(): Int {
var result = 0
result = unknownFields.hashCode()
var result = unknownFields.hashCode()
result = result * 37 + (preamble?.hashCode() ?: 0)
result = result * 37 + (content?.hashCode() ?: 0)
result = result * 37 + (type?.hashCode() ?: 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -889,10 +889,10 @@ class KotlinGenerator private constructor(
if (!mutableTypes) {
addStatement("var %N = super.hashCode", resultName)
beginControlFlow("if (%N == 0)", resultName)
addStatement("%N = unknownFields.hashCode()", resultName)
} else {
addStatement("var %N = 0", resultName)
addStatement("var %N = unknownFields.hashCode()", resultName)
}
addStatement("%N = unknownFields.hashCode()", resultName)

for (fieldOrOneOf in type.fieldsAndFlatOneOfFieldsAndBoxedOneOfs()) {
when (fieldOrOneOf) {
Expand Down Expand Up @@ -1784,8 +1784,13 @@ class KotlinGenerator private constructor(
val sizeName = localNameAllocator.newName("size")

val body = buildCodeBlock {
val fieldsAndOneOfs = message.fieldsAndFlatOneOfFieldsAndBoxedOneOfs()
if (fieldsAndOneOfs.isEmpty()) {
addStatement("return value.unknownFields.size")
return@buildCodeBlock
}
addStatement("var %N = value.unknownFields.size", sizeName)
for (fieldOrOneOf in message.fieldsAndFlatOneOfFieldsAndBoxedOneOfs()) {
for (fieldOrOneOf in fieldsAndOneOfs) {
when (fieldOrOneOf) {
is Field -> {
val fieldName = localNameAllocator[fieldOrOneOf]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2771,6 +2771,25 @@ class KotlinGeneratorTest {
assertContains(code, "result = result * 37 + list.hashCode()")
}

@Test fun encodedSizeFunctionWithoutFieldsHasNoLocalVariable() {
val schema = buildSchema {
add(
"message.proto".toPath(),
"""
|message NoFields {
|}
""".trimMargin(),
)
}
val code = KotlinWithProfilesGenerator(schema).generateKotlin("NoFields")
assertThat(code).contains(
"""
| override fun encodedSize(`value`: NoFields): Int = value.unknownFields.size
""".trimMargin(),
)
assertThat(code).doesNotContain("var size")
}

@Test
fun enumConstantConflictingDeclaration() {
val schema = buildSchema {
Expand Down Expand Up @@ -2909,7 +2928,8 @@ class KotlinGeneratorTest {
assertThat(code).contains("override var unknownFields: ByteString = ByteString.EMPTY")
assertThat(code).contains("MutableHeader#ADAPTER") // should refer to adapters of Mutable message types.
assertThat(code).contains("MutablePayload#ADAPTER")
assertThat(code).contains("var result = 0") // hashCode() is no longer calling super.hashCode().
// hashCode() is no longer calling super.hashCode(), and it has no redundant initializer.
assertThat(code).contains("var result = unknownFields.hashCode()")
assertThat(code).contains(
"throw UnsupportedOperationException(\"newBuilder() is unsupported for mutable message types\")",
)
Expand Down
Loading