Skip to content

Commit 1a5a424

Browse files
committed
Cleanup, reflecting review comments.
1 parent c88e3f2 commit 1a5a424

File tree

20 files changed

+307
-277
lines changed

20 files changed

+307
-277
lines changed

src/java.base/share/classes/java/lang/runtime/SwitchBootstraps.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,14 @@ private static<T extends CallSite> MethodHandle typeInitHook(T receiver) {
9898
* the {@code NameAndType} of the {@code InvokeDynamic}
9999
* structure and is stacked automatically by the VM.
100100
* @param labels non-null case labels - {@code String} and {@code Integer} constants
101-
* and {@code Class} instances
102-
* @return the index into {@code labels} of the target value, if the target
103-
* is an instance of any of the types, {@literal -1} if the target
101+
* and {@code Class} instances, in any combination
102+
* @return a {@code CallSite}, which accepts two parameters: one is an instance
103+
* of the target type, and second is a restart index. It returns the
104+
* index into {@code labels} of the target value, if the target
105+
* is an instance of any of the types or equal to any of the constants, {@literal -1} if the target
104106
* value is {@code null}, or {@code types.length} if the target value
105-
* is not an instance of any of the types
107+
* is not an instance of any of the types or equal to any of the constants. Will return
108+
* an index that is greater or equal to the restart index provided.
106109
* @throws NullPointerException if any required argument is null
107110
* @throws IllegalArgumentException if any labels are null, or if the
108111
* invocation type is not {@code (T)int for some reference type {@code T}}

src/jdk.compiler/share/classes/com/sun/source/tree/TreeVisitor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ public interface TreeVisitor<R,P> {
277277
*/
278278
@PreviewFeature(feature=PreviewFeature.Feature.SWITCH_PATTERN_MATCHING, reflective=true)
279279
R visitDefaultCaseLabel(DefaultCaseLabelTree node, P p);
280-
280+
281281
/**
282282
* Visits a MethodTree node.
283283
* @param node the node being visited
@@ -303,7 +303,7 @@ public interface TreeVisitor<R,P> {
303303
R visitNewArray(NewArrayTree node, P p);
304304

305305
/**
306-
* Visits an GuardPatternTree node.
306+
* Visits a GuardPatternTree node.
307307
* @param node the node being visited
308308
* @param p a parameter value
309309
* @return a result value
@@ -313,7 +313,7 @@ public interface TreeVisitor<R,P> {
313313
R visitGuardedPattern(GuardedPatternTree node, P p);
314314

315315
/**
316-
* Visits an AndPatternTree node.
316+
* Visits a ParenthesizedPatternTree node.
317317
* @param node the node being visited
318318
* @param p a parameter value
319319
* @return a result value

src/jdk.compiler/share/classes/com/sun/tools/javac/code/Preview.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import static com.sun.tools.javac.code.Flags.SEALED;
5151
import static com.sun.tools.javac.code.Flags.NON_SEALED;
5252
import static com.sun.tools.javac.main.Option.PREVIEW;
53+
import com.sun.tools.javac.util.JCDiagnostic;
5354

5455
/**
5556
* Helper class to handle preview language features. This class maps certain language features
@@ -79,6 +80,7 @@ public class Preview {
7980

8081
private final Lint lint;
8182
private final Log log;
83+
private final Source source;
8284

8385
private static final Context.Key<Preview> previewKey = new Context.Key<>();
8486

@@ -96,7 +98,7 @@ public static Preview instance(Context context) {
9698
enabled = options.isSet(PREVIEW);
9799
log = Log.instance(context);
98100
lint = Lint.instance(context);
99-
Source source = Source.instance(context);
101+
source = Source.instance(context);
100102
this.previewHandler =
101103
new MandatoryWarningHandler(log, source, lint.isEnabled(LintCategory.PREVIEW), true, "preview", LintCategory.PREVIEW);
102104
forcePreview = options.isSet("forcePreview");
@@ -242,4 +244,19 @@ public void clear() {
242244
previewHandler.clear();
243245
}
244246

247+
public void checkSourceLevel(DiagnosticPosition pos, Feature feature) {
248+
if (isPreview(feature) && !isEnabled()) {
249+
//preview feature without --preview flag, error
250+
log.error(JCDiagnostic.DiagnosticFlag.SOURCE_LEVEL, pos, disabledError(feature));
251+
} else {
252+
if (!feature.allowedInSource(source)) {
253+
log.error(JCDiagnostic.DiagnosticFlag.SOURCE_LEVEL, pos,
254+
feature.error(source.name));
255+
}
256+
if (isEnabled() && isPreview(feature)) {
257+
warnPreview(pos, feature);
258+
}
259+
}
260+
}
261+
245262
}

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java

Lines changed: 7 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,8 @@ protected Attr(Context context) {
169169
allowDefaultMethods = Feature.DEFAULT_METHODS.allowedInSource(source);
170170
allowStaticInterfaceMethods = Feature.STATIC_INTERFACE_METHODS.allowedInSource(source);
171171
allowReifiableTypesInInstanceof =
172-
Feature.REIFIABLE_TYPES_INSTANCEOF.allowedInSource(source) &&
173-
(!preview.isPreview(Feature.REIFIABLE_TYPES_INSTANCEOF) || preview.isEnabled());
172+
Feature.REIFIABLE_TYPES_INSTANCEOF.allowedInSource(source);
174173
allowRecords = Feature.RECORDS.allowedInSource(source);
175-
allowCaseNull =
176-
Feature.CASE_NULL.allowedInSource(source) &&
177-
(!preview.isPreview(Feature.CASE_NULL) || preview.isEnabled());
178-
allowPatternSwitch =
179-
Feature.PATTERN_SWITCH.allowedInSource(source) &&
180-
(!preview.isPreview(Feature.PATTERN_SWITCH) || preview.isEnabled());
181174
sourceName = source.name;
182175
useBeforeDeclarationWarning = options.isSet("useBeforeDeclarationWarning");
183176

@@ -218,14 +211,6 @@ protected Attr(Context context) {
218211
*/
219212
private final boolean allowRecords;
220213

221-
/** Switch: case null allowed?
222-
*/
223-
boolean allowCaseNull;
224-
225-
/** Switch: pattern switch allowed?
226-
*/
227-
boolean allowPatternSwitch;
228-
229214
/**
230215
* Switch: warn about use of variable before declaration?
231216
* RFE: 6425594
@@ -1675,19 +1660,7 @@ private void handleSwitch(JCTree switchTree,
16751660
boolean stringSwitch = types.isSameType(seltype, syms.stringType);
16761661
boolean errorEnumSwitch = TreeInfo.isErrorEnumSwitch(selector, cases);
16771662
if (!enumSwitch && !stringSwitch && !types.isAssignable(seltype, syms.intType)) {
1678-
if (preview.isPreview(Feature.PATTERN_SWITCH) && !preview.isEnabled()) {
1679-
//preview feature without --preview flag, error
1680-
log.error(DiagnosticFlag.SOURCE_LEVEL, selector.pos(), preview.disabledError(Feature.PATTERN_SWITCH));
1681-
} else {
1682-
if (!allowPatternSwitch) {
1683-
log.error(DiagnosticFlag.SOURCE_LEVEL, selector.pos(),
1684-
Feature.PATTERN_SWITCH.error(this.sourceName));
1685-
allowPatternSwitch = true;
1686-
}
1687-
if (preview.isEnabled() && preview.isPreview(Feature.PATTERN_SWITCH)) {
1688-
preview.warnPreview(selector.pos(), Feature.PATTERN_SWITCH);
1689-
}
1690-
}
1663+
preview.checkSourceLevel(selector.pos(), Feature.PATTERN_SWITCH);
16911664
}
16921665

16931666
// Attribute all cases and
@@ -1715,19 +1688,7 @@ private void handleSwitch(JCTree switchTree,
17151688
if (pat.isExpression()) {
17161689
JCExpression expr = (JCExpression) pat;
17171690
if (TreeInfo.isNull(expr)) {
1718-
if (preview.isPreview(Feature.CASE_NULL) && !preview.isEnabled()) {
1719-
//preview feature without --preview flag, error
1720-
log.error(DiagnosticFlag.SOURCE_LEVEL, expr.pos(), preview.disabledError(Feature.CASE_NULL));
1721-
} else {
1722-
if (!allowCaseNull) {
1723-
log.error(DiagnosticFlag.SOURCE_LEVEL, expr.pos(),
1724-
Feature.CASE_NULL.error(this.sourceName));
1725-
allowCaseNull = true;
1726-
}
1727-
if (preview.isEnabled() && preview.isPreview(Feature.CASE_NULL)) {
1728-
preview.warnPreview(expr.pos(), Feature.CASE_NULL);
1729-
}
1730-
}
1691+
preview.checkSourceLevel(expr.pos(), Feature.CASE_NULL);
17311692
if (hasNullPattern) {
17321693
log.error(c.pos(), Errors.DuplicateCaseLabel);
17331694
}
@@ -1781,9 +1742,9 @@ private void handleSwitch(JCTree switchTree,
17811742
}
17821743
//binding pattern
17831744
attribExpr(pat, switchEnv, seltype);
1784-
var primary = primaryType((JCPattern) pat);
1785-
Type patternType = types.erasure(primary.fst);
1786-
boolean isTotal = primary.snd &&
1745+
var primary = TreeInfo.primaryPatternType((JCPattern) pat);
1746+
Type patternType = types.erasure(primary.type());
1747+
boolean isTotal = primary.unconditional() &&
17871748
types.isSubtype(types.erasure(seltype), patternType);
17881749
if (isTotal) {
17891750
if (hasTotalPattern) {
@@ -1798,7 +1759,7 @@ private void handleSwitch(JCTree switchTree,
17981759
log.error(pat.pos(), Errors.PatternDominated);
17991760
}
18001761
}
1801-
if (primary.snd) {
1762+
if (primary.unconditional()) {
18021763
coveredTypes = coveredTypes.prepend(patternType);
18031764
}
18041765
}
@@ -1858,26 +1819,6 @@ private Symbol enumConstant(JCTree tree, Type enumType) {
18581819
}
18591820
return null;
18601821
}
1861-
private Pair<Type, Boolean> primaryType(JCPattern pat) {
1862-
return switch (pat.getTag()) {
1863-
case BINDINGPATTERN -> Pair.of(((JCBindingPattern) pat).type, true);
1864-
case GUARDPATTERN -> {
1865-
JCGuardPattern guarded = (JCGuardPattern) pat;
1866-
Pair<Type, Boolean> nested = primaryType(guarded.patt);
1867-
boolean full = false;
1868-
//TODO: duplicated in Flow:
1869-
if (guarded.expr.type.hasTag(BOOLEAN)) {
1870-
var constValue = guarded.expr.type.constValue();
1871-
if (constValue != null && ((int) constValue) == 1) {
1872-
full = true;
1873-
}
1874-
}
1875-
yield Pair.of(nested.fst, full);
1876-
}
1877-
case PARENTHESIZEDPATTERN -> primaryType(((JCParenthesizedPattern) pat).pattern);
1878-
default -> throw new AssertionError();
1879-
};
1880-
}
18811822

18821823
public void visitSynchronized(JCSynchronized tree) {
18831824
chk.checkRefType(tree.pos(), attribExpr(tree.lock, env));
@@ -4121,9 +4062,6 @@ public void visitTypeTest(JCInstanceOf tree) {
41214062
if (!clazztype.isErroneous() && !types.isReifiable(clazztype)) {
41224063
boolean valid = false;
41234064
if (allowReifiableTypesInInstanceof) {
4124-
if (preview.isPreview(Feature.REIFIABLE_TYPES_INSTANCEOF)) {
4125-
preview.warnPreview(tree.expr.pos(), Feature.REIFIABLE_TYPES_INSTANCEOF);
4126-
}
41274065
Warner warner = new Warner();
41284066
if (!types.isCastable(exprtype, clazztype, warner)) {
41294067
chk.basicHandler.report(tree.expr.pos(),

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.sun.tools.javac.resources.CompilerProperties.Errors;
4040
import com.sun.tools.javac.resources.CompilerProperties.Warnings;
4141
import com.sun.tools.javac.tree.*;
42+
import com.sun.tools.javac.tree.TreeInfo.PatternPrimaryType;
4243
import com.sun.tools.javac.util.*;
4344
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
4445
import com.sun.tools.javac.util.JCDiagnostic.Error;
@@ -53,8 +54,10 @@
5354
import static com.sun.tools.javac.code.Kinds.Kind.*;
5455
import static com.sun.tools.javac.code.TypeTag.BOOLEAN;
5556
import static com.sun.tools.javac.code.TypeTag.VOID;
57+
import com.sun.tools.javac.resources.CompilerProperties.Fragments;
5658
import com.sun.tools.javac.tree.JCTree.JCParenthesizedPattern;
5759
import static com.sun.tools.javac.tree.JCTree.Tag.*;
60+
import com.sun.tools.javac.util.JCDiagnostic.Fragment;
5861

5962
/** This pass implements dataflow analysis for Java programs though
6063
* different AST visitor steps. Liveness analysis (see AliveAnalyzer) checks that
@@ -706,7 +709,6 @@ public void visitSwitchExpression(JCSwitchExpression tree) {
706709
constants = new HashSet<>();
707710
constants.addAll(((ClassSymbol) selectorSym).permitted);
708711
}
709-
boolean hasDefault = false;
710712
boolean coversInput = false;
711713
Liveness prevAlive = alive;
712714
for (List<JCCase> l = tree.cases; l.nonEmpty(); l = l.tail) {
@@ -720,19 +722,21 @@ public void visitSwitchExpression(JCSwitchExpression tree) {
720722
if (expr.hasTag(IDENT))
721723
constants.remove(((JCIdent) expr).name);
722724
} else if (pat.isPattern()) {
723-
constants.remove(patternType((JCTree.JCPattern) pat));
725+
PatternPrimaryType patternType = TreeInfo.primaryPatternType((JCPattern) pat);
726+
727+
if (patternType.unconditional()) {
728+
constants.remove(patternType.type().tsym);
729+
}
724730
}
725731
}
726-
if (!pat.isExpression() && !pat.hasTag(DEFAULTCASELABEL)) {
727-
TypeSymbol patternType = patternType((JCTree.JCPattern) pat);
728-
if (patternType != null && types.isSubtype(types.erasure(tree.selector.type),
729-
types.erasure(patternType.type))) {
732+
if (pat.isPattern()) {
733+
PatternPrimaryType patternType = TreeInfo.primaryPatternType((JCPattern) pat);
734+
if (patternType.unconditional() &&
735+
types.isSubtype(types.erasure(tree.selector.type),
736+
types.erasure(patternType.type()))) {
730737
coversInput = true;
731738
}
732739
}
733-
if (pat.hasTag(Tag.DEFAULTCASELABEL)) {
734-
hasDefault = true;
735-
}
736740
}
737741
scanStats(c.stats);
738742
if (alive == Liveness.ALIVE) {
@@ -746,31 +750,14 @@ public void visitSwitchExpression(JCSwitchExpression tree) {
746750
}
747751
c.completesNormally = alive != Liveness.DEAD;
748752
}
749-
if ((constants == null || !constants.isEmpty()) && !hasDefault && !coversInput &&
750-
!TreeInfo.isErrorEnumSwitch(tree.selector, tree.cases)) {
753+
if ((constants == null || !constants.isEmpty()) && !tree.hasTotalPattern &&
754+
!coversInput && !TreeInfo.isErrorEnumSwitch(tree.selector, tree.cases)) {
751755
log.error(tree, Errors.NotExhaustive);
752756
}
753757
alive = prevAlive;
754758
alive = alive.or(resolveYields(tree, prevPendingExits));
755759
}
756760

757-
private TypeSymbol patternType(JCPattern p) {
758-
return switch (p.getTag()) {
759-
case BINDINGPATTERN -> ((JCBindingPattern) p).var.vartype.type.tsym;
760-
case PARENTHESIZEDPATTERN -> patternType(((JCParenthesizedPattern) p).pattern);
761-
case GUARDPATTERN -> {
762-
JCGuardPattern g = (JCGuardPattern) p;
763-
if (g.expr.type.hasTag(BOOLEAN)) {
764-
var constValue = g.expr.type.constValue();
765-
if (constValue != null && ((int) (Integer) constValue) == 1) {
766-
yield patternType(g.patt);
767-
}
768-
}
769-
yield null;
770-
}
771-
default -> throw new AssertionError("Unexpected pattern type: " + p.getTag());
772-
};
773-
}
774761
public void visitTry(JCTry tree) {
775762
ListBuffer<PendingExit> prevPendingExits = pendingExits;
776763
pendingExits = new ListBuffer<>();
@@ -2913,10 +2900,10 @@ void letInit(JCTree tree) {
29132900
}
29142901

29152902
void reportEffectivelyFinalError(DiagnosticPosition pos, Symbol sym) {
2916-
String subKey = switch (currentTree.getTag()) {
2917-
case LAMBDA -> "lambda";
2918-
case GUARDPATTERN -> "guard";
2919-
case CLASSDEF -> "inner.cls";
2903+
Fragment subKey = switch (currentTree.getTag()) {
2904+
case LAMBDA -> Fragments.Lambda;
2905+
case GUARDPATTERN -> Fragments.Guard;
2906+
case CLASSDEF -> Fragments.InnerCls;
29202907
default -> throw new AssertionError("Unexpected tree kind: " + currentTree.getTag());
29212908
};
29222909
log.error(pos, Errors.CantRefNonEffectivelyFinalVar(sym, diags.fragment(subKey)));

0 commit comments

Comments
 (0)