src/share/classes/com/sun/tools/javac/code/Types.java

changeset 640
995bcdb9a41d
parent 637
c655e0280bdc
child 657
70ebdef189c9
     1.1 --- a/src/share/classes/com/sun/tools/javac/code/Types.java	Thu Aug 19 11:54:25 2010 +0100
     1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Types.java	Mon Aug 23 16:59:30 2010 +0100
     1.3 @@ -960,7 +960,7 @@
     1.4                      return true;
     1.5  
     1.6                  if (s.tag == TYPEVAR) {
     1.7 -                    if (isCastable(s.getUpperBound(), t, Warner.noWarnings)) {
     1.8 +                    if (isCastable(t, s.getUpperBound(), Warner.noWarnings)) {
     1.9                          warnStack.head.warnUnchecked();
    1.10                          return true;
    1.11                      } else {
    1.12 @@ -1030,7 +1030,12 @@
    1.13                                  && !disjointTypes(aHigh.allparams(), lowSub.allparams())
    1.14                                  && !disjointTypes(aLow.allparams(), highSub.allparams())
    1.15                                  && !disjointTypes(aLow.allparams(), lowSub.allparams())) {
    1.16 -                                if (upcast ? giveWarning(a, b) :
    1.17 +                                if (s.isInterface() &&
    1.18 +                                        !t.isInterface() &&
    1.19 +                                        t.isFinal() &&
    1.20 +                                        !isSubtype(t, s)) {
    1.21 +                                    return false;
    1.22 +                                } else if (upcast ? giveWarning(a, b) :
    1.23                                      giveWarning(b, a))
    1.24                                      warnStack.head.warnUnchecked();
    1.25                                  return true;
    1.26 @@ -1230,18 +1235,23 @@
    1.27          if (t == s) return false;
    1.28          if (t.tag == TYPEVAR) {
    1.29              TypeVar tv = (TypeVar) t;
    1.30 -            if (s.tag == TYPEVAR)
    1.31 -                s = s.getUpperBound();
    1.32              return !isCastable(tv.bound,
    1.33 -                               s,
    1.34 +                               relaxBound(s),
    1.35                                 Warner.noWarnings);
    1.36          }
    1.37          if (s.tag != WILDCARD)
    1.38              s = upperBound(s);
    1.39 -        if (s.tag == TYPEVAR)
    1.40 -            s = s.getUpperBound();
    1.41 -
    1.42 -        return !isSubtype(t, s);
    1.43 +
    1.44 +        return !isSubtype(t, relaxBound(s));
    1.45 +    }
    1.46 +
    1.47 +    private Type relaxBound(Type t) {
    1.48 +        if (t.tag == TYPEVAR) {
    1.49 +            while (t.tag == TYPEVAR)
    1.50 +                t = t.getUpperBound();
    1.51 +            t = rewriteQuantifiers(t, true, true);
    1.52 +        }
    1.53 +        return t;
    1.54      }
    1.55      // </editor-fold>
    1.56  
    1.57 @@ -3280,7 +3290,7 @@
    1.58       * quantifiers) only
    1.59       */
    1.60      private Type rewriteQuantifiers(Type t, boolean high, boolean rewriteTypeVars) {
    1.61 -        return new Rewriter(high, rewriteTypeVars).rewrite(t);
    1.62 +        return new Rewriter(high, rewriteTypeVars).visit(t);
    1.63      }
    1.64  
    1.65      class Rewriter extends UnaryVisitor<Type> {
    1.66 @@ -3293,25 +3303,21 @@
    1.67              this.rewriteTypeVars = rewriteTypeVars;
    1.68          }
    1.69  
    1.70 -        Type rewrite(Type t) {
    1.71 -            ListBuffer<Type> from = new ListBuffer<Type>();
    1.72 -            ListBuffer<Type> to = new ListBuffer<Type>();
    1.73 -            adaptSelf(t, from, to);
    1.74 +        @Override
    1.75 +        public Type visitClassType(ClassType t, Void s) {
    1.76              ListBuffer<Type> rewritten = new ListBuffer<Type>();
    1.77 -            List<Type> formals = from.toList();
    1.78              boolean changed = false;
    1.79 -            for (Type arg : to.toList()) {
    1.80 +            for (Type arg : t.allparams()) {
    1.81                  Type bound = visit(arg);
    1.82                  if (arg != bound) {
    1.83                      changed = true;
    1.84 -                    bound = high ? makeExtendsWildcard(bound, (TypeVar)formals.head)
    1.85 -                              : makeSuperWildcard(bound, (TypeVar)formals.head);
    1.86                  }
    1.87                  rewritten.append(bound);
    1.88 -                formals = formals.tail;
    1.89              }
    1.90              if (changed)
    1.91 -                return subst(t.tsym.type, from.toList(), rewritten.toList());
    1.92 +                return subst(t.tsym.type,
    1.93 +                        t.tsym.type.allparams(),
    1.94 +                        rewritten.toList());
    1.95              else
    1.96                  return t;
    1.97          }
    1.98 @@ -3322,13 +3328,22 @@
    1.99  
   1.100          @Override
   1.101          public Type visitCapturedType(CapturedType t, Void s) {
   1.102 -            return visitWildcardType(t.wildcard, null);
   1.103 +            Type bound = visitWildcardType(t.wildcard, null);
   1.104 +            return (bound.contains(t)) ?
   1.105 +                    (high ? syms.objectType : syms.botType) :
   1.106 +                        bound;
   1.107          }
   1.108  
   1.109          @Override
   1.110          public Type visitTypeVar(TypeVar t, Void s) {
   1.111 -            if (rewriteTypeVars)
   1.112 -                return high ? t.bound : syms.botType;
   1.113 +            if (rewriteTypeVars) {
   1.114 +                Type bound = high ?
   1.115 +                    (t.bound.contains(t) ?
   1.116 +                        syms.objectType :
   1.117 +                        visit(t.bound)) :
   1.118 +                    syms.botType;
   1.119 +                return rewriteAsWildcardType(bound, t);
   1.120 +            }
   1.121              else
   1.122                  return t;
   1.123          }
   1.124 @@ -3338,11 +3353,31 @@
   1.125              Type bound = high ? t.getExtendsBound() :
   1.126                                  t.getSuperBound();
   1.127              if (bound == null)
   1.128 -                bound = high ? syms.objectType : syms.botType;
   1.129 -            return bound;
   1.130 +            bound = high ? syms.objectType : syms.botType;
   1.131 +            return rewriteAsWildcardType(visit(bound), t.bound);
   1.132 +        }
   1.133 +
   1.134 +        private Type rewriteAsWildcardType(Type bound, TypeVar formal) {
   1.135 +            return high ?
   1.136 +                makeExtendsWildcard(B(bound), formal) :
   1.137 +                makeSuperWildcard(B(bound), formal);
   1.138 +        }
   1.139 +
   1.140 +        Type B(Type t) {
   1.141 +            while (t.tag == WILDCARD) {
   1.142 +                WildcardType w = (WildcardType)t;
   1.143 +                t = high ?
   1.144 +                    w.getExtendsBound() :
   1.145 +                    w.getSuperBound();
   1.146 +                if (t == null) {
   1.147 +                    t = high ? syms.objectType : syms.botType;
   1.148 +                }
   1.149 +            }
   1.150 +            return t;
   1.151          }
   1.152      }
   1.153  
   1.154 +
   1.155      /**
   1.156       * Create a wildcard with the given upper (extends) bound; create
   1.157       * an unbounded wildcard if bound is Object.

mercurial