D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 8027 - in contract is never checked for overrided functions
Summary: in contract is never checked for overrided functions
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Linux
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-05-03 14:11 UTC by Adam Chrapkowski
Modified: 2012-05-03 14:35 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Adam Chrapkowski 2012-05-03 14:11:30 UTC
import std.stdio;

class Foo {
  int foobar(int a, int b)
  in {
    assert (a > 0 && b > 0);
    writeln("Foo in");
  } out(ret) { 
    assert(ret > 0);
    writeln("Foo out");
  } body {
    return a + b;
  }
}

class Bar : Foo {
  override int foobar(int a, int b)
  in {
    assert(a * b + 8 > 1);
    writeln("Bar in");
  } out(ret) {
    assert (ret > 1);
    writeln("Bar out");
  } body {
    return 2;
  }
}


void main() {
  try {
    auto _foo = new Bar();
    _foo.foobar(1, 2);
  } catch (Exception e) {
    writeln(e);
  }
}


______________________________________________________________________
IN contract for function Bar.foobar() is never checked.
For me it makes sense (becuse in contracts must be checked by the caller which may not know anything about overriding function) but compiler should not allow to define IN contract for an overriding function.
Comment 1 timon.gehr 2012-05-03 14:32:27 UTC

*** This issue has been marked as a duplicate of issue 6856 ***
Comment 2 timon.gehr 2012-05-03 14:35:25 UTC
Oops, actually this is not a duplicate, it is just invalid.
This is how precondition inheritance is supposed to work. (a>0 && b>0 suffices as a condition for the inheriting class to need to accept the input, therefore the additional in-contract is not even checked.)