7118412: Shadowing of type-variables vs. member types

Wed, 21 Aug 2013 20:23:36 -0400

author
emc
date
Wed, 21 Aug 2013 20:23:36 -0400
changeset 1970
2068190f8ac2
parent 1969
7de231613e4a
child 1971
57e1266527dd

7118412: Shadowing of type-variables vs. member types
4987840: What is the scope of an annotation?
Summary: Fixed issue with shadowing of type names.
Reviewed-by: jjg, abuckley, mcimadamore

src/share/classes/com/sun/tools/javac/comp/Resolve.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Resolve.java	Wed Aug 21 16:13:50 2013 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Resolve.java	Wed Aug 21 20:23:36 2013 -0400
     1.3 @@ -1859,7 +1859,10 @@
     1.4          }
     1.5      }
     1.6  
     1.7 -    /** Find qualified member type.
     1.8 +
     1.9 +    /**
    1.10 +     * Find a type declared in a scope (not inherited).  Return null
    1.11 +     * if none is found.
    1.12       *  @param env       The current environment.
    1.13       *  @param site      The original type from where the selection takes
    1.14       *                   place.
    1.15 @@ -1868,12 +1871,10 @@
    1.16       *                   always a superclass or implemented interface of
    1.17       *                   site's class.
    1.18       */
    1.19 -    Symbol findMemberType(Env<AttrContext> env,
    1.20 -                          Type site,
    1.21 -                          Name name,
    1.22 -                          TypeSymbol c) {
    1.23 -        Symbol bestSoFar = typeNotFound;
    1.24 -        Symbol sym;
    1.25 +    Symbol findImmediateMemberType(Env<AttrContext> env,
    1.26 +                                   Type site,
    1.27 +                                   Name name,
    1.28 +                                   TypeSymbol c) {
    1.29          Scope.Entry e = c.members().lookup(name);
    1.30          while (e.scope != null) {
    1.31              if (e.sym.kind == TYP) {
    1.32 @@ -1883,6 +1884,24 @@
    1.33              }
    1.34              e = e.next();
    1.35          }
    1.36 +        return typeNotFound;
    1.37 +    }
    1.38 +
    1.39 +    /** Find a member type inherited from a superclass or interface.
    1.40 +     *  @param env       The current environment.
    1.41 +     *  @param site      The original type from where the selection takes
    1.42 +     *                   place.
    1.43 +     *  @param name      The type's name.
    1.44 +     *  @param c         The class to search for the member type. This is
    1.45 +     *                   always a superclass or implemented interface of
    1.46 +     *                   site's class.
    1.47 +     */
    1.48 +    Symbol findInheritedMemberType(Env<AttrContext> env,
    1.49 +                                   Type site,
    1.50 +                                   Name name,
    1.51 +                                   TypeSymbol c) {
    1.52 +        Symbol bestSoFar = typeNotFound;
    1.53 +        Symbol sym;
    1.54          Type st = types.supertype(c.type);
    1.55          if (st != null && st.hasTag(CLASS)) {
    1.56              sym = findMemberType(env, site, name, st.tsym);
    1.57 @@ -1901,6 +1920,28 @@
    1.58          return bestSoFar;
    1.59      }
    1.60  
    1.61 +    /** Find qualified member type.
    1.62 +     *  @param env       The current environment.
    1.63 +     *  @param site      The original type from where the selection takes
    1.64 +     *                   place.
    1.65 +     *  @param name      The type's name.
    1.66 +     *  @param c         The class to search for the member type. This is
    1.67 +     *                   always a superclass or implemented interface of
    1.68 +     *                   site's class.
    1.69 +     */
    1.70 +    Symbol findMemberType(Env<AttrContext> env,
    1.71 +                          Type site,
    1.72 +                          Name name,
    1.73 +                          TypeSymbol c) {
    1.74 +        Symbol sym = findImmediateMemberType(env, site, name, c);
    1.75 +
    1.76 +        if (sym != typeNotFound)
    1.77 +            return sym;
    1.78 +
    1.79 +        return findInheritedMemberType(env, site, name, c);
    1.80 +
    1.81 +    }
    1.82 +
    1.83      /** Find a global type in given scope and load corresponding class.
    1.84       *  @param env       The current environment.
    1.85       *  @param scope     The scope in which to look for the type.
    1.86 @@ -1919,6 +1960,21 @@
    1.87          return bestSoFar;
    1.88      }
    1.89  
    1.90 +    Symbol findTypeVar(Env<AttrContext> env, Name name, boolean staticOnly) {
    1.91 +        for (Scope.Entry e = env.info.scope.lookup(name);
    1.92 +             e.scope != null;
    1.93 +             e = e.next()) {
    1.94 +            if (e.sym.kind == TYP) {
    1.95 +                if (staticOnly &&
    1.96 +                    e.sym.type.hasTag(TYPEVAR) &&
    1.97 +                    e.sym.owner.kind == TYP)
    1.98 +                    return new StaticError(e.sym);
    1.99 +                return e.sym;
   1.100 +            }
   1.101 +        }
   1.102 +        return typeNotFound;
   1.103 +    }
   1.104 +
   1.105      /** Find an unqualified type symbol.
   1.106       *  @param env       The current environment.
   1.107       *  @param name      The type's name.
   1.108 @@ -1929,19 +1985,26 @@
   1.109          boolean staticOnly = false;
   1.110          for (Env<AttrContext> env1 = env; env1.outer != null; env1 = env1.outer) {
   1.111              if (isStatic(env1)) staticOnly = true;
   1.112 -            for (Scope.Entry e = env1.info.scope.lookup(name);
   1.113 -                 e.scope != null;
   1.114 -                 e = e.next()) {
   1.115 -                if (e.sym.kind == TYP) {
   1.116 -                    if (staticOnly &&
   1.117 -                        e.sym.type.hasTag(TYPEVAR) &&
   1.118 -                        e.sym.owner.kind == TYP) return new StaticError(e.sym);
   1.119 -                    return e.sym;
   1.120 -                }
   1.121 +            // First, look for a type variable and the first member type
   1.122 +            final Symbol tyvar = findTypeVar(env1, name, staticOnly);
   1.123 +            sym = findImmediateMemberType(env1, env1.enclClass.sym.type,
   1.124 +                                          name, env1.enclClass.sym);
   1.125 +
   1.126 +            // Return the type variable if we have it, and have no
   1.127 +            // immediate member, OR the type variable is for a method.
   1.128 +            if (tyvar != typeNotFound) {
   1.129 +                if (sym == typeNotFound ||
   1.130 +                    (tyvar.kind == TYP && tyvar.exists() &&
   1.131 +                     tyvar.owner.kind == MTH))
   1.132 +                    return tyvar;
   1.133              }
   1.134  
   1.135 -            sym = findMemberType(env1, env1.enclClass.sym.type, name,
   1.136 -                                 env1.enclClass.sym);
   1.137 +            // If the environment is a class def, finish up,
   1.138 +            // otherwise, do the entire findMemberType
   1.139 +            if (sym == typeNotFound)
   1.140 +                sym = findInheritedMemberType(env1, env1.enclClass.sym.type,
   1.141 +                                              name, env1.enclClass.sym);
   1.142 +
   1.143              if (staticOnly && sym.kind == TYP &&
   1.144                  sym.type.hasTag(CLASS) &&
   1.145                  sym.type.getEnclosingType().hasTag(CLASS) &&

mercurial