Wiki

Ticket #117: non-nilable-coalesce.patch

File non-nilable-coalesce.patch, 3.2 KB (added by hopscc, 15 years ago)
  • Source/BinaryOpExpr.cobra

     
    950950        if _left.type is ptt or _right.type is ptt 
    951951            # x ? y   ...where either is typed as passthrough 
    952952            _type = ptt 
     953        else if not _left.type inherits NilableType  
     954            .throwError('The left hand type of "?" is not nilable (it\'s "[_left.type.name]") so the expression will always evaluate to the left hand side.') 
    953955        else if _left.type inherits NilableType and not _right.type inherits NilableType 
    954956            # x ? y   ...where x can be nil, but y cannot 
    955957            _type = _left.type.nonNil  # TODO: should be greatest common denominator between the two 
     
    9991001 
    10001002    def _bindImp 
    10011003        base._bindImp 
    1002         # TODO: error check that the left hand type is nilable? 
     1004        if not _left.type inherits NilableType   
     1005            .throwError('The left hand type of "?=" is not nilable (it\'s "[_left.type.name]") so the expression will always evaluate to the left hand side.') 
    10031006        # TODO: error check that the right hand type is not nilable? 
    10041007        _type = _right.type 
    10051008 
  • Tests/820-errors/200-expressions/134-non-nilable-coalesce.cobra

     
     1# errors on nil coalesce if lhs non-nilable 
     2class Nilable 
     3    def main is shared 
     4        a = 0 
     5         
     6        b = a ? 99 # .error. The left hand type of "?" is not nilable 
     7        assert  b == 0 
     8        a = 10 
     9        b = a ? 99 # .error. The left hand type of "?" is not nilable 
     10        assert  b == 10 
     11     
     12        a ?= 47 # .error. The left hand type of "?=" is not nilable 
     13        assert b == 10 
     14         
     15        s = 'yy' 
     16        assert s ? 'xxx' == 'yy' # .error. The left hand type of "?" is not nilable 
     17         
     18        s ?= 'xxx' # .error. The left hand type of "?=" is not nilable 
     19        assert s == 'yy' 
     20         
     21         
  • Tests/240-generics/200-nilables/801-qu-op.cobra

     
    1414        return _name ? .getType.name  # used to be a bug 
    1515 
    1616    def foo 
    17         s = (Test('foo') ? Test()).name 
     17        t as Test? = Test('foo') 
     18        s = (t ? Test()).name     
    1819        assert s=='foo' 
     20         
    1921        assert (Test().nilableProperty ? 'aoeu') == 'aoeu' 
     22         
     23        # augmented  assign ?= 
     24        t1 as Test? = Test('foo') 
     25        t1 ?= Test() 
     26        s = t1.name 
     27        assert s=='foo' 
    2028 
    2129    get nilableProperty as String? 
    2230        return nil 
  • 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: Operators ? and ?= give error if lhs expression is not nilable  ticket:117