Wiki

Ticket #76: typecast-to-nil-warnings.patch

File typecast-to-nil-warnings.patch, 3.6 KB (added by hopscc, 15 years ago)
  • Source/BinaryOpExpr.cobra

     
    924924 
    925925    cue init(opToken as IToken, op as String, left as Expr, right as Expr) 
    926926        base.init(opToken, op, left, right) 
    927         # TODO: use IPotentialTypeExpr like ToExpr does 
    928927 
    929928    def _bindImp 
    930929        base._bindImp 
    931930        if not _type inherits NilableType 
    932931            _type = NilableType(_type).bindAll to NilableType  # CC: axe cast when 'as this' is supported 
    933         # TODO: warn about redundancy 
     932        # warn about redundancy 
     933        rightTypeExpr = _right to IPotentialTypeExpr 
     934        rightType = rightTypeExpr.potentialType 
     935        if rightType 
     936            if _left.type inherits NilableType and not rightType inherits NilableType 
     937                if _left.type.nonNil == rightType 
     938                    .compiler.warning(this, 'The given expression is already a "[rightTypeExpr.toCobraSource]", but nilable. You can use "to !" to remove the nilability or remove the redundant cast.') 
     939            else if not _left.type inherits NilableType and rightType inherits NilableType 
     940                pass # TODO: chk rhs type is castable to lhs regardless of nilibility 
     941            else if _left.type == rightType 
     942                .compiler.warning(this, 'The given expression is already a "[rightTypeExpr.toCobraSource]" so the typecast is redundant. You can remove it.') 
     943        else 
     944            .throwError('Cannot locate a type for the type cast "to [rightTypeExpr.toCobraSource]".') 
    934945 
    935946 
    936947class CoalesceExpr 
  • Tests/800-warnings/201-typecast-nil-warnings.cobra

     
     1class Foo 
     2 
     3    def main is shared 
     4         
     5        a = TypeA() 
     6        b = a to? TypeA  # .warning. The given expression is already a "TypeA" so the typecast is redundant. You can remove it 
     7        c = a to ? 
     8        d = c to? TypeA  # .warning. The given expression is already a "TypeA", but nilable. You can use "to !" to remove the nilability or remove the redundant cast. 
     9         
     10        ok = a to? TypeA? 
     11        e = c to? TypeA? # .warning. The given expression is already a "TypeA?" so the typecast is redundant. You can remove it. 
     12        CobraCore.noOp(a, b, c, d, ok, e) 
     13     
     14         
     15        s as String? = 'aoeu' 
     16        t = s to? String  # .warning. The given expression is already a "String", but nilable. 
     17 
     18        ba = B() to? A    # no warning here because casting to a more general type is a legit way to affect type inference 
     19        .compute(ba)      # just to use the variable to avoid a warning 
     20 
     21        u = 'aoeu' to? String? 
     22        t = u to? String  # .warning. The given expression is already a "String", but nilable. You can use "to !" to remove the nilability or remove the redundant cast. 
     23        assert t 
     24         
     25        u2 = u to? String?  # .warning. The given expression is already a "String?" so the typecast is redundant. You can remove it. 
     26        assert u2  # just to use the variable to avoid a warning 
     27 
     28         
     29    def compute(a as A?) is shared 
     30        pass 
     31 
     32class A 
     33    pass 
     34 
     35 
     36class B 
     37    inherits A   
     38    pass 
     39     
     40class TypeA 
     41    pass 
     42     
  • Developer/IntermediateReleaseNotes.text

     
    392392* Fixed: Cobra gives an inscrutable error message for numeric for-loops that have incompatible numeric types. ticket:165 
    393393 
    394394* Fixed: Improper codegen for static constructors. ticket:169 
     395 
     396* Fixed: to? should give warning if operation is redundant. ticket:76 
     397