Skip to content

Commit 6b0a1e4

Browse files
AbdullinAMSpace Team
authored andcommitted
[IR] Use sanitized names when calculating scopes for lambdas
^KT-80285 Fixed When calculating scopes for lambdas, we previously used non-sanitized versions of names, which caused names like `test @` and `test #` to be differentiated during scope calculation, but mangled together into `test__` (JVM) when generating names
1 parent 64daa7e commit 6b0a1e4

File tree

27 files changed

+192
-3
lines changed

27 files changed

+192
-3
lines changed

analysis/low-level-api-fir/tests-gen/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLBlackBoxTestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

analysis/low-level-api-fir/tests-gen/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLReversedBlackBoxTestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirLightTreeBlackBoxCodegenTestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/fir/fir2ir/tests-gen/org/jetbrains/kotlin/test/runners/codegen/FirPsiBlackBoxCodegenTestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/LocalDeclarationsLowering.kt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ open class LocalDeclarationsLowering(
135135
val newParameterToOld: MutableMap<IrValueParameter, IrValueParameter> = mutableMapOf(),
136136
val oldParameterToNew: MutableMap<IrValueParameter, IrValueParameter> = mutableMapOf(),
137137
) : BodyLoweringPass {
138-
internal val declarationScopesWithCounter: MutableMap<IrClass, MutableMap<Name, ScopeWithCounter>> = mutableMapOf()
138+
internal val declarationScopesWithCounter: MutableMap<IrClass, MutableMap<SanitizedName, ScopeWithCounter>> = mutableMapOf()
139139

140140
open val invalidChars: Set<Char>
141141
get() = emptySet()
@@ -213,6 +213,20 @@ open class LocalDeclarationsLowering(
213213
protected open fun IrClass.getConstructorsThatCouldCaptureParamsWithoutFieldCreating(): Iterable<IrConstructor> =
214214
listOfNotNull(primaryConstructor)
215215

216+
internal inner class SanitizedName(val name: Name) {
217+
val sanitizedName: String = localNameSanitizer(name.asString())
218+
219+
override fun hashCode(): Int = if (name.isSpecial) name.hashCode() else sanitizedName.hashCode()
220+
override fun equals(other: Any?): Boolean {
221+
if (this === other) return true
222+
if (other !is SanitizedName) return false
223+
return when {
224+
name.isSpecial -> name == other.name
225+
else -> sanitizedName == other.sanitizedName
226+
}
227+
}
228+
}
229+
216230
internal class ScopeWithCounter(val irElement: IrElement) {
217231
// Continuous numbering across all declarations in the container.
218232
var counter: Int = 0
@@ -226,12 +240,13 @@ open class LocalDeclarationsLowering(
226240
private fun IrField.getOrCreateScopeWithCounter(): ScopeWithCounter? {
227241
val klass = parentClassOrNull ?: return null
228242
return declarationScopesWithCounter.getOrPut(klass, ::mutableMapOf)
229-
.getOrPut(this.name) { ScopeWithCounter(this) }
243+
.getOrPut(SanitizedName(this.name)) { ScopeWithCounter(this) }
230244
}
245+
231246
private fun IrFunction.getOrCreateScopeWithCounter(): ScopeWithCounter? {
232247
val klass = parentClassOrNull ?: return null
233248
return declarationScopesWithCounter.getOrPut(klass, ::mutableMapOf)
234-
.getOrPut(this.name) { ScopeWithCounter(this) }
249+
.getOrPut(SanitizedName(this.name)) { ScopeWithCounter(this) }
235250
}
236251

237252
abstract class LocalContext {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// IGNORE_DEXING
2+
// IGNORE_BACKEND_K1: WASM, JS_IR, JS_IR_ES6
3+
// IGNORE_BACKEND: NATIVE
4+
// IGNORE_IR_DESERIALIZATION_TEST: NATIVE
5+
6+
fun `test unquoted string variable in }} json body`() {
7+
{}
8+
}
9+
10+
fun `test unquoted string variable in {{ json body`() {
11+
{}
12+
}
13+
14+
fun `test @`(): () -> String {
15+
return { "O" }
16+
}
17+
18+
fun `test #`(): () -> String {
19+
return { "K" }
20+
}
21+
22+
fun box(): String {
23+
return `test @`()() + `test #`()()
24+
}

compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/JvmAbiConsistencyTestBoxGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenTestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/IrBlackBoxCodegenWithIrInlinerTestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/codegen/inlineScopes/FirBlackBoxCodegenTestWithInlineScopesGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)