Repository navigation
Description
Split out of the #2660 audit (Batch B.2 of #2669). #2660 listed kotlin as a candidate for the #2651 ^[ \t]* → ^(?![ \t]) anchor change. It is a real defect, but that fix is wrong here — it deletes the true positive along with the false one. Filing separately so #2660 can close on go alone.
Current rule
"globals": re.compile(r"\b(object|companion\s+object)\b|^[ \t]*(?:const[ \t]+)?val\s+[A-Z_0-9]+[ \t]*=")The second alternative accepts any indentation, so it counts both of these:
class Foo {
companion object {
const val LIMIT = 5 // <- a real global. THE idiomatic Kotlin global constant.
}
fun f() {
val TIMEOUT = 3000 // <- false positive: a function-local.
}
}Scan of that file: now = 3 → ['companion object', 'const val LIMIT =', 'val TIMEOUT ='].
With #2651's ^(?![ \t]) anchor: fixed = 1 → ['companion object']. It drops the constant and keeps only the enclosing keyword. So the anchor change trades a rare false positive (an uppercase function-local val) for losing the commonest way Kotlin declares a global.
A non-object class property is the second false-positive class, and it is the same shape:
class Foo {
val TIMEOUT = 3000 // instance property, not a global -- currently counts
}Fix shape
Indentation is the wrong discriminator; the const modifier is the right one. Kotlin permits const val only on a top-level property or on a member of an object / companion object — it is a compile error inside a function body or on a property of a regular class. So const val at any indentation is always a global, and a bare val is only a global at top level. Split the one alternative in two:
"globals": re.compile(
r"\b(object|companion\s+object)\b"
r"|^[ \t]*const[ \t]+val\s+[A-Za-z_]\w*[ \t]*=" # const val: any indentation -- language guarantees global
r"|^(?![ \t])(?:const[ \t]+)?val\s+[A-Z_0-9]+[ \t]*=" # bare val: top level only
)Note the const val branch drops the [A-Z_0-9]+ SCREAMING_CASE restriction, because const val is a global whatever it is named; the bare-val branch keeps it, since that is what makes an unqualified top-level val read as a constant rather than an ordinary property.
Verification
| case | now | fixed | want |
|---|---|---|---|
top-level const val MAX = 10 |
1 | 1 | 1 ✅ |
top-level val REGISTRY = mutableMapOf… |
1 | 1 | 1 ✅ |
companion object { const val LIMIT = 5 } |
2 | 2 | 2 ✅ |
object Cfg { const val TIMEOUT = 30 } |
2 | 2 | 2 ✅ |
fun f() { val TIMEOUT = 3000 } |
1 | 0 | 0 ✅ |
class Foo { val TIMEOUT = 3000 } |
1 | 0 | 0 ✅ |
Corpus regression — no rebless needed, neither corpus moves:
- keyword-rosetta
data/kotlin/:globals2 → 2 (its planted 2 areobject Region {}/object Home {}, both alternative 1) - language-crucible
data/kotlin/(real okhttp): unchanged
ReDoS probe on the new quantified alternative, 100k-char identifier after const val: 0.0005s.
The corpora cannot validate this fix, and that is worth stating plainly: rosetta's kotlin globals comes entirely from the object keyword, and the real okhttp sources contain no const val at all. The evidence above is the Kotlin language rule (a const val placement guarantee) plus constructed cases, not incidence. Tests should carry all six rows above.
Not in scope
Alternative 1 counts anonymous object expressions (object : Runnable { … } inside a function) as globals. Real, separate, and untouched by this fix.
Activity
chore(crucible): re-bless golden masters for the B.3 wave-1 regex batch
fix(core-engine): Rosetta batch 3 wave 1 — five registry-regex fixes …
squid-protocol commentedon Sep 3, 2026
Closing: verified fixed on main. An indented
const valinside a companion object is counted as a global, while an indented barevalis not — the discriminator is theconstmodifier, not the column, exactly as the B.2 analysis proposed (Kotlin permitsconst valonly at top level / object / companion object). Landed in #2677.