JDK-6667132 : Cast to partially raw type(rare type) should not be allowed
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 7
  • Priority: P4
  • Status: Closed
  • Resolution: Cannot Reproduce
  • OS: generic
  • CPU: generic
  • Submitted: 2008-02-25
  • Updated: 2010-04-06
  • Resolved: 2009-01-20
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7Resolved
Related Reports
Relates :  
Relates :  
Description
Description:
JLS Section 4.8 states that a generic inner class of a raw type can itself only be used as a raw type,it is not possible to access Inner as partially raw type (a "rare" type).But in the following code,cast to a partially raw type is allowed by the compiler. Compiler should instead be throwing an error.

<code>
bash-3.00$ 
<code>
bash-3.00$ cat Outer.java 
<code>
class Outer<T>{ 
    
     class Inner<S>{      
    }
     <R> void met(R r){
         
     }
     void met2(){
	 Outer.Inner oo2 = (Outer.Inner<String> )null; //Cast to partial raw type should not compile
	//  Outer.Inner<String> oo2 = null; // This does not compile on uncommenting, as expected.
	 Outer<String>.Inner<String> oo = null;
         this.met((Outer.Inner<String> )oo);//Cast to partial raw type should not compile
     }    
    
              
}
</code>
Compiles fine. Compiler should actually be throwing an error as the cast type is illegal.

<version>
/net/sqindia/export/disk09/jdk/7/latest/binaries/solsparc/bin/java -version
java version "1.7.0-ea"
Java(TM) SE Runtime Environment (build 1.7.0-ea-b24)
Java HotSpot(TM) Client VM (build 12.0-b01, mixed mode)

bash-3.00$ uname -a
SunOS hrajan 5.10 Generic sun4u sparc SUNW,Sun-Blade-100

Comments
EVALUATION Solved 6665356 as a result this is not reproducible (7 b44)
20-01-2009

SUGGESTED FIX diff -r 9a66ca7c79fa src/share/classes/com/sun/tools/javac/comp/Attr.java --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java Sat Dec 01 00:00:00 2007 +0000 +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java Mon Feb 25 09:57:20 2008 +0000 @@ -1714,6 +1714,7 @@ public class Attr extends JCTree.Visitor public void visitTypeCast(JCTypeCast tree) { Type clazztype = attribType(tree.clazz, env); + chk.validate(tree.clazz); Type exprtype = attribExpr(tree.expr, env, Infer.anyPoly); Type owntype = chk.checkCastable(tree.expr.pos(), exprtype, clazztype); if (exprtype.constValue() != null)
25-02-2008

EVALUATION This is a bug. As reported in the 'description' section, JLS (4.8) state that: "it is not possible to access Inner as partially raw type (a "rare" type) Outer.Inner<Double> x = null; // illegalt is not possible to access Inner as partially raw type (a "rare" type)" and also that: "...This means that the ban on "rare" types extends to the case where the qualifying type is parameterized, but we attempt to use the inner class as a raw type: Outer<Integer>.Inner x = null; // illegal" So, the above code should not compile. There is, I think, an historical reason for which javac accepts bad formed types as the target type of a cast. The reason is to be searched in CRs 6665356 and 6558462. See 'workaround' for further details. Solving this bug is quite easy, as the bug is caused by javac not validating the target type of a cast (this is different wrt e.g. the target type of an instanceof expression). The solution is in inserting the well-formedness check when the code of a cast expression is to be attributed.
25-02-2008

WORK AROUND Since the compiler is currently not accepting full generic qualified types as target types of a cast (as Outer<S>.Inner<T> -see CR 6665356), it's not possible to use e.g. Outer<String>.Inner<?> instead of a rare type.
25-02-2008