提交 29ab714c 编写于 作者: D darcy

7013420: Project Coin: remove general expression support from try-with-resources statement

Reviewed-by: mcimadamore, jjg
上级 e871a245
...@@ -142,7 +142,7 @@ public class JavacParser implements Parser { ...@@ -142,7 +142,7 @@ public class JavacParser implements Parser {
*/ */
boolean allowAnnotations; boolean allowAnnotations;
/** Switch: should we recognize automatic resource management? /** Switch: should we recognize try-with-resources?
*/ */
boolean allowTWR; boolean allowTWR;
...@@ -2184,29 +2184,23 @@ public class JavacParser implements Parser { ...@@ -2184,29 +2184,23 @@ public class JavacParser implements Parser {
while (S.token() == SEMI) { while (S.token() == SEMI) {
// All but last of multiple declarators subsume a semicolon // All but last of multiple declarators subsume a semicolon
storeEnd(defs.elems.last(), S.endPos()); storeEnd(defs.elems.last(), S.endPos());
int semiColonPos = S.pos();
S.nextToken(); S.nextToken();
if (S.token() == RPAREN) { // Illegal trailing semicolon
// after last resource
error(semiColonPos, "try.resource.trailing.semi");
break;
}
defs.append(resource()); defs.append(resource());
} }
return defs.toList(); return defs.toList();
} }
/** Resource = /** Resource = VariableModifiersOpt Type VariableDeclaratorId = Expression
* VariableModifiers Type VariableDeclaratorId = Expression
* | Expression
*/ */
JCTree resource() { JCTree resource() {
int pos = S.pos(); return variableDeclaratorRest(S.pos(), optFinal(Flags.FINAL),
if (S.token() == FINAL || S.token() == MONKEYS_AT) { parseType(), ident(), true, null);
return variableDeclaratorRest(pos, optFinal(0), parseType(),
ident(), true, null);
} else {
JCExpression t = term(EXPR | TYPE);
if ((lastmode & TYPE) != 0 && S.token() == IDENTIFIER)
return variableDeclaratorRest(pos, toP(F.at(pos).Modifiers(Flags.FINAL)), t,
ident(), true, null);
else
return t;
}
} }
/** CompilationUnit = [ { "@" Annotation } PACKAGE Qualident ";"] {ImportDeclaration} {TypeDeclaration} /** CompilationUnit = [ { "@" Annotation } PACKAGE Qualident ";"] {ImportDeclaration} {TypeDeclaration}
......
...@@ -177,6 +177,8 @@ compiler.err.final.parameter.may.not.be.assigned=\ ...@@ -177,6 +177,8 @@ compiler.err.final.parameter.may.not.be.assigned=\
final parameter {0} may not be assigned final parameter {0} may not be assigned
compiler.err.try.resource.may.not.be.assigned=\ compiler.err.try.resource.may.not.be.assigned=\
auto-closeable resource {0} may not be assigned auto-closeable resource {0} may not be assigned
compiler.err.try.resource.trailing.semi=\
illegal trailing semicolon in resources declaration
compiler.err.multicatch.parameter.may.not.be.assigned=\ compiler.err.multicatch.parameter.may.not.be.assigned=\
multi-catch parameter {0} may not be assigned multi-catch parameter {0} may not be assigned
compiler.err.finally.without.try=\ compiler.err.finally.without.try=\
......
BadTwrSyntax.java:14:43: compiler.err.illegal.start.of.expr BadTwrSyntax.java:14:42: compiler.err.try.resource.trailing.semi
1 error 1 error
/* /*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2010, 2011 Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -23,9 +23,9 @@ ...@@ -23,9 +23,9 @@
/* /*
* @test * @test
* @bug 6911256 6964740 6965277 * @bug 6911256 6964740 6965277 7013420
* @author Maurizio Cimadamore * @author Maurizio Cimadamore
* @summary Check that lowered arm block does not end up creating resource twice * @summary Check that lowered try-with-resources block does not end up creating resource twice
*/ */
import java.util.ArrayList; import java.util.ArrayList;
...@@ -45,7 +45,7 @@ public class DuplicateResource { ...@@ -45,7 +45,7 @@ public class DuplicateResource {
static ArrayList<TestResource> resources = new ArrayList<TestResource>(); static ArrayList<TestResource> resources = new ArrayList<TestResource>();
public static void main(String[] args) { public static void main(String[] args) {
try(new TestResource()) { try(TestResource tr = new TestResource()) {
//do something //do something
} catch (Exception e) { } catch (Exception e) {
throw new AssertionError("Shouldn't reach here", e); throw new AssertionError("Shouldn't reach here", e);
...@@ -59,7 +59,7 @@ public class DuplicateResource { ...@@ -59,7 +59,7 @@ public class DuplicateResource {
} }
TestResource resource = resources.get(0); TestResource resource = resources.get(0);
if (!resource.isClosed) { if (!resource.isClosed) {
throw new AssertionError("Resource used in ARM block has not been automatically closed"); throw new AssertionError("Resource used in try-with-resources block has not been automatically closed");
} }
} }
} }
/* /*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -23,25 +23,34 @@ ...@@ -23,25 +23,34 @@
/* /*
* @test * @test
* @bug 6911256 6964740 6965277 * @bug 7013420
* @author Maurizio Cimadamore * @author Joseph D. Darcy
* @summary Resource of an intersection type crashes Flow * @summary Test that resource variables are accepted as explicitly final.
* @compile TwrIntersection.java
*/ */
interface MyCloseable extends AutoCloseable { import java.io.IOException;
void close() throws java.io.IOException;
}
class ResourceTypeVar {
public void test() { public class ExplicitFinal implements AutoCloseable {
try(getX()) { public static void main(String... args) {
//do something try(final ExplicitFinal r2 = new ExplicitFinal()) {
} catch (java.io.IOException e) { // Not reachable r2.toString();
throw new AssertionError("Shouldn't reach here", e); } catch (IOException ioe) {
throw new AssertionError("Shouldn't reach here", ioe);
} }
try(final @SuppressWarnings("unchecked") ExplicitFinal r3 = new ExplicitFinal()) {
r3.toString();
} catch (IOException ioe) {
throw new AssertionError("Shouldn't reach here", ioe);
} }
<X extends Number & MyCloseable> X getX() { return null; } try(@SuppressWarnings("unchecked") ExplicitFinal r4 = new ExplicitFinal()) {
r4.toString();
} catch (IOException ioe) {
throw new AssertionError("Shouldn't reach here", ioe);
}
}
public void close() throws IOException {
System.out.println("Calling close on " + this);
}
} }
/* /*
* @test /nodynamiccopyright/ * @test /nodynamiccopyright/
* @bug 6911256 6964740 6965277 * @bug 6911256 6964740 6965277 7013420
* @author Maurizio Cimadamore * @author Maurizio Cimadamore
* @summary Test that resource variables are implicitly final * @summary Test that resource variables are implicitly final
* @compile/fail/ref=ImplicitFinal.out -XDrawDiagnostics ImplicitFinal.java * @compile/fail/ref=ImplicitFinal.out -XDrawDiagnostics ImplicitFinal.java
...@@ -15,12 +15,25 @@ class ImplicitFinal implements AutoCloseable { ...@@ -15,12 +15,25 @@ class ImplicitFinal implements AutoCloseable {
} catch (IOException ioe) { // Not reachable } catch (IOException ioe) { // Not reachable
throw new AssertionError("Shouldn't reach here", ioe); throw new AssertionError("Shouldn't reach here", ioe);
} }
}
try(@SuppressWarnings("unchecked") ImplicitFinal r1 = new ImplicitFinal()) {
r1 = null; //disallowed
} catch (IOException ioe) { // Not reachable
throw new AssertionError("Shouldn't reach here", ioe);
}
// A close method, but the class is <em>not</em> Closeable or try(final ImplicitFinal r2 = new ImplicitFinal()) {
// AutoCloseable. r2 = null; //disallowed
} catch (IOException ioe) { // Not reachable
throw new AssertionError("Shouldn't reach here", ioe);
}
try(final @SuppressWarnings("unchecked") ImplicitFinal r3 = new ImplicitFinal()) {
r3 = null; //disallowed
} catch (IOException ioe) { // Not reachable
throw new AssertionError("Shouldn't reach here", ioe);
}
}
public void close() throws IOException { public void close() throws IOException {
throw new IOException(); throw new IOException();
} }
......
ImplicitFinal.java:14:13: compiler.err.try.resource.may.not.be.assigned: r ImplicitFinal.java:14:13: compiler.err.try.resource.may.not.be.assigned: r
1 error ImplicitFinal.java:20:13: compiler.err.try.resource.may.not.be.assigned: r1
ImplicitFinal.java:26:13: compiler.err.try.resource.may.not.be.assigned: r2
ImplicitFinal.java:32:13: compiler.err.try.resource.may.not.be.assigned: r3
4 errors
/* /*
* @test /nodynamiccopyright/ * @test /nodynamiccopyright/
* @bug 6911256 6964740 * @bug 6911256 6964740 7013420
* @author Joseph D. Darcy * @author Joseph D. Darcy
* @summary Test exception analysis of ARM blocks * @summary Test exception analysis of try-with-resources blocks
* @compile/fail/ref=TwrFlow.out -XDrawDiagnostics TwrFlow.java * @compile/fail/ref=TwrFlow.out -XDrawDiagnostics TwrFlow.java
*/ */
import java.io.IOException; import java.io.IOException;
public class TwrFlow implements AutoCloseable { public class TwrFlow implements AutoCloseable {
public static void main(String... args) { public static void main(String... args) {
try(TwrFlow armflow = new TwrFlow()) { try(TwrFlow twrFlow = new TwrFlow()) {
System.out.println(armflow.toString()); System.out.println(twrFlow.toString());
} catch (IOException ioe) { // Not reachable
throw new AssertionError("Shouldn't reach here", ioe);
}
// CustomCloseException should be caught or added to throws clause
// Also check behavior on a resource expression rather than a
// declaration.
TwrFlow armflowexpr = new TwrFlow();
try(armflowexpr) {
System.out.println(armflowexpr.toString());
} catch (IOException ioe) { // Not reachable } catch (IOException ioe) { // Not reachable
throw new AssertionError("Shouldn't reach here", ioe); throw new AssertionError("Shouldn't reach here", ioe);
} }
......
TwrFlow.java:14:11: compiler.err.except.never.thrown.in.try: java.io.IOException TwrFlow.java:14:11: compiler.err.except.never.thrown.in.try: java.io.IOException
TwrFlow.java:24:11: compiler.err.except.never.thrown.in.try: java.io.IOException
TwrFlow.java:12:46: compiler.err.unreported.exception.need.to.catch.or.throw: CustomCloseException TwrFlow.java:12:46: compiler.err.unreported.exception.need.to.catch.or.throw: CustomCloseException
TwrFlow.java:22:26: compiler.err.unreported.exception.need.to.catch.or.throw: CustomCloseException 2 errors
4 errors
/*
* @test /nodynamiccopyright/
* @bug 6911256 6964740 6965277
* @author Maurizio Cimadamore
* @summary Check that resources of an intersection type forces union of exception types
* to be caught outside twr block
* @compile/fail/ref=TwrIntersection02.out -XDrawDiagnostics TwrIntersection02.java
*/
class TwrIntersection02 {
static class Exception1 extends Exception {}
static class Exception2 extends Exception {}
interface MyResource1 extends AutoCloseable {
void close() throws Exception1;
}
interface MyResource2 extends AutoCloseable {
void close() throws Exception2;
}
public void test1() throws Exception1 {
try(getX()) {
//do something
}
}
public void test2() throws Exception2 {
try(getX()) {
//do something
}
}
<X extends MyResource1 & MyResource2> X getX() { return null; }
}
TwrIntersection02.java:25:21: compiler.err.unreported.exception.need.to.catch.or.throw: TwrIntersection02.Exception2
TwrIntersection02.java:31:21: compiler.err.unreported.exception.need.to.catch.or.throw: TwrIntersection02.Exception1
2 errors
/* /*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
/* /*
* @test * @test
* @bug 6911256 6964740 * @bug 6911256 6964740 7013420
* @author Joseph D. Darcy * @author Joseph D. Darcy
* @summary Test that TWR and multi-catch play well together * @summary Test that TWR and multi-catch play well together
* @compile TwrMultiCatch.java * @compile TwrMultiCatch.java
...@@ -48,9 +48,9 @@ public class TwrMultiCatch implements AutoCloseable { ...@@ -48,9 +48,9 @@ public class TwrMultiCatch implements AutoCloseable {
private static void test(TwrMultiCatch twrMultiCatch, private static void test(TwrMultiCatch twrMultiCatch,
Class<? extends Exception> expected) { Class<? extends Exception> expected) {
try(twrMultiCatch) { try(TwrMultiCatch tmc = twrMultiCatch) {
System.out.println(twrMultiCatch.toString()); System.out.println(tmc.toString());
} catch (final CustomCloseException1 | } catch (CustomCloseException1 |
CustomCloseException2 exception) { CustomCloseException2 exception) {
if (!exception.getClass().equals(expected) ) { if (!exception.getClass().equals(expected) ) {
throw new RuntimeException("Unexpected catch!"); throw new RuntimeException("Unexpected catch!");
...@@ -68,7 +68,7 @@ public class TwrMultiCatch implements AutoCloseable { ...@@ -68,7 +68,7 @@ public class TwrMultiCatch implements AutoCloseable {
try { try {
throw t; throw t;
} catch (final CustomCloseException1 | } catch (CustomCloseException1 |
CustomCloseException2 exception) { CustomCloseException2 exception) {
throw exception; throw exception;
} catch (Throwable throwable) { } catch (Throwable throwable) {
......
/* /*
* @test /nodynamiccopyright/ * @test /nodynamiccopyright/
* @bug 6911256 6964740 * @bug 6911256 6964740 7013420
* @author Joseph D. Darcy * @author Joseph D. Darcy
* @summary Verify invalid TWR block is not accepted. * @summary Verify invalid TWR block is not accepted.
* @compile/fail -source 6 TwrOnNonResource.java * @compile/fail -source 6 TwrOnNonResource.java
...@@ -18,18 +18,6 @@ class TwrOnNonResource { ...@@ -18,18 +18,6 @@ class TwrOnNonResource {
try(TwrOnNonResource aonr = new TwrOnNonResource()) { try(TwrOnNonResource aonr = new TwrOnNonResource()) {
System.out.println(aonr.toString()); System.out.println(aonr.toString());
} catch (Exception e) {;} } catch (Exception e) {;}
// Also check expression form
TwrOnNonResource aonr = new TwrOnNonResource();
try(aonr) {
System.out.println(aonr.toString());
}
try(aonr) {
System.out.println(aonr.toString());
} finally {;}
try(aonr) {
System.out.println(aonr.toString());
} catch (Exception e) {;}
} }
/* /*
......
TwrOnNonResource.java:12:13: compiler.err.prob.found.req: (compiler.misc.try.not.applicable.to.type), TwrOnNonResource, java.lang.AutoCloseable TwrOnNonResource.java:12:13: compiler.err.prob.found.req: (compiler.misc.try.not.applicable.to.type), TwrOnNonResource, java.lang.AutoCloseable
TwrOnNonResource.java:15:13: compiler.err.prob.found.req: (compiler.misc.try.not.applicable.to.type), TwrOnNonResource, java.lang.AutoCloseable TwrOnNonResource.java:15:13: compiler.err.prob.found.req: (compiler.misc.try.not.applicable.to.type), TwrOnNonResource, java.lang.AutoCloseable
TwrOnNonResource.java:18:13: compiler.err.prob.found.req: (compiler.misc.try.not.applicable.to.type), TwrOnNonResource, java.lang.AutoCloseable TwrOnNonResource.java:18:13: compiler.err.prob.found.req: (compiler.misc.try.not.applicable.to.type), TwrOnNonResource, java.lang.AutoCloseable
TwrOnNonResource.java:24:13: compiler.err.prob.found.req: (compiler.misc.try.not.applicable.to.type), TwrOnNonResource, java.lang.AutoCloseable 3 errors
TwrOnNonResource.java:27:13: compiler.err.prob.found.req: (compiler.misc.try.not.applicable.to.type), TwrOnNonResource, java.lang.AutoCloseable
TwrOnNonResource.java:30:13: compiler.err.prob.found.req: (compiler.misc.try.not.applicable.to.type), TwrOnNonResource, java.lang.AutoCloseable
6 errors
/* /*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -21,23 +21,15 @@ ...@@ -21,23 +21,15 @@
* questions. * questions.
*/ */
/* // key: compiler.err.try.resource.trailing.semi
* @test
* @bug 6911256 6964740 6965277
* @author Maurizio Cimadamore
* @summary Verify that method type-inference works as expected in TWR context
* @compile TwrInference.java
*/
class TwrInference {
public void test() { class TryResoureTrailingSemi implements AutoCloseable {
try(getX()) { public static void main(String... args) {
//do something try(TryResoureTrailingSemi r = new TryResoureTrailingSemi();) {
} catch (Exception e) { // Not reachable System.out.println(r.toString());
throw new AssertionError("Shouldn't reach here", e);
} }
} }
<X> X getX() { return null; } @Override
public void close() {return;}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册