8341070: javac fails with an exception when compiling import module under source level 8

Reviewed-by: asotona
This commit is contained in:
Jan Lahoda 2024-10-11 10:07:34 +00:00
parent 519544c1d7
commit 6133866150
2 changed files with 63 additions and 19 deletions

View File

@ -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, P> R accept(ElementVisitor<R, P> 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();

View File

@ -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<String> l;
}
""");
Files.createDirectories(classes);
List<String> 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<String> 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);
}
}
}