Skip to content

Support val/var keywords in destructuring entries - #637

Closed
asapha wants to merge 3 commits into
Kotlin:mainfrom
asapha:asapha/fix-destructuring
Closed

asapha wants to merge 3 commits into
Kotlin:mainfrom
asapha:asapha/fix-destructuring

Conversation

@asapha

@asapha asapha commented Jun 29, 2026 •

Copy link
Copy Markdown
Contributor

Before the change, this code

fun f(d: D) {
  (val a) = d
}

raised the error

com.google.googlejavaformat.FormattingError: 2:4: error: expected token: 'val'; generated a instead

	at com.google.googlejavaformat.OpsBuilder.token(OpsBuilder.java:342)
	at com.facebook.ktfmt.format.KotlinInputAstVisitor.token(KotlinInputAstVisitor.kt:2899)
	at com.facebook.ktfmt.format.KotlinInputAstVisitor.token$default(KotlinInputAstVisitor.kt:2898)
	at com.facebook.ktfmt.format.KotlinInputAstVisitor.declareOne$lambda$0$0(KotlinInputAstVisitor.kt:1418)
	at com.facebook.ktfmt.format.KotlinInputAstVisitor.block(KotlinInputAstVisitor.kt:2924)
	at com.facebook.ktfmt.format.KotlinInputAstVisitor.block$default(KotlinInputAstVisitor.kt:2916)
	at com.facebook.ktfmt.format.KotlinInputAstVisitor.declareOne$lambda$0(KotlinInputAstVisitor.kt:1400)
	at com.facebook.ktfmt.format.KotlinInputAstVisitor.block(KotlinInputAstVisitor.kt:2924)
	at com.facebook.ktfmt.format.KotlinInputAstVisitor.block$default(KotlinInputAstVisitor.kt:2916)
	at com.facebook.ktfmt.format.KotlinInputAstVisitor.declareOne(KotlinInputAstVisitor.kt:1399)
	at com.facebook.ktfmt.format.KotlinInputAstVisitor.declareOne$default(KotlinInputAstVisitor.kt:1376)
	at com.facebook.ktfmt.format.KotlinInputAstVisitor.visitDestructuringDeclarationEntry(KotlinInputAstVisitor.kt:2435)
	at org.jetbrains.kotlin.psi.KtVisitorVoid.visitDestructuringDeclarationEntry(KtVisitorVoid.java:527)
	at org.jetbrains.kotlin.psi.KtVisitorVoid.visitDestructuringDeclarationEntry(KtVisitorVoid.java:10)
	at org.jetbrains.kotlin.psi.KtDestructuringDeclarationEntry.accept(KtDestructuringDeclarationEntry.java:93)
	at org.jetbrains.kotlin.psi.KtElementImpl.accept(KtElementImpl.java:38)
	at com.facebook.ktfmt.format.KotlinInputAstVisitor.visit(KotlinInputAstVisitor.kt:2952)
	at 
[…]

Fixes #632

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 29, 2026
@hick209

hick209 commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator

Don't forget to update the CHANGELOG.md

@meta-codesync

meta-codesync Bot commented Jun 29, 2026

Copy link
Copy Markdown

@hick209 has imported this pull request. If you are a Meta employee, you can view this in D110056810.

@hick209

hick209 commented Jun 29, 2026 •

Copy link
Copy Markdown
Collaborator

Edit: Nevermind, I had misread the issue. This is indeed the right fix! Great work 😄

@asapha
asapha force-pushed the asapha/fix-destructuring branch from 9a53f01 to 24c6ed2 Compare June 29, 2026 15:28
@asapha

asapha commented Jun 29, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, it should be OK now

@hick209

hick209 commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator

Internally we are still in Kotiln 2.2 (on the way to 2.3) so we got this compile issue here:

core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt:2441:49: error: unresolved reference 'ownValOrVarKeyword'.
        valOrVarKeyword = multiDeclarationEntry.ownValOrVarKeyword?.text,
                                                ^^^^^^^^^^^^^^^^^^

@asapha

asapha commented Jun 29, 2026

Copy link
Copy Markdown
Contributor Author

@hick209 Added an entry to CompatibilityUtils.kt and ran tests using 2.2. It seems OK.

Comment thread CHANGELOG.md Resolved
@asapha
asapha force-pushed the asapha/fix-destructuring branch from df833fc to 9ff8c32 Compare June 30, 2026 09:38
@asapha

asapha commented Jun 30, 2026

Copy link
Copy Markdown
Contributor Author

Rebased to edit the changelog

@cortinico cortinico left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review automatically exported from Phabricator review in Meta.

@meta-codesync meta-codesync Bot closed this in beabfde Jun 30, 2026
@meta-codesync meta-codesync Bot added the Merged label Jun 30, 2026
@meta-codesync

meta-codesync Bot commented Jun 30, 2026

Copy link
Copy Markdown

@hick209 merged this pull request in beabfde.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support Kotlin 2.4 name-based destructuring syntax

4 participants