Skip to content

Commit

Permalink
[WIP] fix error reporting for a pattern variable + multiple patterns
Browse files Browse the repository at this point in the history
Signed-off-by: David Thompson <[email protected]>
  • Loading branch information
datho7561 committed Dec 13, 2023
1 parent 6b8147e commit 6bc645b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,14 @@ private Expression getFirstValidExpression(BlockScope scope, SwitchStatement swi
if (e instanceof Pattern) {
scope.problemReporter().validateJavaFeatureSupport(JavaFeature.PATTERN_MATCHING_IN_SWITCH,
e.sourceStart, e.sourceEnd);
if (this.constantExpressions.length > 1) {
scope.problemReporter().illegalCaseLabelWithMultiplePatterns(this.constantExpressions[1], this.constantExpressions[this.constantExpressions.length - 1]);
return e;
if (this.constantExpressions.length > 1 || e instanceof GuardedPattern gp && gp.patterns.length > 1) {
PatternVariableCounter myVisitor = new PatternVariableCounter();
this.traverse(myVisitor, scope);
if (myVisitor.numVars > 0) {
scope.problemReporter().illegalCaseLabelWithMultiplePatterns(this);
}
}
return e;
} else if (e instanceof NullLiteral) {
scope.problemReporter().validateJavaFeatureSupport(JavaFeature.PATTERN_MATCHING_IN_SWITCH,
e.sourceStart, e.sourceEnd);
Expand Down Expand Up @@ -559,4 +563,24 @@ public LocalDeclaration getLocalDeclaration() {
return patternVariableIntroduced;
}

private class PatternVariableCounter extends ASTVisitor {

public int numVars = 0;

@Override
public boolean visit(TypePattern pattern, BlockScope scope) {
if (pattern.local != null && (pattern.local.name.length != 1 || pattern.local.name[0] != '_') && !"\\u005F".equals(pattern.local.name.toString())) { //$NON-NLS-1$
this.numVars++;
}
return true;
}

@Override
public boolean visit(RecordPattern pattern, BlockScope scope) {
if (pattern.local != null && (pattern.local.name.length != 1 || pattern.local.name[0] != '_') && !"\\u005F".equals(pattern.local.name.toString())) { //$NON-NLS-1$
this.numVars++;
}
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12342,13 +12342,13 @@ public void illegalFallthroughFromAPattern(Statement statement) {
statement.sourceStart,
statement.sourceEnd);
}
public void illegalCaseLabelWithMultiplePatterns(Expression secondPattern, Expression lastPattern) {
public void illegalCaseLabelWithMultiplePatterns(Statement statement) {
this.handle(
IProblem.CaseLabelWithPatternMustHaveExactlyOnePattern,
NoArgument,
NoArgument,
secondPattern.sourceStart,
lastPattern.sourceEnd);
statement.sourceStart,
statement.sourceEnd);
}
public void illegalCaseConstantCombination(Expression element) {
this.handle(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,7 @@
1916 = A case label guard cannot have a constant expression with value as 'false'
1940 = Cannot infer record pattern types for {0}
1941 = Syntax error, record patterns are not allowed here
1942 = Case labels with a pattern must have only one pattern
1942 = Case labels with a named pattern variable must have only one pattern


1990 = Access to {1}({2}) from the type {0} is emulated by a synthetic accessor method
Expand Down

0 comments on commit 6bc645b

Please sign in to comment.