Skip to content

Commit f1ccb6b

Browse files
Kotlin: converge delegated-setter value-parameter name to <set-?>
The synthetic setter of a *delegated* property has one implicit value parameter. The K2 frontend names it `<set-?>` (`SpecialNames.IMPLICIT_SETTER_PARAMETER`), matching what it does for an ordinary member property's setter. The K1 frontend instead names a delegated property's setter parameter `value`, diverging from both K2 and from its own naming of member-property setters. This produced two divergent rows per delegated `var` between test-kotlin1 and test-kotlin2 (the `[Parameter]` declaration and the `[VarAccess]` that reads it back inside the generated setter body): - [Parameter] value -> [Parameter] <set-?> - [VarAccess] value -> [VarAccess] <set-?> Decision: adopt the K2 name. `<set-?>` is the canonical implicit setter-parameter name the frontend already uses everywhere else, so it is the more consistent and less surprising choice; a bare `value` is indistinguishable from a user-written parameter of that name. `getConvergedValueParameterName` renames the parameter only when its enclosing function has origin `DELEGATED_PROPERTY_ACCESSOR` *and* its K1 source name is `value`. The origin guard restricts the rename to the fully synthetic delegated accessors (a hand-written `ReadWriteProperty.setValue` has origin `DEFINED`, so its `value` parameter is untouched); the name guard restricts it to the setter value parameter, leaving the accessor's receiver parameters (`<this>` for an extension property, `<dispatchReceiver>` for a member property) unchanged. Under K2 the setter parameter is already `<set-?>` and receivers are `<this>`, so nothing matches and the canonical K2 output is byte-for-byte unchanged (only the three K1 setters in test-kotlin1 relearn). The literal `<set-?>` is used because `SpecialNames.IMPLICIT_SETTER_PARAMETER` is not resolvable in every compiler version the extractor is built against. Relearned both suites (all 3333 tests pass); only the three delegated setters in test-kotlin1 (delegatedProperties.kt lines 19, 34, 82) change. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent a17c88a commit f1ccb6b

3 files changed

Lines changed: 40 additions & 10 deletions

File tree

java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1439,7 +1439,7 @@ open class KotlinFileExtractor(
14391439
return extractValueParameter(
14401440
id,
14411441
substitutedType,
1442-
vp.name.asString(),
1442+
getConvergedValueParameterName(vp),
14431443
location,
14441444
parent,
14451445
idx,
@@ -3168,6 +3168,36 @@ open class KotlinFileExtractor(
31683168
return tw.getLocation(keyword.startOffset, vp.endOffset)
31693169
}
31703170

3171+
/**
3172+
* Returns the name to record for a value parameter, converging the two frontends where they
3173+
* disagree.
3174+
*
3175+
* A property's synthetic setter has a single implicit value parameter. The K2 frontend names
3176+
* it `<set-?>`; the K1 frontend does the same for a
3177+
* member property's setter but names a *delegated* property's setter parameter `value`. That
3178+
* parameter is always compiler-synthesised (a delegated-property accessor carries no
3179+
* source-written parameters), so renaming it can never affect a user-written `value` parameter
3180+
* (for example the `value` parameter of a hand-written `ReadWriteProperty.setValue`, whose
3181+
* function has origin `DEFINED`). We therefore converge the delegated-setter parameter onto the
3182+
* canonical `<set-?>` name (the `SpecialNames.IMPLICIT_SETTER_PARAMETER` constant is not present
3183+
* in every supported compiler version, so the literal is used directly).
3184+
*
3185+
* The rename is guarded on the K1 source name `value` so it targets *only* the setter value
3186+
* parameter: a delegated accessor's other parameters are receivers (`<this>` for an extension
3187+
* property, `<dispatchReceiver>` for a member property), which must keep their names. Under K2
3188+
* the setter parameter is already `<set-?>` and the receivers are `<this>`, so none match and
3189+
* the canonical K2 output is left untouched.
3190+
*/
3191+
private fun getConvergedValueParameterName(vp: IrValueParameter): String {
3192+
if (
3193+
(vp.parent as? IrFunction)?.origin == IrDeclarationOrigin.DELEGATED_PROPERTY_ACCESSOR &&
3194+
vp.name.asString() == "value"
3195+
) {
3196+
return "<set-?>"
3197+
}
3198+
return vp.name.asString()
3199+
}
3200+
31713201
/**
31723202
* Returns the PSI-based location for a *synthesised* value parameter that has no source
31733203
* token of its own, matching the span the K2 frontend emits natively, or null to leave the

java/ql/test-kotlin1/library-tests/exprs/PrintAst.expected

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ delegatedProperties.kt:
294294
# 19| 2: [Method] <set-varResource1>
295295
# 19| 3: [TypeAccess] Unit
296296
#-----| 4: (Parameters)
297-
# 19| 0: [Parameter] value
297+
# 19| 0: [Parameter] <set-?>
298298
# 19| 0: [TypeAccess] int
299299
# 19| 5: [BlockStmt] { ... }
300300
# 19| 0: [ReturnStmt] return ...
@@ -328,7 +328,7 @@ delegatedProperties.kt:
328328
# 19| 0: [VarAccess] a0
329329
# 19| -3: [TypeAccess] KMutableProperty0<Integer>
330330
# 19| 0: [TypeAccess] Integer
331-
# 19| 2: [VarAccess] value
331+
# 19| 2: [VarAccess] <set-?>
332332
# 20| 1: [ExprStmt] <Expr>;
333333
# 20| 0: [MethodCall] println(...)
334334
# 20| -1: [TypeAccess] ConsoleKt
@@ -538,7 +538,7 @@ delegatedProperties.kt:
538538
# 34| 2: [Method] <set-readWrite>
539539
# 34| 3: [TypeAccess] Unit
540540
#-----| 4: (Parameters)
541-
# 34| 0: [Parameter] value
541+
# 34| 0: [Parameter] <set-?>
542542
# 34| 0: [TypeAccess] int
543543
# 34| 5: [BlockStmt] { ... }
544544
# 34| 0: [ReturnStmt] return ...
@@ -572,7 +572,7 @@ delegatedProperties.kt:
572572
# 34| 0: [VarAccess] a0
573573
# 34| -3: [TypeAccess] KMutableProperty0<Integer>
574574
# 34| 0: [TypeAccess] Integer
575-
# 34| 2: [VarAccess] value
575+
# 34| 2: [VarAccess] <set-?>
576576
# 36| 7: [ExprStmt] <Expr>;
577577
# 36| 0: [MethodCall] println(...)
578578
# 36| -1: [TypeAccess] ConsoleKt
@@ -1848,7 +1848,7 @@ delegatedProperties.kt:
18481848
# 82| 2: [Method] <set-delegatedToMember3>
18491849
# 82| 3: [TypeAccess] Unit
18501850
#-----| 4: (Parameters)
1851-
# 82| 0: [Parameter] value
1851+
# 82| 0: [Parameter] <set-?>
18521852
# 82| 0: [TypeAccess] int
18531853
# 82| 5: [BlockStmt] { ... }
18541854
# 82| 0: [ReturnStmt] return ...
@@ -1884,7 +1884,7 @@ delegatedProperties.kt:
18841884
# 82| 0: [VarAccess] a0
18851885
# 82| -3: [TypeAccess] KMutableProperty0<Integer>
18861886
# 82| 0: [TypeAccess] Integer
1887-
# 82| 3: [VarAccess] value
1887+
# 82| 3: [VarAccess] <set-?>
18881888
# 83| 1: [ExprStmt] <Expr>;
18891889
# 83| 0: [MethodCall] fn(...)
18901890
# 83| -1: [ThisAccess] this

java/ql/test-kotlin1/library-tests/exprs/exprs.expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
| delegatedProperties.kt:19:34:19:51 | ...::... | delegatedProperties.kt:19:9:19:51 | <set-varResource1> | PropertyRefExpr |
5959
| delegatedProperties.kt:19:34:19:51 | <get-varResource1>(...) | delegatedProperties.kt:19:34:19:51 | get | MethodCall |
6060
| delegatedProperties.kt:19:34:19:51 | <get-varResource1>(...) | delegatedProperties.kt:19:34:19:51 | get | MethodCall |
61+
| delegatedProperties.kt:19:34:19:51 | <set-?> | delegatedProperties.kt:19:9:19:51 | <set-varResource1> | VarAccess |
6162
| delegatedProperties.kt:19:34:19:51 | <set-varResource1>(...) | delegatedProperties.kt:19:34:19:51 | set | MethodCall |
6263
| delegatedProperties.kt:19:34:19:51 | <set-varResource1>(...) | delegatedProperties.kt:19:34:19:51 | set | MethodCall |
6364
| delegatedProperties.kt:19:34:19:51 | Integer | delegatedProperties.kt:19:9:19:51 | <get-varResource1> | TypeAccess |
@@ -82,7 +83,6 @@
8283
| delegatedProperties.kt:19:34:19:51 | setValue(...) | delegatedProperties.kt:19:9:19:51 | <set-varResource1> | MethodCall |
8384
| delegatedProperties.kt:19:34:19:51 | this | delegatedProperties.kt:19:34:19:51 | invoke | ThisAccess |
8485
| delegatedProperties.kt:19:34:19:51 | this | delegatedProperties.kt:19:34:19:51 | invoke | ThisAccess |
85-
| delegatedProperties.kt:19:34:19:51 | value | delegatedProperties.kt:19:9:19:51 | <set-varResource1> | VarAccess |
8686
| delegatedProperties.kt:19:34:19:51 | varResource1$delegate | delegatedProperties.kt:19:9:19:51 | <get-varResource1> | VarAccess |
8787
| delegatedProperties.kt:19:34:19:51 | varResource1$delegate | delegatedProperties.kt:19:9:19:51 | <set-varResource1> | VarAccess |
8888
| delegatedProperties.kt:20:9:20:29 | ConsoleKt | delegatedProperties.kt:18:5:40:5 | fn | TypeAccess |
@@ -166,6 +166,7 @@
166166
| delegatedProperties.kt:34:31:34:48 | ...::... | delegatedProperties.kt:34:9:34:48 | <set-readWrite> | PropertyRefExpr |
167167
| delegatedProperties.kt:34:31:34:48 | <get-readWrite>(...) | delegatedProperties.kt:34:31:34:48 | get | MethodCall |
168168
| delegatedProperties.kt:34:31:34:48 | <get-readWrite>(...) | delegatedProperties.kt:34:31:34:48 | get | MethodCall |
169+
| delegatedProperties.kt:34:31:34:48 | <set-?> | delegatedProperties.kt:34:9:34:48 | <set-readWrite> | VarAccess |
169170
| delegatedProperties.kt:34:31:34:48 | <set-readWrite>(...) | delegatedProperties.kt:34:31:34:48 | set | MethodCall |
170171
| delegatedProperties.kt:34:31:34:48 | <set-readWrite>(...) | delegatedProperties.kt:34:31:34:48 | set | MethodCall |
171172
| delegatedProperties.kt:34:31:34:48 | Integer | delegatedProperties.kt:34:9:34:48 | <get-readWrite> | TypeAccess |
@@ -193,7 +194,6 @@
193194
| delegatedProperties.kt:34:31:34:48 | setValue(...) | delegatedProperties.kt:34:9:34:48 | <set-readWrite> | MethodCall |
194195
| delegatedProperties.kt:34:31:34:48 | this | delegatedProperties.kt:34:31:34:48 | invoke | ThisAccess |
195196
| delegatedProperties.kt:34:31:34:48 | this | delegatedProperties.kt:34:31:34:48 | invoke | ThisAccess |
196-
| delegatedProperties.kt:34:31:34:48 | value | delegatedProperties.kt:34:9:34:48 | <set-readWrite> | VarAccess |
197197
| delegatedProperties.kt:36:9:36:29 | ConsoleKt | delegatedProperties.kt:18:5:40:5 | fn | TypeAccess |
198198
| delegatedProperties.kt:36:9:36:29 | println(...) | delegatedProperties.kt:18:5:40:5 | fn | MethodCall |
199199
| delegatedProperties.kt:36:17:36:28 | getVarResource0(...) | delegatedProperties.kt:18:5:40:5 | fn | MethodCall |
@@ -777,6 +777,7 @@
777777
| delegatedProperties.kt:82:40:82:54 | <dispatchReceiver> | delegatedProperties.kt:82:40:82:54 | | VarAccess |
778778
| delegatedProperties.kt:82:40:82:54 | <get-delegatedToMember3>(...) | delegatedProperties.kt:82:40:82:54 | get | MethodCall |
779779
| delegatedProperties.kt:82:40:82:54 | <get-delegatedToMember3>(...) | delegatedProperties.kt:82:40:82:54 | get | MethodCall |
780+
| delegatedProperties.kt:82:40:82:54 | <set-?> | delegatedProperties.kt:82:9:82:54 | <set-delegatedToMember3> | VarAccess |
780781
| delegatedProperties.kt:82:40:82:54 | <set-delegatedToMember3>(...) | delegatedProperties.kt:82:40:82:54 | set | MethodCall |
781782
| delegatedProperties.kt:82:40:82:54 | <set-delegatedToMember3>(...) | delegatedProperties.kt:82:40:82:54 | set | MethodCall |
782783
| delegatedProperties.kt:82:40:82:54 | Integer | delegatedProperties.kt:81:5:84:5 | fn | TypeAccess |
@@ -819,7 +820,6 @@
819820
| delegatedProperties.kt:82:40:82:54 | this.<dispatchReceiver> | delegatedProperties.kt:82:40:82:54 | | VarAccess |
820821
| delegatedProperties.kt:82:40:82:54 | this.<dispatchReceiver> | delegatedProperties.kt:82:40:82:54 | get | VarAccess |
821822
| delegatedProperties.kt:82:40:82:54 | this.<dispatchReceiver> | delegatedProperties.kt:82:40:82:54 | set | VarAccess |
822-
| delegatedProperties.kt:82:40:82:54 | value | delegatedProperties.kt:82:9:82:54 | <set-delegatedToMember3> | VarAccess |
823823
| delegatedProperties.kt:83:9:83:12 | fn(...) | delegatedProperties.kt:81:5:84:5 | fn | MethodCall |
824824
| delegatedProperties.kt:83:9:83:12 | this | delegatedProperties.kt:81:5:84:5 | fn | ThisAccess |
825825
| delegatedProperties.kt:87:1:87:46 | MyClass | file://:0:0:0:0 | <none> | TypeAccess |

0 commit comments

Comments
 (0)