From 05a00638c4e74ad9099593a9f8a8c368e09919c9 Mon Sep 17 00:00:00 2001 From: Jan Lahoda Date: Tue, 14 Jan 2025 13:05:52 +0100 Subject: [PATCH] Backport bb93f67ea8955216e81d1aef58d0ec8bf1fc9bb1 --- .../com/sun/tools/javac/comp/Modules.java | 15 +--- test/langtools/tools/javac/ImportModule.java | 85 ++++++++++++++++++- .../ModifierNotAllowed/module-info.java | 4 +- .../tools/javac/modules/JavaBaseTest.java | 4 +- 4 files changed, 90 insertions(+), 18 deletions(-) diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java index bfad334d1942f..96d633cac7f95 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java @@ -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.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 @@ protected Modules(Context context) { 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 void visitRequires(JCRequires tree) { Set 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); diff --git a/test/langtools/tools/javac/ImportModule.java b/test/langtools/tools/javac/ImportModule.java index ac00ea823f134..a53bc92f1b03f 100644 --- a/test/langtools/tools/javac/ImportModule.java +++ b/test/langtools/tools/javac/ImportModule.java @@ -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.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 Object {} .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 actualErrors = new JavacTask(tb) + .options("-XDrawDiagnostics") + .outdir(maClasses) + .files(tb.findJavaFiles(ma)) + .run(Task.Expect.FAIL) + .writeAll() + .getOutputLines(Task.OutputKind.DIRECT); + + List 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"); + } + } } diff --git a/test/langtools/tools/javac/diags/examples/ModifierNotAllowed/module-info.java b/test/langtools/tools/javac/diags/examples/ModifierNotAllowed/module-info.java index f2e5899a91566..02cb11f5ac257 100644 --- a/test/langtools/tools/javac/diags/examples/ModifierNotAllowed/module-info.java +++ b/test/langtools/tools/javac/diags/examples/ModifierNotAllowed/module-info.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 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 { diff --git a/test/langtools/tools/javac/modules/JavaBaseTest.java b/test/langtools/tools/javac/modules/JavaBaseTest.java index 935ddfa1d437f..a888c430f5369 100644 --- a/test/langtools/tools/javac/modules/JavaBaseTest.java +++ b/test/langtools/tools/javac/modules/JavaBaseTest.java @@ -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 @@ void testSource(Path base, List mods, String target) throws Exception { 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;