8081271: NPE while compiling a program with erroneous use of constructor reference expressions

Missing enclosing instance error should not be swallowed when attributing constructor reference expressions.

Reviewed-by: mcimadamore, vromero
This commit is contained in:
Srikanth Adayapalam 2015-05-29 09:15:42 +05:30
parent 63dc40184a
commit 4815781a4a
7 changed files with 36 additions and 12 deletions

View File

@ -3185,10 +3185,8 @@ public class Resolve {
findDiamond(env, site, argtypes, typeargtypes, phase.isBoxingRequired(), phase.isVarargsRequired()) :
findMethod(env, site, name, argtypes, typeargtypes,
phase.isBoxingRequired(), phase.isVarargsRequired());
return (sym.kind != MTH ||
site.getEnclosingType().hasTag(NONE) ||
hasEnclosingInstance(env, site)) ?
sym : new BadConstructorReferenceError(sym);
return site.getEnclosingType().hasTag(CLASS) && !hasEnclosingInstance(env, site) ?
new BadConstructorReferenceError(sym) : sym;
}
@Override

View File

@ -1,6 +1,6 @@
/*
* @test /nodynamiccopyright/
* @bug 8003280 8075184
* @bug 8003280 8075184 8081271
* @summary Add lambda tests
* check that pair of bound/non-bound constructor references is flagged as ambiguous
* @author Maurizio Cimadamore
@ -49,8 +49,8 @@ class MethodReference23 {
static void call3(SAM22 s) { }
static void test11() {
SAM11 s = MethodReference23.Inner1::new; //ok
call11(MethodReference23.Inner1::new); //ok
SAM11 s = MethodReference23.Inner1::new; // fail.
call11(MethodReference23.Inner1::new); // fail.
}
static void test12() {

View File

@ -1,5 +1,5 @@
MethodReference23.java:52:19: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.cant.access.inner.cls.constr: Inner1, MethodReference23, MethodReference23)
MethodReference23.java:53:16: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.cant.access.inner.cls.constr: Inner1, MethodReference23, MethodReference23)
MethodReference23.java:53:16: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.cant.access.inner.cls.constr: Inner1, , MethodReference23)
MethodReference23.java:57:19: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.cant.access.inner.cls.constr: Inner1, , MethodReference23)
MethodReference23.java:58:16: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.cant.access.inner.cls.constr: Inner1, , MethodReference23)
MethodReference23.java:72:9: compiler.err.ref.ambiguous: call3, kindname.method, call3(MethodReference23.SAM21), MethodReference23, kindname.method, call3(MethodReference23.SAM22), MethodReference23

View File

@ -1,6 +1,6 @@
/*
* @test /nodynamiccopyright/
* @bug 8003280
* @bug 8003280 8081271
* @summary Add lambda tests
* spurious exceptions when checking references to inner constructors where
* the enclosing class is not defined in any outer context
@ -20,7 +20,7 @@ class MethodReference37 {
static class Outer {
class Inner { }
static void test1() {
void test1() {
SAM2<Inner, Outer> sam = Inner::new;
}

View File

@ -1,5 +1,5 @@
MethodReference37.java:24:38: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.constructor, (compiler.misc.cant.apply.symbol: kindname.constructor, Inner, compiler.misc.no.args, MethodReference37.Outer, kindname.class, MethodReference37.Outer.Inner, (compiler.misc.arg.length.mismatch)))
MethodReference37.java:29:39: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.constructor, (compiler.misc.cant.apply.symbol: kindname.constructor, Inner, compiler.misc.no.args, MethodReference37.Outer, kindname.class, MethodReference37.Outer.Inner, (compiler.misc.arg.length.mismatch)))
MethodReference37.java:34:40: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.constructor, (compiler.misc.cant.apply.symbol: kindname.constructor, Inner, compiler.misc.no.args, MethodReference37.Outer, kindname.class, MethodReference37.Outer.Inner, (compiler.misc.arg.length.mismatch)))
MethodReference37.java:38:41: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.constructor, (compiler.misc.cant.apply.symbol: kindname.constructor, Inner, compiler.misc.no.args, MethodReference37.Outer, kindname.class, MethodReference37.Outer.Inner, (compiler.misc.arg.length.mismatch)))
MethodReference37.java:34:40: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.cant.access.inner.cls.constr: Inner, MethodReference37.Outer, MethodReference37.Outer)
MethodReference37.java:38:41: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.cant.access.inner.cls.constr: Inner, MethodReference37.Outer, MethodReference37.Outer)
4 errors

View File

@ -0,0 +1,24 @@
/*
* @test /nodynamiccopyright/
* @bug 8081271
* @summary NPE while compiling a program with erroneous use of constructor reference expressions.
* @compile/fail/ref=MethodRefToInnerWithoutOuter.out -XDrawDiagnostics MethodRefToInnerWithoutOuter.java
*/
import java.util.List;
import java.util.ArrayList;
class MethodRefToInnerBase {
class TestString {
String str;
TestString(String strin) {
str = strin;
}
}
}
public class MethodRefToInnerWithoutOuter extends MethodRefToInnerBase {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.stream().forEach(TestString::new);
}
}

View File

@ -0,0 +1,2 @@
MethodRefToInnerWithoutOuter.java:22:31: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.cant.access.inner.cls.constr: TestString, , MethodRefToInnerBase)
1 error