From 6133866150cf6131ab578f1537f84c239703fa67 Mon Sep 17 00:00:00 2001 From: Jan Lahoda Date: Fri, 11 Oct 2024 10:07:34 +0000 Subject: [PATCH] 8341070: javac fails with an exception when compiling import module under source level 8 Reviewed-by: asotona --- .../com/sun/tools/javac/code/Symtab.java | 36 +++++++-------- test/langtools/tools/javac/ImportModule.java | 46 +++++++++++++++++++ 2 files changed, 63 insertions(+), 19 deletions(-) diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symtab.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symtab.java index 4e62828c426..cee6a16a10f 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symtab.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symtab.java @@ -420,6 +420,23 @@ public class Symtab { missingInfoHandler, target.runtimeUseNestAccess()); + noModule = new ModuleSymbol(names.empty, null) { + @Override public boolean isNoModule() { + return true; + } + }; + addRootPackageFor(noModule); + + Source source = Source.instance(context); + if (Feature.MODULES.allowedInSource(source)) { + java_base = enterModule(names.java_base); + //avoid completing java.base during the Symtab initialization + java_base.completer = Completer.NULL_COMPLETER; + java_base.visiblePackages = Collections.emptyMap(); + } else { + java_base = noModule; + } + // create the basic builtin symbols unnamedModule = new ModuleSymbol(names.empty, null) { { @@ -427,7 +444,6 @@ public class Symtab { exports = List.nil(); provides = List.nil(); uses = List.nil(); - ModuleSymbol java_base = enterModule(names.java_base); com.sun.tools.javac.code.Directive.RequiresDirective d = new com.sun.tools.javac.code.Directive.RequiresDirective(java_base, EnumSet.of(com.sun.tools.javac.code.Directive.RequiresFlag.MANDATED)); @@ -447,7 +463,6 @@ public class Symtab { exports = List.nil(); provides = List.nil(); uses = List.nil(); - ModuleSymbol java_base = enterModule(names.java_base); com.sun.tools.javac.code.Directive.RequiresDirective d = new com.sun.tools.javac.code.Directive.RequiresDirective(java_base, EnumSet.of(com.sun.tools.javac.code.Directive.RequiresFlag.MANDATED)); @@ -456,13 +471,6 @@ public class Symtab { }; addRootPackageFor(errModule); - noModule = new ModuleSymbol(names.empty, null) { - @Override public boolean isNoModule() { - return true; - } - }; - addRootPackageFor(noModule); - noSymbol = new TypeSymbol(NIL, 0, names.empty, Type.noType, rootPackage) { @Override @DefinedBy(Api.LANGUAGE_MODEL) public R accept(ElementVisitor v, P p) { @@ -526,16 +534,6 @@ public class Symtab { // Enter symbol for the errSymbol scope.enter(errSymbol); - Source source = Source.instance(context); - if (Feature.MODULES.allowedInSource(source)) { - java_base = enterModule(names.java_base); - //avoid completing java.base during the Symtab initialization - java_base.completer = Completer.NULL_COMPLETER; - java_base.visiblePackages = Collections.emptyMap(); - } else { - java_base = noModule; - } - // Get the initial completer for ModuleSymbols from Modules moduleCompleter = Modules.instance(context).getCompleter(); diff --git a/test/langtools/tools/javac/ImportModule.java b/test/langtools/tools/javac/ImportModule.java index f38b77062e2..dc1ed9bd0c1 100644 --- a/test/langtools/tools/javac/ImportModule.java +++ b/test/langtools/tools/javac/ImportModule.java @@ -797,4 +797,50 @@ public class ImportModule extends TestRunner { } } + + @Test + public void testImportModuleNoModules(Path base) throws Exception { + Path current = base.resolve("."); + Path src = current.resolve("src"); + Path classes = current.resolve("classes"); + tb.writeJavaFiles(src, + """ + package test; + import module java.base; + public class Test { + List l; + } + """); + + Files.createDirectories(classes); + + List actualErrors = new JavacTask(tb) + .options("--release", "8", + "-XDshould-stop.at=FLOW", + "-XDdev", + "-XDrawDiagnostics") + .outdir(classes) + .files(tb.findJavaFiles(src)) + .run(Task.Expect.FAIL) + .writeAll() + .getOutputLines(Task.OutputKind.DIRECT); + + List expectedErrors = List.of( + "- compiler.warn.option.obsolete.source: 8", + "- compiler.warn.option.obsolete.target: 8", + "- compiler.warn.option.obsolete.suppression", + "Test.java:2:8: compiler.err.preview.feature.disabled.plural: (compiler.misc.feature.module.imports)", + "Test.java:2:1: compiler.err.import.module.not.found: java.base", + "Test.java:4:5: compiler.err.cant.resolve.location: kindname.class, List, , , (compiler.misc.location: kindname.class, test.Test, null)", + "3 errors", + "3 warnings" + ); + + if (!Objects.equals(expectedErrors, actualErrors)) { + throw new AssertionError("Incorrect Output, expected: " + expectedErrors + + ", actual: " + out); + + } + } + }