Menu

Inheritance and EnhancedFor

Developers
2011-06-01
2012-10-08
  • Andrea Poli

    Andrea Poli - 2011-06-01

    Hi, I tried to analyze this piece of code usign RECODER:

    Collection<Integer> numbers = new ArrayList<Integer>();
            numbers.add((Integer) num);
    
            this.numbers = new int[numbers.size()];
            int c = 0;
            for (Number n : numbers) {          
                this.numbers[c++] = n.intValue();
            }
    

    I got the following error:

    +++ Error: TypingException - lhs does not match rhs - java.lang.Number :
    java.util.Collection<java.lang.Integer> - exp: numbers - 35/25 - - -
    recoder.service.SemanticsChecker$SemanticsCheckerVisitor.visitEnhancedFor(Sema
    nticsChecker.java:163)
    recoder.java.statement.EnhancedFor.accept(EnhancedFor.java:117)
    recoder.service.SemanticsChecker.check(SemanticsChecker.java:105)
    recoder.service.SemanticsChecker.checkAllCompilationUnits(SemanticsChecker.jav
    a:92)
    ...

    (Error occurred outside a model update)

    I started to debug the code and it seems that Number isn't a supertype of
    Integer according to:

    public boolean isSubtype(ClassType a, ClassType b)
    

    method of class recoder.service.DefaultProgramModelInfo. During the debug
    parameter a value was Number type and parameter b value was Integer type.

    I tried to add this piece of code at line 546 of the above method:

    }else if (b.getSupertypes().contains(a)){
                return true;
            }
    

    After this change Number is recognized as a supertype of Integer but it isn't
    the right way to do it because it makes other typing errors appear.

    Thanks a lot :-)

     
  • Andrea Poli

    Andrea Poli - 2011-06-01

    I think the problem is the call to isWidening inside
    recoder.service.SemanticsChecker at line 156, visitEnhancedFor method.

                            if (si.isWidening(lhsType, ct)) {
                                    foundProperType = true;
                                break;
                            }
    

    It seems that it checks if lhsType (initializer) is a subtype of ct (guard)
    but it should check if ct is a subtype of lhsType.

                            if (si.isWidening(ct, lhsType)) {
                                    foundProperType = true;
                                break;
                            }
    
     
  • Tobias Gutzmann

    Tobias Gutzmann - 2011-06-12

    Correct. It's a bug. Please use the bug tracker for those :)
    Fixed in SVN / next release.

     

Log in to post a comment.