8207224: Javac compiles source code despite illegal use of unchecked conversions
authorvromero
Tue, 18 Dec 2018 16:22:46 -0500
changeset 53040 6aeb6a23fb83
parent 53039 3bc260237317
child 53041 f15af1e2c683
8207224: Javac compiles source code despite illegal use of unchecked conversions Reviewed-by: mcimadamore, darcy
src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java
src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java
test/langtools/tools/javac/T8207224/ReturnTypeSubstitutableTest.java
test/langtools/tools/javac/T8207224/ReturnTypeSubstitutableTest.out
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java	Tue Dec 18 07:33:07 2018 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java	Tue Dec 18 16:22:46 2018 -0500
@@ -189,7 +189,8 @@
         SWITCH_MULTIPLE_CASE_LABELS(JDK13, Fragments.FeatureMultipleCaseLabels, DiagKind.PLURAL),
         SWITCH_RULE(JDK13, Fragments.FeatureSwitchRules, DiagKind.PLURAL),
         SWITCH_EXPRESSION(JDK13, Fragments.FeatureSwitchExpressions, DiagKind.PLURAL),
-        RAW_STRING_LITERALS(JDK13, Fragments.FeatureRawStringLiterals, DiagKind.PLURAL);
+        RAW_STRING_LITERALS(JDK13, Fragments.FeatureRawStringLiterals, DiagKind.PLURAL),
+        RETURN_TYPE_SUBSTITUTABLE_SAME_TYPE(JDK13);
 
         enum DiagKind {
             NORMAL,
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java	Tue Dec 18 07:33:07 2018 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java	Tue Dec 18 16:22:46 2018 -0500
@@ -91,6 +91,7 @@
     final Names names;
     final boolean allowDefaultMethods;
     final boolean mapCapturesToBounds;
+    final boolean returnTypeSustitutableSameType;
     final Check chk;
     final Enter enter;
     JCDiagnostic.Factory diags;
@@ -114,6 +115,7 @@
         Source source = Source.instance(context);
         allowDefaultMethods = Feature.DEFAULT_METHODS.allowedInSource(source);
         mapCapturesToBounds = Feature.MAP_CAPTURES_TO_BOUNDS.allowedInSource(source);
+        returnTypeSustitutableSameType = Feature.RETURN_TYPE_SUBSTITUTABLE_SAME_TYPE.allowedInSource(source);
         chk = Check.instance(context);
         enter = Enter.instance(context);
         capturedName = names.fromString("<captured wildcard>");
@@ -4235,8 +4237,13 @@
             return covariantReturnType(r1.getReturnType(), r2res, warner);
         if (isSubtypeUnchecked(r1.getReturnType(), r2res, warner))
             return true;
-        if (!isSubtype(r1.getReturnType(), erasure(r2res)))
-            return false;
+        if (returnTypeSustitutableSameType) {
+            if (!isSameType(r1.getReturnType(), erasure(r2res)))
+                return false;
+        } else {
+            if (!isSubtype(r1.getReturnType(), erasure(r2res)))
+                return false;
+        }
         warner.warn(LintCategory.UNCHECKED);
         return true;
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/T8207224/ReturnTypeSubstitutableTest.java	Tue Dec 18 16:22:46 2018 -0500
@@ -0,0 +1,28 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 8207224
+ * @summary Javac compiles source code despite illegal use of unchecked conversions
+ * @compile/fail/ref=ReturnTypeSubstitutableTest.out -XDrawDiagnostics ReturnTypeSubstitutableTest.java
+ * @compile -source 12 ReturnTypeSubstitutableTest.java
+ */
+
+class ReturnTypeSubstitutableTest {
+    abstract class AbstractDemo<Request extends AbstractResult, Response extends AbstractResult> {
+        protected abstract Response test(Request request);
+    }
+
+    abstract interface AbstractResult {}
+
+    abstract interface SimpleResult extends AbstractResult {}
+
+    class Result1 implements SimpleResult {}
+
+    class OtherResult implements AbstractResult {}
+
+    public class SimpleDemo<Request extends AbstractResult, Response extends AbstractResult> extends AbstractDemo<Request, Response> {
+        @Override
+        protected SimpleResult test(AbstractResult request) {
+            return null;
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/T8207224/ReturnTypeSubstitutableTest.out	Tue Dec 18 16:22:46 2018 -0500
@@ -0,0 +1,2 @@
+ReturnTypeSubstitutableTest.java:24:32: compiler.err.override.incompatible.ret: (compiler.misc.cant.override: test(ReturnTypeSubstitutableTest.AbstractResult), ReturnTypeSubstitutableTest.SimpleDemo, test(Request), ReturnTypeSubstitutableTest.AbstractDemo), ReturnTypeSubstitutableTest.SimpleResult, Response
+1 error