src/share/classes/com/sun/tools/javac/comp/Check.java

changeset 383
8109aa93b212
parent 377
d9febdd5ae21
child 398
8d999cb7ec09
     1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Check.java	Wed Aug 26 19:28:51 2009 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Check.java	Thu Aug 27 13:40:48 2009 +0100
     1.3 @@ -362,8 +362,6 @@
     1.4      Type checkType(DiagnosticPosition pos, Type found, Type req) {
     1.5          if (req.tag == ERROR)
     1.6              return req;
     1.7 -        if (found.tag == FORALL)
     1.8 -            return instantiatePoly(pos, (ForAll)found, req, convertWarner(pos, found, req));
     1.9          if (req.tag == NONE)
    1.10              return found;
    1.11          if (types.isAssignable(found, req, convertWarner(pos, found, req)))
    1.12 @@ -381,11 +379,38 @@
    1.13          return typeError(pos, diags.fragment("incompatible.types"), found, req);
    1.14      }
    1.15  
    1.16 +    Type checkReturnType(DiagnosticPosition pos, Type found, Type req) {
    1.17 +        if (found.tag == FORALL) {
    1.18 +            try {
    1.19 +                return instantiatePoly(pos, (ForAll) found, req, convertWarner(pos, found, req));
    1.20 +            } catch (Infer.NoInstanceException ex) {
    1.21 +                if (ex.isAmbiguous) {
    1.22 +                    JCDiagnostic d = ex.getDiagnostic();
    1.23 +                    log.error(pos,
    1.24 +                            "undetermined.type" + (d != null ? ".1" : ""),
    1.25 +                            found, d);
    1.26 +                    return types.createErrorType(req);
    1.27 +                } else {
    1.28 +                    JCDiagnostic d = ex.getDiagnostic();
    1.29 +                    return typeError(pos,
    1.30 +                            diags.fragment("incompatible.types" + (d != null ? ".1" : ""), d),
    1.31 +                            found, req);
    1.32 +                }
    1.33 +            } catch (Infer.InvalidInstanceException ex) {
    1.34 +                JCDiagnostic d = ex.getDiagnostic();
    1.35 +                log.error(pos, "invalid.inferred.types", ((ForAll)found).tvars, d);
    1.36 +                return types.createErrorType(req);
    1.37 +            }
    1.38 +        } else {
    1.39 +            return checkType(pos, found, req);
    1.40 +        }
    1.41 +    }
    1.42 +
    1.43      /** Instantiate polymorphic type to some prototype, unless
    1.44       *  prototype is `anyPoly' in which case polymorphic type
    1.45       *  is returned unchanged.
    1.46       */
    1.47 -    Type instantiatePoly(DiagnosticPosition pos, ForAll t, Type pt, Warner warn) {
    1.48 +    Type instantiatePoly(DiagnosticPosition pos, ForAll t, Type pt, Warner warn) throws Infer.NoInstanceException {
    1.49          if (pt == Infer.anyPoly && complexInference) {
    1.50              return t;
    1.51          } else if (pt == Infer.anyPoly || pt.tag == NONE) {
    1.52 @@ -394,28 +419,9 @@
    1.53          } else if (pt.tag == ERROR) {
    1.54              return pt;
    1.55          } else {
    1.56 -            try {
    1.57 -                return infer.instantiateExpr(t, pt, warn);
    1.58 -            } catch (Infer.NoInstanceException ex) {
    1.59 -                if (ex.isAmbiguous) {
    1.60 -                    JCDiagnostic d = ex.getDiagnostic();
    1.61 -                    log.error(pos,
    1.62 -                              "undetermined.type" + (d!=null ? ".1" : ""),
    1.63 -                              t, d);
    1.64 -                    return types.createErrorType(pt);
    1.65 -                } else {
    1.66 -                    JCDiagnostic d = ex.getDiagnostic();
    1.67 -                    return typeError(pos,
    1.68 -                                     diags.fragment("incompatible.types" + (d!=null ? ".1" : ""), d),
    1.69 -                                     t, pt);
    1.70 -                }
    1.71 -            } catch (Infer.InvalidInstanceException ex) {
    1.72 -                JCDiagnostic d = ex.getDiagnostic();
    1.73 -                log.error(pos, "invalid.inferred.types", t.tvars, d);
    1.74 -                return types.createErrorType(pt);
    1.75 -            }
    1.76 +            return infer.instantiateExpr(t, pt, warn);
    1.77          }
    1.78 -    }
    1.79 +     }
    1.80  
    1.81      /** Check that a given type can be cast to a given target type.
    1.82       *  Return the result of the cast.
    1.83 @@ -538,6 +544,29 @@
    1.84          return t;
    1.85      }
    1.86  
    1.87 +    /** Check that type is a valid type for a new expression. If the type contains
    1.88 +     * some uninferred type variables, instantiate them exploiting the expected
    1.89 +     * type.
    1.90 +     *
    1.91 +     *  @param pos           Position to be used for error reporting.
    1.92 +     *  @param t             The type to be checked.
    1.93 +     *  @param noBounds    True if type bounds are illegal here.
    1.94 +     *  @param pt          Expected type (used with diamond operator)
    1.95 +     */
    1.96 +    Type checkNewClassType(DiagnosticPosition pos, Type t, boolean noBounds, Type pt) {
    1.97 +        if (t.tag == FORALL) {
    1.98 +            try {
    1.99 +                t = instantiatePoly(pos, (ForAll)t, pt, Warner.noWarnings);
   1.100 +            }
   1.101 +            catch (Infer.NoInstanceException ex) {
   1.102 +                JCDiagnostic d = ex.getDiagnostic();
   1.103 +                log.error(pos, "cant.apply.diamond", t.getTypeArguments(), d);
   1.104 +                return types.createErrorType(pt);
   1.105 +            }
   1.106 +        }
   1.107 +        return checkClassType(pos, t, noBounds);
   1.108 +    }
   1.109 +
   1.110      /** Check that type is a reifiable class, interface or array type.
   1.111       *  @param pos           Position to be used for error reporting.
   1.112       *  @param t             The type to be checked.
   1.113 @@ -890,7 +919,8 @@
   1.114                  }
   1.115  
   1.116                  checkCapture(tree);
   1.117 -
   1.118 +            }
   1.119 +            if (tree.type.tag == CLASS || tree.type.tag == FORALL) {
   1.120                  // Check that this type is either fully parameterized, or
   1.121                  // not parameterized at all.
   1.122                  if (tree.type.getEnclosingType().isRaw())

mercurial