www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 20628] New: Inherited In contracts should only check the most

https://issues.dlang.org/show_bug.cgi?id=20628

          Issue ID: 20628
           Summary: Inherited In contracts should only check the most
                    inherited function
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: default_357-line yahoo.de

Right now, D does "check in-contract in superclass, if it fails then check
in-contract in subclass." However, in-contracts are defined to only be allowed
to tighten the condition. As such, this behavior makes no sense either within
debug mode or without.

Within debug mode, D should enforce that in contracts widen the conditions. As
such, it should always execute both superclass and subclass contract and Error
if superclass-in passes but subclass-in does not. This will also fix
weirdnesses such as

interface I { void foo(); }
class C : I { void foo() in(this.is.never.compiled) { } }

or

interface I { void foo() in(true); }
class C : I { void foo() in(false) { } }

which would then be a compiletime error or runtime error, respectively.

Without debug mode, it still doesn't make sense to check the superclass
in-contracts. A method is either written correctly or it isn't. If it is
written correctly, its in-contract will include the superclass in-contract by
definition, so there's no need to check it. If it is not written correctly, and
doesn't accept things that it should accept according to the superclass, then
we preferably want to fail with a nice assert-provided error message, not
randomly enter the class body anyways.

What we don't want to see is `void foo(int i) in (i > 5) { assert(i > 5, "this
cannot happen"); } }` fail with "this cannot happen".

--
Mar 03