Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8303374: Implement JEP 455: Primitive Types in Patterns, instanceof, and switch (Preview) #15638

Closed
wants to merge 78 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
d354f7e
8303374: Compiler Implementation for Primitive types in patterns, ins…
biboudis Aug 30, 2023
1ae06ac
Apply suggestions from code review
biboudis Sep 28, 2023
6ebae0d
Implement type pairs to exactnessMethod name
biboudis Oct 2, 2023
ad6071b
Merge branch 'master' into primitive-patterns
biboudis Oct 2, 2023
d356b36
Fix exhausts and equals in SwitchBootstraps
biboudis Oct 3, 2023
f036fdb
Remove unnecessary disambiguation in TransPatterns and simplify Lower…
biboudis Oct 4, 2023
1d88baa
Rename exactness methods
biboudis Oct 4, 2023
ef98c04
Simplify TypePairs
biboudis Oct 4, 2023
ceee1e4
Declutter Lower.visitTypeTest
biboudis Oct 5, 2023
c8b860d
Use sun.invoke.util.Wrapper in SwitchBootstraps
biboudis Oct 5, 2023
c041d20
Fix `checkUnconditionallyExact` and adjust the type of numeric consta…
biboudis Oct 5, 2023
ff07aef
Merge branch 'master' into primitive-patterns
biboudis Oct 5, 2023
01ebecd
Fix Lower.visitTypeTest
biboudis Oct 6, 2023
f1a5e14
Add comment for TypePairs
biboudis Oct 6, 2023
cf146c1
Attempting to improve reported errors.
lahodaj Oct 6, 2023
8450cb2
Fixing error conditions, tweaking tests.
lahodaj Oct 9, 2023
69485e2
Improve non integral check
biboudis Oct 9, 2023
f0da1c1
Add tests
biboudis Oct 9, 2023
cdaa186
Fix exhaustiveness check for primitive types
biboudis Oct 9, 2023
bab2633
Adjust tests
biboudis Oct 9, 2023
3030dc1
Merge branch 'master' into primitive-patterns
biboudis Oct 10, 2023
27c2aab
Update name of exactness methods class
biboudis Oct 10, 2023
3c0d85e
Merge branch 'master' into primitive-patterns
biboudis Oct 11, 2023
d06b217
Fix duplicate method name and add a new test in PrimitivePatternsSwit…
biboudis Oct 11, 2023
ded2bac
Address review
biboudis Oct 16, 2023
2f03b1b
Merge branch 'master' into primitive-patterns
biboudis Oct 16, 2023
ac0b678
Adjust javadoc on ExactConversionsSupport
biboudis Oct 16, 2023
b923714
Improve javadoc of ExactConversionsSupport
biboudis Oct 17, 2023
4acb66e
Merge branch 'master' into primitive-patterns
biboudis Oct 17, 2023
248971b
Fix SwitchBootstraps for pattern matching over longs
biboudis Oct 18, 2023
fa6256b
Fix Bootstrap initialization error by using `explicitCastArguments` f…
biboudis Oct 18, 2023
b391a48
Add tests for new data types in switch statements
biboudis Oct 29, 2023
b437373
Merge branch 'master' into primitive-patterns
biboudis Oct 30, 2023
e5b6d32
Attempting to speeding SwitchBootstraps.
lahodaj Nov 1, 2023
30bf4f9
Cleanup.
lahodaj Nov 1, 2023
a7f778c
Add test for instanceof as a pattern on with record patterns
biboudis Nov 1, 2023
263bd3d
Avoiding generating dead code - review feedback.
lahodaj Nov 3, 2023
fd3bc68
Reflecting review feedback.
lahodaj Nov 3, 2023
94d4748
Some more get->orElseThrow
lahodaj Nov 3, 2023
ff390e9
Reflecting review feedback.
lahodaj Nov 3, 2023
92f8517
reflecting review comment: fixing letter case.
lahodaj Nov 3, 2023
cc8a6a8
Merge branch 'master' into JDK-8319220
lahodaj Nov 7, 2023
e81f023
Small cleanup in PrimitivePatternsSwitch
biboudis Nov 7, 2023
4cf3c06
Merge branch 'JDK-8319220' into primitive-patterns
lahodaj Nov 15, 2023
0a07af9
Merge branch 'master' into primitive-patterns
biboudis Nov 16, 2023
ca7cc2a
Merge branch 'master' into primitive-patterns
biboudis Nov 17, 2023
584a4a3
Merge branch 'master' into primitive-patterns
biboudis Nov 20, 2023
2b2405c
Merge branch 'master' into primitive-patterns
biboudis Nov 21, 2023
fe2c5ef
Fixed switch in the cases of unboxing and widening
biboudis Dec 11, 2023
fac9ca9
Merge branch 'primitive-patterns-and-generating-dispatch' into primit…
biboudis Dec 11, 2023
4fbdd70
Remove trailing spaces
biboudis Dec 11, 2023
6545789
Merge branch 'master' into primitive-patterns
lahodaj Dec 11, 2023
7a71ffa
Cleanup
biboudis Dec 11, 2023
a03fea7
Merge branch 'master' into primitive-patterns
biboudis Dec 14, 2023
e255d25
Merge branch 'master' into primitive-patterns
biboudis Jan 20, 2024
676af9d
Update copyright year, javadoc, JDK version
biboudis Jan 22, 2024
14a85ac
Revert "Update copyright year, javadoc, JDK version"
biboudis Jan 22, 2024
ed61d5f
Update ExactConversionsSupport Javadoc and JDK version
biboudis Jan 22, 2024
5f7f579
Merge branch 'master' into primitive-patterns
biboudis Jan 22, 2024
50cb983
Improve Javadoc of ExactConversionsSupport
biboudis Jan 22, 2024
663c7a5
Set previewEnabled properly in SwitchBootstraps
biboudis Jan 24, 2024
9b7fb7f
Check the range of the new types of switch through TypeTag.isInSuperC…
biboudis Jan 24, 2024
5e48ea2
Enhance Javadoc of ExactConversionsSupport
biboudis Jan 24, 2024
aa9b2fc
Enhance Javadoc of ExactConversionsSupport (2)
biboudis Jan 24, 2024
0fcdca8
Remove redundant test from checkUnconditionallyExact
biboudis Jan 24, 2024
ea31a80
Cleanup test parameters
biboudis Jan 24, 2024
4e5ac77
Introduce ExactnessConversionsSupportTest
biboudis Jan 24, 2024
4ed60e9
Simplify checkUnconditionallyExact
biboudis Jan 24, 2024
80cfc06
Cleanup redundant clone
biboudis Jan 24, 2024
2af70ba
Update year
biboudis Jan 24, 2024
13bfc43
Improve readability in SwitchBootstraps
biboudis Jan 25, 2024
b77d231
Tidy up Javadoc of IllegalArgumentException in typeSwitch
biboudis Jan 25, 2024
4463468
Tidy up Javadoc of Lower.visitTypeTest
biboudis Jan 25, 2024
854c20a
Small fix in Javadoc
biboudis Jan 25, 2024
e466cfb
Remove redundant null check and introduce a const boolean for uncond…
biboudis Jan 26, 2024
8c27c5c
Address review by Jan
biboudis Jan 29, 2024
7ac1013
Update summary in ExactnessConversionsSupportTest
biboudis Jan 31, 2024
f68748b
Merge branch 'master' into primitive-patterns
biboudis Jan 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -225,7 +225,7 @@ private static MethodHandle createRepeatIndexSwitch(MethodHandles.Lookup lookup,
if (unconditionalExactnessCheck(selectorType, currentLabelClass)) {
currentTest = trueDef;
} else if (currentLabelClass.isPrimitive()) {
if (selectorType.isInstance(Object.class)) {
if (selectorType.equals(Object.class)) {
currentTest = INSTANCEOF_CHECK;
if (currentLabelClass.isAssignableFrom(byte.class)) { testLabel = Byte.class; }
else if (currentLabelClass.isAssignableFrom(short.class)) { testLabel = Short.class; }
@@ -234,15 +234,6 @@ private static MethodHandle createRepeatIndexSwitch(MethodHandles.Lookup lookup,
else if (currentLabelClass.isAssignableFrom(double.class)) { testLabel = Double.class; }
else if (currentLabelClass.isAssignableFrom(float.class)) { testLabel = Float.class; }
else { testLabel = Long.class; }
} else if (!selectorType.isPrimitive()) {
currentTest = INSTANCEOF_CHECK;
if (currentLabelClass.equals(byte.class)) { testLabel = Byte.class; }
else if (currentLabelClass.equals(short.class)) { testLabel = Short.class; }
else if (currentLabelClass.equals(char.class)) { testLabel = Character.class; }
else if (currentLabelClass.equals(int.class)) { testLabel = Integer.class; }
else if (currentLabelClass.equals(double.class)) { testLabel = Double.class; }
else if (currentLabelClass.equals(float.class)) { testLabel = Float.class; }
else { testLabel = Long.class; }
} else {
MethodHandle exactnessCheck;
try {
Original file line number Diff line number Diff line change
@@ -4151,7 +4151,7 @@ public boolean compatible(Type found, Type req, Warner warn) {

public void visitTypeTest(JCInstanceOf tree) {
Type exprtype = attribExpr(tree.expr, env);
if(!allowPrimitivePatterns) {
if (!allowPrimitivePatterns) {
exprtype = chk.checkNullOrRefType(
tree.expr.pos(), exprtype);
} else if (tree.pattern.type != null && exprtype.isPrimitive() && preview.isPreview(Feature.PRIMITIVE_PATTERNS)) {
55 changes: 28 additions & 27 deletions src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java
Original file line number Diff line number Diff line change
@@ -732,27 +732,17 @@ public void visitSwitchExpression(JCSwitchExpression tree) {
}
}
}
if (tree.selector.type.hasTag(TypeTag.BOOLEAN)) {
HashSet<JCTree> labelValues = tree.cases.stream()
.filter(TreeInfo::unguardedCase)
.flatMap(c -> c.labels.stream())
.filter(l -> !l.hasTag(DEFAULTCASELABEL))
.map(l -> l.hasTag(CONSTANTCASELABEL) ? ((JCConstantCaseLabel) l).expr
: ((JCPatternCaseLabel) l).pat)
.collect(Collectors.toCollection(HashSet::new));

boolean hasBothTrueAndFalse = labelValues.stream().filter(l -> l.hasTag(Tag.LITERAL)).map(l -> ((JCLiteral)l).value).distinct().count() == 2;
boolean exhaustive = exhausts(tree.selector, tree.cases);

tree.isExhaustive = hasBothTrueAndFalse || tree.hasUnconditionalPattern;

if (hasBothTrueAndFalse && tree.hasUnconditionalPattern) {
log.error(tree, Errors.DefaultLabelNotAllowed);
}
} else {
tree.isExhaustive = tree.hasUnconditionalPattern ||
TreeInfo.isErrorEnumSwitch(tree.selector, tree.cases) ||
exhausts(tree.selector, tree.cases);
if (tree.selector.type.hasTag(TypeTag.BOOLEAN) && exhaustive && tree.hasUnconditionalPattern) {
log.error(tree, Errors.DefaultLabelNotAllowed);
}

tree.isExhaustive = tree.hasUnconditionalPattern ||
TreeInfo.isErrorEnumSwitch(tree.selector, tree.cases) ||
exhaustive;

if (!tree.isExhaustive) {
log.error(tree, Errors.NotExhaustive);
}
@@ -763,6 +753,7 @@ public void visitSwitchExpression(JCSwitchExpression tree) {
private boolean exhausts(JCExpression selector, List<JCCase> cases) {
Set<PatternDescription> patternSet = new HashSet<>();
Map<Symbol, Set<Symbol>> enum2Constants = new HashMap<>();
Set<Object> booleanLiterals = new HashSet<>();
for (JCCase c : cases) {
if (!TreeInfo.unguardedCase(c))
continue;
@@ -773,19 +764,29 @@ private boolean exhausts(JCExpression selector, List<JCCase> cases) {
patternSet.add(makePatternDescription(component, patternLabel.pat));
}
} else if (l instanceof JCConstantCaseLabel constantLabel) {
Symbol s = TreeInfo.symbol(constantLabel.expr);
if (s != null && s.isEnum()) {
enum2Constants.computeIfAbsent(s.owner, x -> {
Set<Symbol> result = new HashSet<>();
s.owner.members()
.getSymbols(sym -> sym.kind == Kind.VAR && sym.isEnum())
.forEach(result::add);
return result;
}).remove(s);
if (selector.type.hasTag(TypeTag.BOOLEAN)) {
Object value = ((JCLiteral) constantLabel.expr).value;
booleanLiterals.add(value);
} else {
Symbol s = TreeInfo.symbol(constantLabel.expr);
if (s != null && s.isEnum()) {
enum2Constants.computeIfAbsent(s.owner, x -> {
Set<Symbol> result = new HashSet<>();
s.owner.members()
.getSymbols(sym -> sym.kind == Kind.VAR && sym.isEnum())
.forEach(result::add);
return result;
}).remove(s);
}
}
}
}
}

if (selector.type.hasTag(TypeTag.BOOLEAN) && booleanLiterals.size() == 2) {
return true;
}

for (Entry<Symbol, Set<Symbol>> e : enum2Constants.entrySet()) {
if (e.getValue().isEmpty()) {
patternSet.add(new BindingPattern(e.getKey().type));