Skip to content

Commit a17c88a

Browse files
Kotlin: exclude the by keyword from an interface-delegation field location
For `class C : Intf by <expr>` the compiler synthesises a `$$delegate_0` field that stores the delegate `<expr>`, so its natural location is that expression. The two frontends disagreed on the field's own location: - K1 records it at the delegate expression (`intfDelegate.kt:7:26:9:1`, the `object : Intf { ... }`); while - K2 records it from the delegated supertype, so it includes the `Intf by ` glue (`7:18:9:1`). This is the interface-delegation analogue of the delegated-property `$delegate` field, where we already adopted the delegate-expression span and excluded the `by` keyword (it is syntactic glue, not part of the stored expression). We apply the same principle here and converge K2 onto K1's narrower span. `getClassDelegateFieldLocation` anchors a class-delegation field (`IrDeclarationOrigin.DELEGATE`, which has no corresponding property and would otherwise fall through to the raw IR offset) on its own initialiser expression. It uses the initialiser's raw offsets, which are available under both frontends (`7:26:9:1` in each), so it is frontend-stable: under K1 it reproduces the offset already recorded (no change) and under K2 it trims the leading `Intf by `. Relearn (both suites, CPUS=5): all 3333 tests pass. Only test-kotlin2 changes; interface-delegate/test is now byte-identical across suites (2 -> 0 divergent rows). No other file changes. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent dc81b51 commit a17c88a

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2679,6 +2679,7 @@ open class KotlinFileExtractor(
26792679
?.takeUnless { it.isDelegated }
26802680
?.let { getPsiBasedLocation(it) }
26812681
?: getDelegateFieldLocation(f)
2682+
?: getClassDelegateFieldLocation(f)
26822683
?: tw.getLocation(f)
26832684
return extractField(
26842685
id,
@@ -3559,6 +3560,30 @@ open class KotlinFileExtractor(
35593560
return tw.getLocation(expression.startOffset, expression.endOffset)
35603561
}
35613562

3563+
/**
3564+
* Returns the location for an interface-delegation `$$delegate_0` backing field, or null to
3565+
* leave the raw IR offset in place.
3566+
*
3567+
* For `class C : Intf by <expr>` the compiler synthesises a `$$delegate_0` field that stores
3568+
* `<expr>`, so its natural location is that delegate expression. The K1 frontend's raw offset
3569+
* already spans the expression (`intfDelegate.kt:7:26:9:1`, the `object : Intf { ... }`); the
3570+
* K2 frontend's raw offset starts at the delegated supertype and so includes the `Intf by `
3571+
* glue (`7:18:9:1`). As with a delegated property's `$delegate` field, the `by` clause is
3572+
* syntactic glue rather than part of the stored expression, so we converge on the narrower
3573+
* delegate-expression span by anchoring the field on its own initialiser.
3574+
*
3575+
* This uses the field initialiser's raw offsets (available under both frontends), so it is
3576+
* frontend-stable: under K1 it reproduces the offset already recorded, and under K2 it trims
3577+
* the leading `Intf by `. It fires only for class-delegation fields (`IrDeclarationOrigin.DELEGATE`),
3578+
* which have no corresponding property and would otherwise fall through to the raw IR offset.
3579+
*/
3580+
private fun getClassDelegateFieldLocation(f: IrField): Label<DbLocation>? {
3581+
if (f.origin != IrDeclarationOrigin.DELEGATE) return null
3582+
val expression = f.initializer?.expression ?: return null
3583+
if (expression.startOffset < 0 || expression.endOffset < 0) return null
3584+
return tw.getLocation(expression.startOffset, expression.endOffset)
3585+
}
3586+
35623587
/**
35633588
* Reconstructs the location of an `if`/`when` branch [b] (of the enclosing [IrWhen] [w])
35643589
* from the branch's own condition/result offsets, or null to leave the raw location in

java/ql/test-kotlin2/library-tests/interface-delegate/test.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fields
2-
| intfDelegate.kt:7:18:9:1 | $$delegate_0 | intfDelegate.kt:7:26:9:1 | <Stmt> |
2+
| intfDelegate.kt:7:26:9:1 | $$delegate_0 | intfDelegate.kt:7:26:9:1 | <Stmt> |
33
#select
44
| intfDelegate.kt:0:0:0:0 | f | intfDelegate.kt:7:1:10:1 | Concrete |
55
| intfDelegate.kt:3:3:3:15 | f | intfDelegate.kt:1:1:5:1 | Intf |

0 commit comments

Comments
 (0)