Releation between parameter and result note proveable by static checker
hi,
following code:
class1.vb(13,13): warning : contracts: assert unproven
class1.vb(21,9): warning : contracts: assert unproven
since have each on base array (var comp), counter valid entries (max) can't greater length of new array (ret) since must smaler or eqal base array.
miss contracts, in order static checker able prove ?
following code:
imports system.diagnostics.contracts
public class class1
public shared function removeempty(byval comp string()) string()
contract.requires(comp isnot nothing)
contract.ensures(contract.result(of string())() isnot nothing)
contract.ensures(contract.result(of string()).length <= comp.length)
dim ret string()
dim max integer
max = 0
redim ret(comp.length - 1)
each str string in comp
contract.assert(ret.length > max)
ret(max) = str
if not string.isnullorempty(str) then
max += 1
end if
next
contract.assert(max <= comp.length)
redim preserve ret(max - 1)
contract.assert(ret.length <= comp.length)
contract.assert(ret isnot nothing)
contract.assert(ret.length <= comp.length)
return ret
end function
end class
generates warning:class1.vb(13,13): warning : contracts: assert unproven
class1.vb(21,9): warning : contracts: assert unproven
since have each on base array (var comp), counter valid entries (max) can't greater length of new array (ret) since must smaler or eqal base array.
miss contracts, in order static checker able prove ?
it seems checker failing infer loop invariant "max <= ", iteration variable vb compiler introduces in compilation.
i try fix problem inference.
meanwhile, 1 way fix code above use loop (so iteration variable explicit) , add assertion contract.assert(max <= i)
thanks,
f
i try fix problem inference.
meanwhile, 1 way fix code above use loop (so iteration variable explicit) , add assertion contract.assert(max <= i)
thanks,
f
DevLabs > Code Contracts
Comments
Post a Comment