Skip to content

Commit

Permalink
Preventing a NPE when --source 8 + -XDshould-stop.at=FLOW is used in …
Browse files Browse the repository at this point in the history
…combination with import module java.base;
  • Loading branch information
lahodaj committed Sep 25, 2024
1 parent 0f253d1 commit d360fea
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 19 deletions.
36 changes: 17 additions & 19 deletions src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symtab.java
Original file line number Diff line number Diff line change
Expand Up @@ -420,14 +420,30 @@ protected Symtab(Context context) throws CompletionFailure {
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) {
{
directives = List.nil();
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));
Expand All @@ -447,7 +463,6 @@ public String toString() {
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));
Expand All @@ -456,13 +471,6 @@ public String toString() {
};
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) {
Expand Down Expand Up @@ -526,16 +534,6 @@ public <R, P> R accept(ElementVisitor<R, P> v, P p) {
// 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();

Expand Down
50 changes: 50 additions & 0 deletions test/langtools/tools/javac/ImportModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -797,4 +797,54 @@ public class C {}

}
}

@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 {
public static void main(String... args) {
List<String> l = new ArrayList<>();
System.out.println(l.getClass().getName());
}
}
""");

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:5:9: compiler.err.cant.resolve.location: kindname.class, List, , , (compiler.misc.location: kindname.class, test.Test, null)",
"Test.java:5:30: compiler.err.cant.resolve.location: kindname.class, ArrayList, , , (compiler.misc.location: kindname.class, test.Test, null)",
"4 errors",
"3 warnings"
);

if (!Objects.equals(expectedErrors, actualErrors)) {
throw new AssertionError("Incorrect Output, expected: " + expectedErrors +
", actual: " + out);

}
}

}

0 comments on commit d360fea

Please sign in to comment.