Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8255968: Confusing error message for inaccessible constructor
Reviewed-by: mcimadamore
  • Loading branch information
lgxbslgx authored and mcimadamore committed Dec 1, 2020
1 parent c5046ca commit 29d90b9
Show file tree
Hide file tree
Showing 31 changed files with 715 additions and 3 deletions.
Expand Up @@ -1583,19 +1583,55 @@ Symbol selectBest(Env<AttrContext> env,
currentResolutionContext.addApplicableCandidate(sym, mt);
} catch (InapplicableMethodException ex) {
currentResolutionContext.addInapplicableCandidate(sym, ex.getDiagnostic());
// Currently, an InapplicableMethodException occurs.
// If bestSoFar.kind was ABSENT_MTH, return an InapplicableSymbolError(kind is WRONG_MTH).
// If bestSoFar.kind was HIDDEN(AccessError)/WRONG_MTH/WRONG_MTHS, return an InapplicableSymbolsError(kind is WRONG_MTHS).
// See JDK-8255968 for more information.
switch (bestSoFar.kind) {
case ABSENT_MTH:
return new InapplicableSymbolError(currentResolutionContext);
case HIDDEN:
if (bestSoFar instanceof AccessError) {
// Add the JCDiagnostic of previous AccessError to the currentResolutionContext
// and construct InapplicableSymbolsError.
// Intentionally fallthrough.
currentResolutionContext.addInapplicableCandidate(((AccessError) bestSoFar).sym,
((AccessError) bestSoFar).getDiagnostic(JCDiagnostic.DiagnosticType.FRAGMENT, null, null, site, null, argtypes, typeargtypes));
} else {
return bestSoFar;
}
case WRONG_MTH:
bestSoFar = new InapplicableSymbolsError(currentResolutionContext);
default:
return bestSoFar;
}
}
if (!isAccessible(env, site, sym)) {
return (bestSoFar.kind == ABSENT_MTH)
? new AccessError(env, site, sym)
: bestSoFar;
AccessError curAccessError = new AccessError(env, site, sym);
JCDiagnostic curDiagnostic = curAccessError.getDiagnostic(JCDiagnostic.DiagnosticType.FRAGMENT, null, null, site, null, argtypes, typeargtypes);
// Currently, an AccessError occurs.
// If bestSoFar.kind was ABSENT_MTH, return an AccessError(kind is HIDDEN).
// If bestSoFar.kind was HIDDEN(AccessError), WRONG_MTH, WRONG_MTHS, return an InapplicableSymbolsError(kind is WRONG_MTHS).
// See JDK-8255968 for more information.
if (bestSoFar.kind == ABSENT_MTH) {
bestSoFar = curAccessError;
} else if (bestSoFar.kind == WRONG_MTH) {
// Add the JCDiagnostic of current AccessError to the currentResolutionContext
// and construct InapplicableSymbolsError.
currentResolutionContext.addInapplicableCandidate(sym, curDiagnostic);
bestSoFar = new InapplicableSymbolsError(currentResolutionContext);
} else if (bestSoFar.kind == WRONG_MTHS) {
// Add the JCDiagnostic of current AccessError to the currentResolutionContext
currentResolutionContext.addInapplicableCandidate(sym, curDiagnostic);
} else if (bestSoFar.kind == HIDDEN && bestSoFar instanceof AccessError) {
// Add the JCDiagnostics of previous and current AccessError to the currentResolutionContext
// and construct InapplicableSymbolsError.
currentResolutionContext.addInapplicableCandidate(((AccessError) bestSoFar).sym,
((AccessError) bestSoFar).getDiagnostic(JCDiagnostic.DiagnosticType.FRAGMENT, null, null, site, null, argtypes, typeargtypes));
currentResolutionContext.addInapplicableCandidate(sym, curDiagnostic);
bestSoFar = new InapplicableSymbolsError(currentResolutionContext);
}
return bestSoFar;
}
return (bestSoFar.kind.isResolutionError() && bestSoFar.kind != AMBIGUOUS)
? sym
Expand Down
37 changes: 37 additions & 0 deletions test/langtools/tools/javac/T8255968/T8255968_1.java
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8255968
* @summary Confusing error message for inaccessible constructor
* @run compile/fail/ref=T8255968_1.out -XDrawDiagnostics T8255968_1.java
*/

class T8255968_1 {
T8255968_1_Test c = new T8255968_1_Test(0);
}

class T8255968_1_Test {
private T8255968_1_Test(int x) {}
}
2 changes: 2 additions & 0 deletions test/langtools/tools/javac/T8255968/T8255968_1.out
@@ -0,0 +1,2 @@
T8255968_1.java:32:25: compiler.err.report.access: T8255968_1_Test(int), private, T8255968_1_Test
1 error
41 changes: 41 additions & 0 deletions test/langtools/tools/javac/T8255968/T8255968_10.java
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8255968
* @summary Confusing error message for inaccessible constructor
* @run compile/fail/ref=T8255968_10.out -XDrawDiagnostics T8255968_10.java
*/

class T8255968_10 {
T8255968_10_TestMethodReference c = T8255968_10_Test::new;
}

interface T8255968_10_TestMethodReference {
T8255968_10_Test create(int x);
}

class T8255968_10_Test {
private T8255968_10_Test(int x) {}
}
2 changes: 2 additions & 0 deletions test/langtools/tools/javac/T8255968/T8255968_10.out
@@ -0,0 +1,2 @@
T8255968_10.java:32:41: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.constructor, (compiler.misc.report.access: T8255968_10_Test(int), private, T8255968_10_Test))
1 error
41 changes: 41 additions & 0 deletions test/langtools/tools/javac/T8255968/T8255968_11.java
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8255968
* @summary Confusing error message for inaccessible constructor
* @run compile/fail/ref=T8255968_11.out -XDrawDiagnostics T8255968_11.java
*/

class T8255968_11 {
T8255968_11_TestMethodReference c = T8255968_11_Test::new;
}

interface T8255968_11_TestMethodReference {
T8255968_11_Test create(int x);
}

class T8255968_11_Test {
T8255968_11_Test(String x) {} // If this method is private, compiler will output the same error message.
}
2 changes: 2 additions & 0 deletions test/langtools/tools/javac/T8255968/T8255968_11.out
@@ -0,0 +1,2 @@
T8255968_11.java:32:41: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.constructor, (compiler.misc.cant.apply.symbol: kindname.constructor, T8255968_11_Test, java.lang.String, int, kindname.class, T8255968_11_Test, (compiler.misc.no.conforming.assignment.exists: (compiler.misc.inconvertible.types: int, java.lang.String))))
1 error
42 changes: 42 additions & 0 deletions test/langtools/tools/javac/T8255968/T8255968_12.java
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8255968
* @summary Confusing error message for inaccessible constructor
* @run compile/fail/ref=T8255968_12.out -XDrawDiagnostics T8255968_12.java
*/

class T8255968_12 {
T8255968_12_TestMethodReference c = T8255968_12_Test::new;
}

interface T8255968_12_TestMethodReference {
T8255968_12_Test create(int x);
}

class T8255968_12_Test {
private T8255968_12_Test(int x) {} // This method is not at the end.
T8255968_12_Test(String x) {} // If this method is private, compiler will output the same error message.
}
2 changes: 2 additions & 0 deletions test/langtools/tools/javac/T8255968/T8255968_12.out
@@ -0,0 +1,2 @@
T8255968_12.java:32:41: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.constructor, (compiler.misc.cant.apply.symbols: kindname.constructor, T8255968_12_Test, int,{(compiler.misc.inapplicable.method: kindname.constructor, T8255968_12_Test, T8255968_12_Test(int), (compiler.misc.report.access: T8255968_12_Test(int), private, T8255968_12_Test)),(compiler.misc.inapplicable.method: kindname.constructor, T8255968_12_Test, T8255968_12_Test(java.lang.String), (compiler.misc.no.conforming.assignment.exists: (compiler.misc.inconvertible.types: int, java.lang.String)))}))
1 error
42 changes: 42 additions & 0 deletions test/langtools/tools/javac/T8255968/T8255968_13.java
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8255968
* @summary Confusing error message for inaccessible constructor
* @run compile/fail/ref=T8255968_13.out -XDrawDiagnostics T8255968_13.java
*/

class T8255968_13 {
T8255968_13_TestMethodReference c = T8255968_13_Test::new;
}

interface T8255968_13_TestMethodReference {
T8255968_13_Test create(int x);
}

class T8255968_13_Test {
T8255968_13_Test(String x) {} // If this method is private, compiler will output the same error message.
private T8255968_13_Test(int x) {} // This method is at the end.
}
2 changes: 2 additions & 0 deletions test/langtools/tools/javac/T8255968/T8255968_13.out
@@ -0,0 +1,2 @@
T8255968_13.java:32:41: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.constructor, (compiler.misc.cant.apply.symbols: kindname.constructor, T8255968_13_Test, int,{(compiler.misc.inapplicable.method: kindname.constructor, T8255968_13_Test, T8255968_13_Test(int), (compiler.misc.report.access: T8255968_13_Test(int), private, T8255968_13_Test)),(compiler.misc.inapplicable.method: kindname.constructor, T8255968_13_Test, T8255968_13_Test(java.lang.String), (compiler.misc.no.conforming.assignment.exists: (compiler.misc.inconvertible.types: int, java.lang.String)))}))
1 error
43 changes: 43 additions & 0 deletions test/langtools/tools/javac/T8255968/T8255968_14.java
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8255968
* @summary Confusing error message for inaccessible constructor
* @run compile/fail/ref=T8255968_14.out -XDrawDiagnostics T8255968_14.java
*/

class T8255968_14 {
T8255968_14_TestMethodReference c = T8255968_14_Test::new;
}

interface T8255968_14_TestMethodReference {
T8255968_14_Test create(int x);
}

class T8255968_14_Test {
private T8255968_14_Test(int x) {} // This method is not at the end.
T8255968_14_Test(String x) {} // If this method is private, compiler will output the same error message.
private T8255968_14_Test(int[] x) {}
}
2 changes: 2 additions & 0 deletions test/langtools/tools/javac/T8255968/T8255968_14.out
@@ -0,0 +1,2 @@
T8255968_14.java:32:41: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.constructor, (compiler.misc.cant.apply.symbols: kindname.constructor, T8255968_14_Test, int,{(compiler.misc.inapplicable.method: kindname.constructor, T8255968_14_Test, T8255968_14_Test(int), (compiler.misc.report.access: T8255968_14_Test(int), private, T8255968_14_Test)),(compiler.misc.inapplicable.method: kindname.constructor, T8255968_14_Test, T8255968_14_Test(java.lang.String), (compiler.misc.no.conforming.assignment.exists: (compiler.misc.inconvertible.types: int, java.lang.String))),(compiler.misc.inapplicable.method: kindname.constructor, T8255968_14_Test, T8255968_14_Test(int[]), (compiler.misc.no.conforming.assignment.exists: (compiler.misc.inconvertible.types: int, int[])))}))
1 error
43 changes: 43 additions & 0 deletions test/langtools/tools/javac/T8255968/T8255968_15.java
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8255968
* @summary Confusing error message for inaccessible constructor
* @run compile/fail/ref=T8255968_15.out -XDrawDiagnostics T8255968_15.java
*/

class T8255968_15 {
T8255968_15_TestMethodReference c = T8255968_15_Test::new;
}

interface T8255968_15_TestMethodReference {
T8255968_15_Test create(int x);
}

class T8255968_15_Test {
T8255968_15_Test(String x) {} // If this method is private, compiler will output the same error message.
private T8255968_15_Test(int x) {} // This method is not at the end.
private T8255968_15_Test(int[] x) {}
}
2 changes: 2 additions & 0 deletions test/langtools/tools/javac/T8255968/T8255968_15.out
@@ -0,0 +1,2 @@
T8255968_15.java:32:41: compiler.err.prob.found.req: (compiler.misc.invalid.mref: kindname.constructor, (compiler.misc.cant.apply.symbols: kindname.constructor, T8255968_15_Test, int,{(compiler.misc.inapplicable.method: kindname.constructor, T8255968_15_Test, T8255968_15_Test(java.lang.String), (compiler.misc.no.conforming.assignment.exists: (compiler.misc.inconvertible.types: int, java.lang.String))),(compiler.misc.inapplicable.method: kindname.constructor, T8255968_15_Test, T8255968_15_Test(int), (compiler.misc.report.access: T8255968_15_Test(int), private, T8255968_15_Test)),(compiler.misc.inapplicable.method: kindname.constructor, T8255968_15_Test, T8255968_15_Test(int[]), (compiler.misc.no.conforming.assignment.exists: (compiler.misc.inconvertible.types: int, int[])))}))
1 error

1 comment on commit 29d90b9

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.