8347646: module-info classfile missing the preview flag

Reviewed-by: asotona
This commit is contained in:
Jan Lahoda 2025-01-14 10:23:32 +00:00
parent 3e989fd0f7
commit bb93f67ea8
4 changed files with 90 additions and 18 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 2025, 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
@ -103,7 +103,6 @@ import com.sun.tools.javac.tree.JCTree.Tag;
import com.sun.tools.javac.tree.TreeInfo;
import com.sun.tools.javac.util.Assert;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;
@ -151,7 +150,6 @@ public class Modules extends JCTree.Visitor {
private final Target target;
private final boolean allowModules;
private final boolean allowAccessIntoSystem;
private final boolean allowRequiresTransitiveJavaBase;
public final boolean multiModuleMode;
@ -207,11 +205,6 @@ public class Modules extends JCTree.Visitor {
allowAccessIntoSystem = options.isUnset(Option.RELEASE);
Preview preview = Preview.instance(context);
allowRequiresTransitiveJavaBase =
Feature.JAVA_BASE_TRANSITIVE.allowedInSource(source) &&
(!preview.isPreview(Feature.JAVA_BASE_TRANSITIVE) || preview.isEnabled());
lintOptions = options.isUnset(Option.XLINT_CUSTOM, "-" + LintCategory.OPTIONS.option);
multiModuleMode = fileManager.hasLocation(StandardLocation.MODULE_SOURCE_PATH);
@ -822,12 +815,10 @@ public class Modules extends JCTree.Visitor {
Set<RequiresFlag> flags = EnumSet.noneOf(RequiresFlag.class);
if (tree.isTransitive) {
if (msym == syms.java_base &&
!allowRequiresTransitiveJavaBase &&
!preview.participatesInPreview(syms, sym)) {
if (source.compareTo(Source.JDK10) >= 0) {
log.error(DiagnosticFlag.SOURCE_LEVEL,
tree.pos(),
Feature.JAVA_BASE_TRANSITIVE.error(source.name));
preview.checkSourceLevel(tree.pos(),
Feature.JAVA_BASE_TRANSITIVE);
}
}
flags.add(RequiresFlag.TRANSITIVE);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024, 2025, 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
@ -23,7 +23,7 @@
/**
* @test
* @bug 8328481 8332236 8332890 8344647
* @bug 8328481 8332236 8332890 8344647 8347646
* @summary Check behavior of module imports.
* @library /tools/lib
* @modules java.logging
@ -39,6 +39,7 @@ import com.sun.source.tree.Tree;
import com.sun.source.util.TaskEvent;
import com.sun.source.util.TaskEvent.Kind;
import com.sun.source.util.TaskListener;
import java.lang.classfile.ClassFile;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@ -966,4 +967,84 @@ public class ImportModule extends TestRunner {
.run(Task.Expect.SUCCESS)
.writeAll();
}
@Test //JDK-8347646
public void testRequiresTransitiveJavaBase(Path base) throws Exception {
Path current = base.resolve(".");
Path src = current.resolve("src");
Path classes = current.resolve("classes");
Path ma = src.resolve("ma");
Path maClasses = classes.resolve("ma");
tb.writeJavaFiles(ma,
"""
module ma {
requires transitive java.base;
}
""");
Path test = src.resolve("test");
tb.writeJavaFiles(test,
"""
module test {
requires ma;
}
""",
"""
package test;
import module ma;
public class Test {
public static void main(String... args) {
System.out.println(List.of("Hello"));
}
}
""");
Files.createDirectories(maClasses);
List<String> actualErrors = new JavacTask(tb)
.options("-XDrawDiagnostics")
.outdir(maClasses)
.files(tb.findJavaFiles(ma))
.run(Task.Expect.FAIL)
.writeAll()
.getOutputLines(Task.OutputKind.DIRECT);
List<String> expectedErrors = List.of(
"module-info.java:2:4: compiler.err.preview.feature.disabled.plural: (compiler.misc.feature.java.base.transitive)",
"1 error"
);
if (!Objects.equals(expectedErrors, actualErrors)) {
throw new AssertionError("Incorrect Output, expected: " + expectedErrors +
", actual: " + actualErrors);
}
new JavacTask(tb)
.options("-XDrawDiagnostics",
"--source", "9")
.outdir(maClasses)
.files(tb.findJavaFiles(ma))
.run()
.writeAll();
Path maModuleInfo = maClasses.resolve("module-info.class");
if (ClassFile.of().parse(maModuleInfo).minorVersion() == ClassFile.PREVIEW_MINOR_VERSION) {
throw new AssertionError("wrong minor version");
}
new JavacTask(tb)
.options("-XDrawDiagnostics",
"--enable-preview", "--release", SOURCE_VERSION)
.outdir(maClasses)
.files(tb.findJavaFiles(ma))
.run()
.writeAll();
Path maModuleInfo2 = maClasses.resolve("module-info.class");
if (ClassFile.of().parse(maModuleInfo2).minorVersion() != ClassFile.PREVIEW_MINOR_VERSION) {
throw new AssertionError("wrong minor version");
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2025, 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
@ -21,7 +21,7 @@
* questions.
*/
// key: compiler.err.feature.not.supported.in.source.plural
// key: compiler.err.preview.feature.disabled.plural
// key: compiler.misc.feature.java.base.transitive
module m {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2025, 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
@ -166,7 +166,7 @@ public class JavaBaseTest {
for (String mod : mods) {
String key = mod.equals("static")
? "compiler.err.mod.not.allowed.here: " + mod
: "compiler.err.feature.not.supported.in.source.plural: (compiler.misc.feature.java.base.transitive)";
: "compiler.err.preview.feature.disabled.plural: (compiler.misc.feature.java.base.transitive)";
String message = "module-info.java:1:12: " + key;
if (log.contains(message)) {
foundErrorMessage = true;