"Cannot resolve symbol al" after `else if (! (l instanceof ArrayList al))`
Chen Liang
chen.l.liang at oracle.com
Sun Jun 8 00:47:22 UTC 2025
Hi all,
First, note the original example is equivalent to below:
if (l instanceof LinkedList) {
throw new IllegalArgumentException("Use an efficient list implementation");
} else {
if (!(l instanceof ArrayList al)) {
return;
}
}
Which should be more clarifying about why the language has this behavior. Variables defined in a block is not available outside of that block.
From the specification perspective, currently, JLS 6.3.2.2 "if Statements" says:
A pattern variable is introduced by if (e) S else T iff either: ... It is introduced by e when false, and S cannot complete normally, and T can complete normally.
This means that a pattern variable introduced by statement T (here the nested if statement after else) is not introduced by the (main) if statement, and this is working as intended per JLS.
I personally think the status quo is fine - allowing variables to leak beyond blocks is risky and may introduce source incompatibility; even if we commit to such a change, we have too many statements to update (for, if, do, while, etc...) and is also error-prone.
Regards,
Chen Liang
________________________________
From: amber-dev <amber-dev-retn at openjdk.org> on behalf of David Alayachew <davidalayachew at gmail.com>
Sent: Saturday, June 7, 2025 9:55 AM
To: Jean-Noël Rouvignac <jean-noel.rouvignac at pingidentity.com>; Angelos Bimpoudis <angelos.bimpoudis at oracle.com>
Cc: amber-dev <amber-dev at openjdk.org>
Subject: Re: "Cannot resolve symbol al" after `else if (! (l instanceof ArrayList al))`
This definitely looks like a bug. Maybe some of the amber folks are busy with Valhalla or something, but I can't see this NOT being a bug.
@Angelos Bimpoudis<mailto:angelos.bimpoudis at oracle.com> poke poke poke
On Sat, Jun 7, 2025, 3:14 AM Jean-Noël Rouvignac <jean-noel.rouvignac at pingidentity.com<mailto:jean-noel.rouvignac at pingidentity.com>> wrote:
Hello folks,
JDK 25 rampdown phase 1 is now behind us, congrats and thanks for all the work done to get there!
I am reviving this use case to see if anyone agrees that this may be a bug?
Thank you,
Jean-Noel
On Wed, Apr 16, 2025 at 11:37 AM Jean-Noël Rouvignac <jean-noel.rouvignac at pingidentity.com<mailto:jean-noel.rouvignac at pingidentity.com>> wrote:
Hello amber-dev folks!
While using pattern matching for instanceof I noticed a small incoherent oddity.
Take this code:
```
static void ensureCapacity(List l) {
if (l instanceof LinkedList) {
throw new IllegalArgumentException("Use an efficient list implementation");
} else if (!(l instanceof ArrayList al)) {
return;
}
al.ensureCapacity(10);
System.out.println("done");
}
ensureCapacity(new ArrayList());
```
The compiler rejects this code with: `Cannot resolve symbol 'al'`.
(I have tested that code with JDK 24 in the playground (https://dev.java/playground) but also in my IDE with JDK 21)
However #1, removing the `else` keyword makes the code successfully compile:
```
}
if (!(l instanceof ArrayList al)) {
return;
}
```
However #2, adding an additional `else` keyword makes the code successfully compile:
```
} else if (!(l instanceof ArrayList al)) {
return;
} else {
al.ensureCapacity(10);
System.out.println("done");
}
```
While option #1 and #2 are acceptable workarounds, it looks to me like there could be a bug in javac? Therefore I am bringing this to your attention so you can decide if it is a bug or not.
I searched the JDK's bug tracker for this specific case, but could not find anything related.
Thanks!
Jean-Noël
--
<https://www.pingidentity.com/><https://www.pingidentity.com/>[Ping Identity]<https://www.pingidentity.com/>
Jean-Noel Rouvignac
Senior Principal Software Engineer
jean-noel.rouvignac at pingidentity.com<mailto:jean-noel.rouvignac at pingidentity.com>
<https://www.pingidentity.com/en/events/youniverse.html><https://www.pingidentity.com/en/events/youniverse.html><https://www.pingidentity.com/en/events/youniverse.html><https://www.pingidentity.com/en/gartner-magic-quadrant-access-management.html?utm_medium=email><https://www.pingidentity.com/en/events/virtualsummit.html?utm_source=sales&utm_medium=email&utm_campaign=VFE-GLOBAL-2025.03.18-19-PingVirtualEvent><https://www.pingidentity.com/en/events/virtualsummit.html?utm_source=sales&utm_medium=email&utm_campaign=VFE-GLOBAL-2025.03.18-19-PingVirtualEvent><https://www.pingidentity.com/en/events/virtualsummit.html?utm_source=sales&utm_medium=email&utm_campaign=VFE-GLOBAL-2025.03.18-19-PingVirtualEvent><https://www.pingidentity.com/en/events/virtualsummit.html?utm_source=sales&utm_medium=email&utm_campaign=VFE-GLOBAL-2025.03.18-19-PingVirtualEvent><https://www.pingidentity.com/en/events/virtualsummit.html?utm_source=sales&utm_medium=email&utm_campaign=VFE-GLOBAL-2025.03.18-19-PingVirtualEvent><https://www.pingidentity.com/en/events/virtualsummit.html?utm_source=sales&utm_medium=email&utm_campaign=VFE-GLOBAL-2025.03.18-19-PingVirtualEvent><https://www.pingidentity.com/en/events/virtualsummit.html?utm_source=sales&utm_medium=email&utm_campaign=VFE-GLOBAL-2025.03.18-19-PingVirtualEvent><https://www.pingidentity.com/en/events/virtualsummit.html?utm_source=sales&utm_medium=email&utm_campaign=VFE-GLOBAL-2025.03.18-19-PingVirtualEvent><https://www.pingidentity.com/en/events/virtualsummit.html?utm_source=sales&utm_medium=email&utm_campaign=VFE-GLOBAL-2025.03.18-19-PingVirtualEvent><https://www.pingidentity.com/en/events/virtualsummit.html?utm_source=sales&utm_medium=email&utm_campaign=VFE-GLOBAL-2025.03.18-19-PingVirtualEvent><https://www.pingidentity.com/en/events/virtualsummit.html?utm_source=sales&utm_medium=email&utm_campaign=VFE-GLOBAL-2025.03.18-19-PingVirtualEvent>[https://www.pingidentity.com/content/dam/picr/img/em/2025-YOUniverse-EmailSig.jpg]<https://www.pingidentity.com/en/events/youniverse.html>
Connect with us: [Glassdoor logo] <https://www.glassdoor.com/Overview/Working-at-Ping-Identity-EI_IE380907.11,24.htm> [LinkedIn logo] <https://www.linkedin.com/company/21870> [Twitter logo] <https://twitter.com/pingidentity> [YouTube logo] <https://www.youtube.com/user/PingIdentityTV> [Blog logo] <https://www.pingidentity.com/en/blog.html>
To view our privacy policy, click here<https://www.pingidentity.com/en/legal/privacy.html>
To stop receiving these emails, click here<https://4.pingidentity.com/PreferenceCenter.html>
CONFIDENTIALITY NOTICE: This email may contain confidential and privileged material for the sole use of the intended recipient(s). Any review, use, distribution or disclosure by others is strictly prohibited. If you have received this communication in error, please notify the sender immediately by e-mail and delete the message and any file attachments from your computer. Thank you.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20250608/02c524f7/attachment-0001.htm>
More information about the amber-dev
mailing list