Skip to content

Conversation

copybara-service[bot]
Copy link

Issue #2388: Fixed translation of Java 16+ pattern matching in instanceof expressions within if statements.

This change introduces a needsCastChk flag to CastExpression to prevent redundant runtime cast checks. When translating if (expr instanceof Type var), a local variable var is declared and initialized with a cast of expr. Since the instanceof check already guarantees type safety, the generated cast does not need an additional runtime check. For complex conditions like if (expr instanceof Type var && otherCondition), the translation now creates a nested if statement for otherCondition inside the block where var is in scope.

@copybara-service copybara-service bot force-pushed the test_812930294 branch 2 times, most recently from c24673b to 3ddd4d5 Compare October 1, 2025 23:58
…nceof` expressions within `if` statements.

This change introduces a `needsCastChk` flag to `CastExpression` to prevent redundant runtime cast checks. When translating `if (expr instanceof Type var)`, a local variable `var` is declared and initialized with a cast of `expr`. Since the `instanceof` check already guarantees type safety, the generated cast does not need an additional runtime check. For complex conditions like `if (expr instanceof Type var && otherCondition)`, the translation now creates a nested `if` statement for `otherCondition` inside the block where `var` is in scope.

PiperOrigin-RevId: 813986197
@copybara-service copybara-service bot merged commit 6a1c87c into master Oct 2, 2025
@copybara-service copybara-service bot deleted the test_812930294 branch October 2, 2025 00:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant