Skip to content

Preserve explicit variable types in UnnamedVariable when required. - #6322

Open
copybara-service[bot] wants to merge 1 commit into
masterfrom
test_1016302089
Open

copybara-service[bot] wants to merge 1 commit into
masterfrom
test_1016302089

Conversation

@copybara-service

Copy link
Copy Markdown
Contributor

Preserve explicit variable types in UnnamedVariable when required.

Per JLS 14.4.1, var cannot infer a variable's type when the initializer is an array initializer ({...}), has null or void type, or is a poly lambda or method reference expression (including inside parenthesized, conditional, or switch expressions). UnnamedVariable previously replaced the explicit type with var whenever an initializer was present, producing uncompilable var _ = ... declarations.

Check whether the initializer has a standalone inferable type before replacing the declared type with var, falling back to renaming only the identifier to _ when the explicit type is required.

Per JLS 14.4.1, `var` cannot infer a variable's type when the initializer is an array initializer (`{...}`), has `null` or `void` type, or is a poly lambda or method reference expression (including inside parenthesized, conditional, or switch expressions). `UnnamedVariable` previously replaced the explicit type with `var` whenever an initializer was present, producing uncompilable `var _ = ...` declarations.

Check whether the initializer has a standalone inferable type before replacing the declared type with `var`, falling back to renaming only the identifier to `_` when the explicit type is required.

PiperOrigin-RevId: 1016302089

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant