8152062: obscure error message for bad 'provides'
authorvromero
Tue, 31 May 2016 13:00:17 -0400
changeset 38827 884d32899770
parent 38826 0e5f358c2155
child 38828 520da69917b5
8152062: obscure error message for bad 'provides' Reviewed-by: jjg
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties
langtools/test/tools/javac/diags/examples/ServiceImplMustBeSubtypeOfServiceIntf/module-info.java
langtools/test/tools/javac/modules/ObscureMessageForBadProvidesTest.java
langtools/test/tools/javac/modules/ProvidesTest.java
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java	Mon May 30 16:36:41 2016 +0200
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java	Tue May 31 13:00:17 2016 -0400
@@ -68,6 +68,7 @@
 import com.sun.tools.javac.code.Symbol.PackageSymbol;
 import com.sun.tools.javac.code.Symtab;
 import com.sun.tools.javac.code.Type;
+import com.sun.tools.javac.code.Types;
 import com.sun.tools.javac.jvm.ClassWriter;
 import com.sun.tools.javac.jvm.JNIWriter;
 import com.sun.tools.javac.main.Option;
@@ -123,6 +124,7 @@
     private final Symtab syms;
     private final Attr attr;
     private final TypeEnvs typeEnvs;
+    private final Types types;
     private final JavaFileManager fileManager;
     private final ModuleFinder moduleFinder;
     private final boolean allowModules;
@@ -160,6 +162,7 @@
         attr = Attr.instance(context);
         typeEnvs = TypeEnvs.instance(context);
         moduleFinder = ModuleFinder.instance(context);
+        types = Types.instance(context);
         fileManager = context.get(JavaFileManager.class);
         allowModules = Source.instance(context).allowModules();
         Options options = Options.instance(context);
@@ -713,9 +716,12 @@
         @Override
         public void visitProvides(JCProvides tree) {
             Type st = attr.attribType(tree.serviceName, env, syms.objectType);
-            Type it = attr.attribType(tree.implName, env, st);
+            Type it = attr.attribType(tree.implName, env, syms.objectType);
             ClassSymbol service = (ClassSymbol) st.tsym;
             ClassSymbol impl = (ClassSymbol) it.tsym;
+            if (!types.isSubtype(it, st)) {
+                log.error(tree.implName.pos(), Errors.ServiceImplementationMustBeSubtypeOfServiceInterface);
+            }
             if ((impl.flags() & ABSTRACT) != 0) {
                 log.error(tree.implName.pos(), Errors.ServiceImplementationIsAbstract(impl));
             } else if (impl.isInner()) {
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Mon May 30 16:36:41 2016 +0200
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties	Tue May 31 13:00:17 2016 -0400
@@ -2715,6 +2715,9 @@
 compiler.err.service.implementation.is.abstract=\
     the service implementation is an abstract class: {0}
 
+compiler.err.service.implementation.must.be.subtype.of.service.interface=\
+    the service implementation type must be a subtype of the service interface type
+
 # 0: symbol
 compiler.err.service.implementation.is.inner=\
     the service implementation is an inner class: {0}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/diags/examples/ServiceImplMustBeSubtypeOfServiceIntf/module-info.java	Tue May 31 13:00:17 2016 -0400
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2016, 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+// key: compiler.err.service.implementation.must.be.subtype.of.service.interface
+// key: compiler.err.service.implementation.not.in.right.module
+
+module ServiceImplMustBeSubtypeOfServiceIntf {
+    provides java.lang.String with java.io.File;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/modules/ObscureMessageForBadProvidesTest.java	Tue May 31 13:00:17 2016 -0400
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2016, 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8152062
+ * @summary obscure error message for bad 'provides'
+ * @library /tools/lib
+ * @modules
+ *      jdk.compiler/com.sun.tools.javac.api
+ *      jdk.compiler/com.sun.tools.javac.main
+ * @build toolbox.ToolBox toolbox.JavacTask ModuleTestBase
+ * @run main ObscureMessageForBadProvidesTest
+ */
+
+import java.nio.file.Path;
+
+import toolbox.JavacTask;
+import toolbox.Task;
+import toolbox.ToolBox;
+
+public class ObscureMessageForBadProvidesTest extends ModuleTestBase {
+    public static void main(String... args) throws Exception {
+        new ObscureMessageForBadProvidesTest().runTests();
+    }
+
+    @Test
+    public void theTest(Path base) throws Exception {
+        Path mod = base.resolve("mod");
+        tb.writeJavaFiles(mod, "module mod { provides java.lang.String with java.io.File; }");
+        Path classes = base.resolve("classes");
+        tb.createDirectories(classes);
+        String log = new JavacTask(tb)
+                .options("-XDrawDiagnostics")
+                .outdir(classes)
+                .files(findJavaFiles(mod))
+                .run(Task.Expect.FAIL)
+                .writeAll()
+                .getOutput(Task.OutputKind.DIRECT);
+
+        if (!log.startsWith("module-info.java:1:52: compiler.err.service.implementation.must.be.subtype.of.service.interface"))
+            throw new Exception("expected output not found");
+    }
+}
--- a/langtools/test/tools/javac/modules/ProvidesTest.java	Mon May 30 16:36:41 2016 +0200
+++ b/langtools/test/tools/javac/modules/ProvidesTest.java	Tue May 31 13:00:17 2016 -0400
@@ -143,8 +143,7 @@
         List<String> expected = Arrays.asList(
                 "C.java:1:36: compiler.err.cant.resolve.location: kindname.class, Missing, , , (compiler.misc.location: kindname.package, p, null)",
                 "module-info.java:1:22: compiler.err.cant.resolve.location: kindname.class, Missing, , , (compiler.misc.location: kindname.package, p, null)",
-                "module-info.java:1:37: compiler.err.service.implementation.doesnt.have.a.no.args.constructor: <any>",
-                "3 errors");
+                "2 errors");
         if (!output.containsAll(expected)) {
             throw new Exception("Expected output not found");
         }
@@ -192,8 +191,11 @@
                 .writeAll()
                 .getOutputLines(Task.OutputKind.DIRECT);
 
-        List<String> expected = Arrays.asList("module-info.java:1:31: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: p.B, p.A)",
-                "1 error");
+        List<String> expected = Arrays.asList(
+                "module-info.java:1:31: compiler.err.service.implementation.must.be.subtype.of.service.interface",
+                "module-info.java:1:12: compiler.warn.service.provided.but.not.exported.or.used: p.A",
+                "1 error",
+                "1 warning");
         if (!output.containsAll(expected)) {
             throw new Exception("Expected output not found");
         }