Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle some burstValues corner cases #51

Merged
merged 2 commits into from
Oct 29, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,71 @@ class BurstKotlinPluginTest {
)
}

@Test
fun burstValuesWithOverlyLongNames() {
val result = compile(
sourceFile = SourceFile.kotlin(
"CoffeeTest.kt",
"""
import app.cash.burst.Burst
import app.cash.burst.burstValues
import kotlin.test.Test

const val x8192 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx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xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

@Burst
class CoffeeTest {
@Test
fun test(
x1: String = burstValues("a", x8192),
x2: String = burstValues("b", x8192),
x3: String = burstValues("c", x8192),
x4: String = burstValues("d", x8192),
x5: String = burstValues("e", x8192),
x6: String = burstValues("f", x8192),
x7: String = burstValues("g", x8192),
x8: String = burstValues("h", x8192),
) {
}
}
""",
),
)
assertEquals(KotlinCompilation.ExitCode.OK, result.exitCode, result.messages)

val baseClass = result.classLoader.loadClass("CoffeeTest")
assertThat(baseClass.testSuffixes).contains("1_a_b_c_d_e_f_g_${"x".repeat(1024 - 16)}")
assertThat(baseClass.testSuffixes).contains("255_${"x".repeat(1024 - 4)}")
}

@Test
fun burstValuesReferencesEarlierParameter() {
val result = compile(
sourceFile = SourceFile.kotlin(
"CoffeeTest.kt",
"""
import app.cash.burst.Burst
import app.cash.burst.burstValues
import kotlin.test.Test

@Burst
class CoffeeTest {
@Test
fun test(
p1: String = burstValues("a", "b"),
p2: String = burstValues("c", p1.uppercase()),
) {
}
}
""",
),
)
assertEquals(KotlinCompilation.ExitCode.COMPILATION_ERROR, result.exitCode, result.messages)
assertThat(result.messages).contains(
"CoffeeTest.kt:10:5 @Burst parameter may not reference other parameters",
)
}

private val Class<*>.testSuffixes: List<String>
get() = methods.mapNotNull {
when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import org.jetbrains.kotlin.ir.types.getClass
import org.jetbrains.kotlin.ir.util.classId
import org.jetbrains.kotlin.ir.util.deepCopyWithSymbols
import org.jetbrains.kotlin.ir.util.isEnumClass
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
import org.jetbrains.kotlin.name.NameUtils

internal sealed interface Argument {
Expand All @@ -45,8 +46,11 @@ internal sealed interface Argument {
/** A string that's safe to use in a declaration name. */
val name: String

/** Returns an expression that looks up this argument. */
/** Returns a new expression that looks up this argument. */
fun expression(): IrExpression

/** Visits this argument for validation. */
fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R
}

private class EnumValueArgument(
Expand All @@ -59,15 +63,23 @@ private class EnumValueArgument(

override fun expression() =
IrGetEnumValueImpl(original.startOffset, original.endOffset, type, value.symbol)

override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R {
return original.accept(visitor, data)
}
}

private class BurstValuesArgument(
private val declarationParent: IrDeclarationParent,
override val isDefault: Boolean,
override val name: String,
private val value: IrExpression,
val value: IrExpression,
) : Argument {
override fun expression() = value.deepCopyWithSymbols(declarationParent)

override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R {
return value.accept(visitor, data)
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
package app.cash.burst.kotlin

import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrGetValue
import org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI
import org.jetbrains.kotlin.ir.visitors.IrElementTransformer

internal class Specialization(
/** The argument values for this specialization. */
Expand All @@ -37,6 +41,11 @@ internal fun specializations(
): List<Specialization> {
val parameterArguments = parameters.map { parameter ->
pluginContext.allPossibleArguments(parameter, burstApis)
.also { arguments ->
for (argument in arguments) {
argument.accept(ArgumentValidator(parameters, parameter), Unit)
}
}
}

val specializations = parameterArguments.cartesianProduct().map { arguments ->
Expand All @@ -46,13 +55,45 @@ internal fun specializations(
)
}

// If all elements already have distinct names, we're done.
if (specializations.distinctBy { it.name }.size == specializations.size) {
// If all elements already have distinct, short-enough names, we're done.
if (
specializations.distinctBy { it.name }.size == specializations.size &&
specializations.all { it.name.length < NAME_MAX_LENGTH }
) {
return specializations
}

// Otherwise, prefix each with its index.
return specializations.mapIndexed { index, specialization ->
Specialization(specialization.arguments, "${index}_${specialization.name}")
Specialization(
arguments = specialization.arguments,
name = "${index}_${specialization.name}".take(NAME_MAX_LENGTH),
)
}
}

internal class ArgumentValidator(
private val parameters: List<IrValueParameter>,
private val element: IrValueParameter,
) : IrElementTransformer<Unit> {
/**
* Confirm `burstValues()` don't reference other parameters. If we don't validate this here we'll
* get an ugly compiler crash because the referenced parameter won't be visible.
*/
override fun visitGetValue(expression: IrGetValue, data: Unit): IrExpression {
if (parameters.any { it.symbol == expression.symbol }) {
unexpectedParameterReference(element)
}
return super.visitGetValue(expression, data)
}
}

private fun unexpectedParameterReference(element: IrElement): Nothing {
throw BurstCompilationException(
"@Burst parameter may not reference other parameters",
element,
)
}

/** Strictly speaking Java symbol names may up to 64 KiB, but this is an ergonomic limit. */
private const val NAME_MAX_LENGTH = 1024