Issue 6549 - Implement contracts without implementation.
Summary: Implement contracts without implementation.
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords: bootcamp, contracts, pull
Depends on:
Blocks: 12344
  Show dependency treegraph
 
Reported: 2011-08-24 11:23 UTC by timon.gehr
Modified: 2021-01-07 12:35 UTC (History)
9 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description timon.gehr 2011-08-24 11:23:34 UTC
currently, code like the following is rejected:

abstract class C{
    int foo(int x)
      in{assert(x<0);}
      out(result){assert(result>0);}
}

tt.d(10): Error: function tt.C.foo in and out contracts require function body.

Which does not make sense, because contracts logically belong to the method declaration.

Such code should be accepted and the contracts should be subject to contract inheritance. This will require a tiny grammar change.
Comment 1 Shahid 2012-01-28 03:46:36 UTC
I would like a clarification about abstract class/methods with contracts.
Should the bottom two examples be valid D code?

----
// Works ( and documented TDPL/website )
interface I {
        int func( int x )
        in { assert(x == 0); }
}
class C : I {
        int func( int x ) { return x; }
}

// Does not work ( undocumented )
abstract class A
{
        int func( int x )
        in { assert(x == 0); }
}
// ditto
class A2 {
        abstract int func( int x )
        in { assert(x == 0); }
}
Comment 2 timon.gehr 2012-01-28 07:46:14 UTC
Yes, that is the plan.
Comment 3 Alex Rønne Petersen 2012-02-12 09:17:40 UTC
Is there any news on this? Design-by-contract in D is seriously crippled due to this bug.
Comment 4 Stewart Gordon 2012-02-12 10:07:15 UTC
(In reply to comment #3)
> Is there any news on this? Design-by-contract in D is seriously crippled due to
> this bug.

It's an arbitrary restriction, not a bug.

http://www.dlang.org/declaration.html
Decl:
    StorageClasses Decl
    BasicType Declarators ;
    BasicType Declarator FunctionBody
    AutoDeclaration

http://www.dlang.org/function.html
FunctionBody:
    BlockStatement
    BodyStatement
    InStatement BodyStatement
    OutStatement BodyStatement
    InStatement OutStatement BodyStatement
    OutStatement InStatement BodyStatement

InStatement and OutStatement are part of FunctionBody, so if the function has no body then it can't have in and out contracts.

But I entirely agree that it should be allowed.  Contracts are part of the API, not the implementation.  As such, they are equally applicable to abstract/interface methods.  It could also improve contract checking in closed-source libraries.
Comment 5 Stewart Gordon 2012-02-29 06:05:03 UTC
Possible grammar rewrite:

Decl:
    StorageClasses Decl
    BasicType Declarators ;
    BasicType Declarator BlockStatement
    BasicType Declarator BodyStatement
    BasicType Declarator Contracts ;
    BasicType Declarator Contracts BodyStatement
    AutoDeclaration

Contracts:
    InStatement
    OutStatement
    InStatement OutStatement
    OutStatement InStatement
Comment 7 Adam D. Ruppe 2014-03-10 20:43:44 UTC
amen, this should DEFINITELY work. without it, we can't have contracts in .di generation nor in interfaces, making them really hard to use as intended.

This is a bigger problem to me than the lack of preconditions. Ugh.
Comment 8 Adam D. Ruppe 2014-03-10 20:46:56 UTC
well I guess we *can* have interfaces now, but not abstract classes... random limitation indeed.
Comment 9 FeepingCreature 2017-11-30 09:29:03 UTC
Seconding this issue.
Comment 10 github-bugzilla 2017-12-28 22:00:21 UTC
Commits pushed to master at https://github.com/dlang/dmd

https://github.com/dlang/dmd/commit/c7d0672b7faa21ead0edd8924c2a719aca94fc21
fix issue 6549 - Implement contracts without implementation

https://github.com/dlang/dmd/commit/d511b9579559d7f3109a7a6aef05b80786612f49
Merge pull request #7527 from ibuclaw/issue6549

fix issue 6549 - Implement contracts without implementation
Comment 11 Dlang Bot 2021-01-07 12:35:25 UTC
dlang/dmd pull request #12106 "[dmd-cxx] Implement new syntax for contracts and invariants" was merged into dmd-cxx:

- edaf335e4d3c7f9100aed809dca584aa8da2d713 by Iain Buclaw:
  [dmd-cxx] fix issue 6549 - Implement contracts without implementation

https://github.com/dlang/dmd/pull/12106