diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java index 0554cee3149..d8cb63e3c06 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java @@ -4061,7 +4061,7 @@ public class Resolve { location = site.tsym; } if (!location.name.isEmpty()) { - if (location.kind == PCK && !site.tsym.exists()) { + if (location.kind == PCK && !site.tsym.exists() && location.name != names.java) { return diags.create(dkind, log.currentSource(), pos, "doesnt.exist", location); } diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Names.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Names.java index 26e0b7577aa..27ba6f46eaa 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Names.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Names.java @@ -117,6 +117,7 @@ public class Names { public final Name Method; // package names + public final Name java; public final Name java_lang; // module names @@ -291,6 +292,7 @@ public class Names { Method = fromString("Method"); // package names + java = fromString("java"); java_lang = fromString("java.lang"); // module names diff --git a/test/langtools/tools/javac/8264258/MisleadingNonExistentPathError.java b/test/langtools/tools/javac/8264258/MisleadingNonExistentPathError.java new file mode 100644 index 00000000000..a3fd5c6a41e --- /dev/null +++ b/test/langtools/tools/javac/8264258/MisleadingNonExistentPathError.java @@ -0,0 +1,27 @@ +/* + * @test /nodynamiccopyright/ + * @bug 8264258 + * @summary Unknown lookups in the java package give misleading compilation errors + * @compile/fail/ref=MisleadingNonExistentPathError.out -XDrawDiagnostics MisleadingNonExistentPathError.java + */ +package knownpkg; + +public class MisleadingNonExistentPathError { + + void classNotFound() { + // Not found, but in an existing package + Class c1 = knownpkg.NotFound.class; + + // Not found, but in a (system) package which exists and is in scope + Class c2 = java.lang.NotFound.class; + + // Not found, on a genuinely unknown package + Class c3 = unknownpkg.NotFound.class; + + // Not found, but in the 'java' package which is in scope as per JLS 6.3 and observable as per JLS 7.4.3 + Class c4 = java.NotFound.class; + + // Not found, but in a (system) package which exists and is in scope + Object c5 = new java.lang(); + } +} diff --git a/test/langtools/tools/javac/8264258/MisleadingNonExistentPathError.out b/test/langtools/tools/javac/8264258/MisleadingNonExistentPathError.out new file mode 100644 index 00000000000..645aa456bf9 --- /dev/null +++ b/test/langtools/tools/javac/8264258/MisleadingNonExistentPathError.out @@ -0,0 +1,6 @@ +MisleadingNonExistentPathError.java:13:31: compiler.err.cant.resolve.location: kindname.class, NotFound, , , (compiler.misc.location: kindname.package, knownpkg, null) +MisleadingNonExistentPathError.java:16:32: compiler.err.cant.resolve.location: kindname.class, NotFound, , , (compiler.misc.location: kindname.package, java.lang, null) +MisleadingNonExistentPathError.java:19:33: compiler.err.doesnt.exist: unknownpkg +MisleadingNonExistentPathError.java:22:27: compiler.err.cant.resolve.location: kindname.class, NotFound, , , (compiler.misc.location: kindname.package, java, null) +MisleadingNonExistentPathError.java:25:29: compiler.err.cant.resolve.location: kindname.class, lang, , , (compiler.misc.location: kindname.package, java, null) +5 errors diff --git a/test/langtools/tools/javac/TryWithResources/TwrForVariable3.out b/test/langtools/tools/javac/TryWithResources/TwrForVariable3.out index df6e2bfff6f..26e539910c2 100644 --- a/test/langtools/tools/javac/TryWithResources/TwrForVariable3.out +++ b/test/langtools/tools/javac/TryWithResources/TwrForVariable3.out @@ -1,5 +1,5 @@ TwrForVariable3.java:15:14: compiler.err.prob.found.req: (compiler.misc.try.not.applicable.to.type: (compiler.misc.inconvertible.types: java.lang.Object, java.lang.AutoCloseable)) TwrForVariable3.java:18:14: compiler.err.prob.found.req: (compiler.misc.try.not.applicable.to.type: (compiler.misc.inconvertible.types: java.lang.Object, java.lang.AutoCloseable)) -TwrForVariable3.java:21:18: compiler.err.doesnt.exist: java +TwrForVariable3.java:21:18: compiler.err.cant.resolve.location: kindname.class, lang, , , (compiler.misc.location: kindname.package, java, null) TwrForVariable3.java:24:14: compiler.err.cant.resolve.location: kindname.variable, java, , , (compiler.misc.location: kindname.class, TwrForVariable3, null) 4 errors