Skip to content

Commit

Permalink
Fix crash when assigning to bindings
Browse files Browse the repository at this point in the history
Disallow pattern no matching on all control paths

Co-authored-by: Jan Lahoda <[email protected]>o
  • Loading branch information
biboudis committed Nov 3, 2024
1 parent b8cbc6f commit b08e0df
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 5 deletions.
14 changes: 11 additions & 3 deletions src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java
Original file line number Diff line number Diff line change
Expand Up @@ -1042,8 +1042,16 @@ public void visitMethodDef(JCMethodDecl tree) {
}

// Attribute all bindings.
for (List<JCVariableDecl> l = tree.bindings; l != null && l.nonEmpty(); l = l.tail) {
attribStat(l.head, localEnv);
// the bindings have no meaning in the body of the deconstructor,
// so enter them in a temporary scope:
Env<AttrContext> bindingEnv =
env.dup(tree, localEnv.info.dup(localEnv.info.scope.dup()));
try {
for (List<JCVariableDecl> l = tree.bindings; l != null && l.nonEmpty(); l = l.tail) {
attribStat(l.head, bindingEnv);
}
} finally {
bindingEnv.info.scope.leave();
}

chk.checkVarargsMethodDecl(localEnv, tree);
Expand Down Expand Up @@ -2434,7 +2442,7 @@ public void visitMatch(JCMatch tree) {
args = args.tail;
}

if (tree.clazz != tree.meth.name) {
if (tree.clazz != null && tree.clazz != tree.meth.name) {
log.error(tree.pos(), Errors.MatchPatternNameWrong);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -617,8 +617,11 @@ public void visitMethodDef(JCMethodDecl tree) {
scanStat(tree.body);
tree.completesNormally = alive != Liveness.DEAD;

if (alive == Liveness.ALIVE && !tree.sym.type.getReturnType().hasTag(VOID))
if (alive == Liveness.ALIVE && (tree.sym.flags() & PATTERN) != 0) {
log.error(TreeInfo.diagEndPos(tree.body), Errors.MissingMatchStmt);
} else if (alive == Liveness.ALIVE && !tree.sym.type.getReturnType().hasTag(VOID)) {
log.error(TreeInfo.diagEndPos(tree.body), Errors.MissingRetStmt);
}

clearPendingExits(true);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,9 @@ compiler.err.missing.meth.body.or.decl.abstract=\
compiler.err.missing.ret.stmt=\
missing return statement

compiler.err.missing.match.stmt=\
missing match statement

# 0: type
compiler.misc.missing.ret.val=\
missing return value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public pattern Service(String name, int id) {
&& keys2.get("name") instanceof JSONString(var name2)
&& keys2.get("id") instanceof JSONNumber(int id2)) {
match Service(name2, id2);
} else {
match Service("error", -1);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @test
* @enablePreview
* @compile/fail/ref=MatcherFinishingNormally.out -XDrawDiagnostics MatcherFinishingNormally.java
*/
public class MatcherFinishingNormally {
public pattern MatcherFinishingNormally(String content) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MatcherFinishingNormally.java:7:62: compiler.err.missing.match.stmt
- compiler.note.preview.filename: MatcherFinishingNormally.java, DEFAULT
- compiler.note.preview.recompile
1 error
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,37 @@ public class R2 {
pattern R2(int i) { match R2(42); }
pattern R2(int i) { match R2(42); }
}


public class Point {
final int x;
final int y;

protected Point(int x, int y) {
this.x = x;
this.y = y;
}

protected pattern Point(int x, int y) {
match Point(this.x, this.y);
}
}

public class GreatPoint extends Point {
final int magnitude;

public GreatPoint(int x, int y, int magnitude) {
super(x, y);
if (magnitude < 0) throw new IllegalArgumentException();
this.magnitude = magnitude;
}

public pattern GreatPoint(int x, int y, int magnitude) {
if (this instanceof Point(var X, var Y)) {
x = X;
y = Y;
}
match GreatPoint(x, y, this.magnitude);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ PatternDeclarationErrors.java:49:13: compiler.err.pattern.declaration.no.throws
PatternDeclarationErrors.java:52:24: compiler.err.pattern.declaration.cant.throw.exception
PatternDeclarationErrors.java:53:13: compiler.err.pattern.declaration.no.throws
PatternDeclarationErrors.java:58:24: compiler.err.invalid.canonical.deconstructor.in.record: R
PatternDeclarationErrors.java:97:17: compiler.err.cant.assign.val.to.var: final, x
PatternDeclarationErrors.java:98:17: compiler.err.cant.assign.val.to.var: final, y
PatternDeclarationErrors.java:29:13: compiler.err.unreachable.stmt
- compiler.note.preview.filename: PatternDeclarationErrors.java, DEFAULT
- compiler.note.preview.recompile
14 errors
16 errors

0 comments on commit b08e0df

Please sign in to comment.