7038363: cast from object to primitive should be for source >= 1.7 release70_fixes
authorjjg
Mon, 25 Apr 2011 15:56:09 -0700
branchrelease70_fixes
changeset 15026c66d593f9c2
parent 1501 c31120d5087a
7038363: cast from object to primitive should be for source >= 1.7
Reviewed-by: mcimadamore
src/share/classes/com/sun/tools/javac/code/Source.java
src/share/classes/com/sun/tools/javac/code/Types.java
test/tools/javac/types/CastObjectToPrimitiveTest.java
test/tools/javac/types/CastObjectToPrimitiveTest.out
     1.1 --- a/src/share/classes/com/sun/tools/javac/code/Source.java	Thu Mar 24 08:58:35 2011 +0100
     1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Source.java	Mon Apr 25 15:56:09 2011 -0700
     1.3 @@ -180,6 +180,9 @@
     1.4      public boolean allowSimplifiedVarargs() {
     1.5          return compareTo(JDK1_7) >= 0;
     1.6      }
     1.7 +    public boolean allowObjectToPrimitiveCast() {
     1.8 +        return compareTo(JDK1_7) >= 0;
     1.9 +    }
    1.10      public static SourceVersion toSourceVersion(Source source) {
    1.11          switch(source) {
    1.12          case JDK1_2:
     2.1 --- a/src/share/classes/com/sun/tools/javac/code/Types.java	Thu Mar 24 08:58:35 2011 +0100
     2.2 +++ b/src/share/classes/com/sun/tools/javac/code/Types.java	Mon Apr 25 15:56:09 2011 -0700
     2.3 @@ -77,8 +77,9 @@
     2.4      final Names names;
     2.5      final boolean allowGenerics;
     2.6      final boolean allowBoxing;
     2.7 +    final boolean allowCovariantReturns;
     2.8 +    final boolean allowObjectToPrimitiveCast;
     2.9      final ClassReader reader;
    2.10 -    final Source source;
    2.11      final Check chk;
    2.12      List<Warner> warnStack = List.nil();
    2.13      final Name capturedName;
    2.14 @@ -97,9 +98,11 @@
    2.15          scopeCounter = Scope.ScopeCounter.instance(context);
    2.16          names = Names.instance(context);
    2.17          reader = ClassReader.instance(context);
    2.18 -        source = Source.instance(context);
    2.19 +        Source source = Source.instance(context);
    2.20 +        allowBoxing = source.allowBoxing();
    2.21 +        allowCovariantReturns = source.allowCovariantReturns();
    2.22 +        allowObjectToPrimitiveCast = source.allowObjectToPrimitiveCast();
    2.23          allowGenerics = source.allowGenerics();
    2.24 -        allowBoxing = source.allowBoxing();
    2.25          chk = Check.instance(context);
    2.26          capturedName = names.fromString("<captured wildcard>");
    2.27          messages = JavacMessages.instance(context);
    2.28 @@ -960,8 +963,9 @@
    2.29              return true;
    2.30  
    2.31          if (t.isPrimitive() != s.isPrimitive())
    2.32 -            return allowBoxing && (isConvertible(t, s, warn) || isConvertible(s, t, warn));
    2.33 -
    2.34 +            return allowBoxing && (
    2.35 +                    isConvertible(t, s, warn)
    2.36 +                    || (allowObjectToPrimitiveCast && isConvertible(s, t, warn)));
    2.37          if (warn != warnStack.head) {
    2.38              try {
    2.39                  warnStack = warnStack.prepend(warn);
    2.40 @@ -2972,7 +2976,7 @@
    2.41  
    2.42          if (hasSameArgs(r1, r2))
    2.43              return covariantReturnType(r1.getReturnType(), r2res, warner);
    2.44 -        if (!source.allowCovariantReturns())
    2.45 +        if (!allowCovariantReturns)
    2.46              return false;
    2.47          if (isSubtypeUnchecked(r1.getReturnType(), r2res, warner))
    2.48              return true;
    2.49 @@ -2989,7 +2993,7 @@
    2.50      public boolean covariantReturnType(Type t, Type s, Warner warner) {
    2.51          return
    2.52              isSameType(t, s) ||
    2.53 -            source.allowCovariantReturns() &&
    2.54 +            allowCovariantReturns &&
    2.55              !t.isPrimitive() &&
    2.56              !s.isPrimitive() &&
    2.57              isAssignable(t, s, warner);
    2.58 @@ -3197,7 +3201,7 @@
    2.59          }
    2.60          if (giveWarning && !isReifiable(reverse ? from : to))
    2.61              warn.warn(LintCategory.UNCHECKED);
    2.62 -        if (!source.allowCovariantReturns())
    2.63 +        if (!allowCovariantReturns)
    2.64              // reject if there is a common method signature with
    2.65              // incompatible return types.
    2.66              chk.checkCompatibleAbstracts(warn.pos(), from, to);
    2.67 @@ -3224,7 +3228,7 @@
    2.68          Type t2 = to;
    2.69          if (disjointTypes(t1.getTypeArguments(), t2.getTypeArguments()))
    2.70              return false;
    2.71 -        if (!source.allowCovariantReturns())
    2.72 +        if (!allowCovariantReturns)
    2.73              // reject if there is a common method signature with
    2.74              // incompatible return types.
    2.75              chk.checkCompatibleAbstracts(warn.pos(), from, to);
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/tools/javac/types/CastObjectToPrimitiveTest.java	Mon Apr 25 15:56:09 2011 -0700
     3.3 @@ -0,0 +1,38 @@
     3.4 +/*
     3.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3.7 + *
     3.8 + * This code is free software; you can redistribute it and/or modify it
     3.9 + * under the terms of the GNU General Public License version 2 only, as
    3.10 + * published by the Free Software Foundation.
    3.11 + *
    3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    3.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    3.15 + * version 2 for more details (a copy is included in the LICENSE file that
    3.16 + * accompanied this code).
    3.17 + *
    3.18 + * You should have received a copy of the GNU General Public License version
    3.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    3.21 + *
    3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    3.23 + * or visit www.oracle.com if you need additional information or have any
    3.24 + * questions.
    3.25 + */
    3.26 +
    3.27 +/*
    3.28 + * @test
    3.29 + * @bug 7038363
    3.30 + * @summary cast from object to primitive should be for source >= 1.7
    3.31 + * @compile/fail/ref=CastObjectToPrimitiveTest.out -XDrawDiagnostics -Xlint:-options -source 5 CastObjectToPrimitiveTest.java
    3.32 + * @compile/fail/ref=CastObjectToPrimitiveTest.out -XDrawDiagnostics -Xlint:-options -source 6 CastObjectToPrimitiveTest.java
    3.33 + * @compile CastObjectToPrimitiveTest.java
    3.34 + */
    3.35 +
    3.36 +class CastObjectToPrimitiveTest {
    3.37 +    void m() {
    3.38 +        Object o = 42;
    3.39 +        int i = (int) o;
    3.40 +    }
    3.41 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/tools/javac/types/CastObjectToPrimitiveTest.out	Mon Apr 25 15:56:09 2011 -0700
     4.3 @@ -0,0 +1,2 @@
     4.4 +CastObjectToPrimitiveTest.java:36:23: compiler.err.prob.found.req: (compiler.misc.inconvertible.types), java.lang.Object, int
     4.5 +1 error