clang-contracts icon indicating copy to clipboard operation
clang-contracts copied to clipboard

Contract inheritance does not work as expected

Open valmat opened this issue 7 years ago • 2 comments

P0542R5 says: If an overriding function specifies contract conditions, it shall specify the same list of contract conditions as its overridden functions; no diagnostic is required if corresponding conditions will always evaluate to the same value. Otherwise, it is considered to have the list of contract conditions from one of its overridden functions

So in folowing example i expect contract violation

struct Base
{
    virtual int foo(int n)
        [[expects: n < 10]]
        [[ensures r: r > 100]]
    {
        return n*n;
    }
};

struct Derived : Base
{
    virtual int foo(int n)  override // inherits contracts from Base
    {
        return n*2;
    }
};

But no contract violations are happening

...
int main()
{
    Derived obj;
    obj1.foo(100);    // <-- expected contract violation

    Derived* pobj1 = new Derived();
    pobj1->foo(100); // <-- expected contract violation

    Base* pobj2 = new Derived();
    pobj2->foo(100); // <-- expected contract violation

    delete pobj1;
    delete pobj2;
    return 0;
}

Note if repeat the contracts from Base to Derived, everything works as expected.

valmat avatar Mar 10 '19 18:03 valmat

As stated in README.md, this is a known issue (unimplemented as of 3/28/2019). Expect this feature before June 2019.

jalopezg-git avatar Mar 28 '19 15:03 jalopezg-git

Anyone got contract inheritance working?

blockspacer avatar Aug 24 '19 07:08 blockspacer