Wiki
Show
Ignore:
Timestamp:
03/15/10 05:32:06 (2 years ago)
Author:
Chuck.Esterbrook
Message:

Fixed: References to local variables in ensure are not flagged as errors.
ticket:198

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • cobra/trunk/Source/Members.cobra

    r2310 r2319  
    16651665    def _bindImp is override 
    16661666        base._bindImp 
     1667        # note: expressions cannot refer to locals in contracts 
    16671668        newExprs = List<of Expr>() 
    1668         i = 0 
    16691669        for expr in _exprs 
    16701670            expr.bindImp  # TODO: error recovery 
    16711671            assert expr.type 
     1672            if not expr.hasError, _checkForLocalReferences(expr) 
    16721673            if expr.type is not .compiler.boolType 
    16731674                expr = TruthExpr(expr).bindAll to Expr # CC: axe cast when Cobra supports "as this" 
    16741675                assert expr.type 
    16751676            newExprs.add(expr) 
    1676             i += 1 
    16771677        _exprs.clear 
    16781678        _exprs.addRange(newExprs) 
    16791679 
     1680    def _checkForLocalReferences(expr as Expr) 
     1681        for expr in expr.allExprs 
     1682            if expr inherits IdentifierExpr 
     1683                if expr.definition inherits LocalVar 
     1684                    if expr.name <> 'result' or not expr.definition.isImplicit 
     1685                        expr.recordError('Cannot refer to local variable "[expr.name]" in a contract. Use an "assert" statement in the implementation to verify conditions on locals.') 
     1686     
    16801687    def checkConnectToken 
    16811688        """ 
     
    17221729 
    17231730     
    1724 class EnsurePart 
    1725     is partial 
    1726     inherits ContractPart 
     1731class EnsurePart inherits ContractPart is partial 
    17271732 
    17281733    cue init(codeMember as AbstractMethod) 
     
    17471752        return have 
    17481753 
    1749     def _bindImp is override 
    1750         if _codeMember.resultType is .compiler.voidType 
     1754    def _bindImp 
     1755        codeMember = _codeMember 
     1756        if codeMember.resultType is .compiler.voidType 
    17511757            base._bindImp 
    17521758        else 
    1753             resultLocal = _codeMember.findLocal('result') 
     1759            resultLocal = codeMember.findLocal('result') 
    17541760            resultBuiltIn = ResultVar(.token, _codeMember) 
    1755             if resultLocal 
    1756                 _codeMember.replaceLocal(resultBuiltIn) 
    1757             else 
    1758                 _codeMember.addLocal(resultBuiltIn) 
     1761            if resultLocal, codeMember.replaceLocal(resultBuiltIn) 
     1762            else, codeMember.addLocal(resultBuiltIn) 
    17591763            try 
    17601764                base._bindImp 
    17611765            finally 
    1762                 if resultLocal 
    1763                     _codeMember.replaceLocal(resultLocal to LocalVar) 
    1764                 else 
    1765                     _codeMember.removeLocal('result') 
     1766                if resultLocal, codeMember.replaceLocal(resultLocal to LocalVar) 
     1767                else, codeMember.removeLocal('result')