D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 9482 - Option to compile in release mode, but keep in contracts on non-private functions
Summary: Option to compile in release mode, but keep in contracts on non-private funct...
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords: contracts
Depends on:
Blocks:
 
Reported: 2013-02-08 04:50 UTC by Stewart Gordon
Modified: 2020-06-13 22:40 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 Stewart Gordon 2013-02-08 04:50:21 UTC
At the moment, development vs. release mode is a binary switch.

Development build (no -release):
- asserts are compiled in
- in/out contracts are compiled in
- invariants are compiled in
- array bounds checking

Release build (-release):
- asserts are ignored
- in/out contracts are ignored
- invariants are ignored
- no array bounds checking

This all-or-nothing choice is unideal.  In particular, for compiling libraries, it would be useful to still have the in contracts and the asserts therein, but otherwise have a release build.  This means that the tested and released library code will perform at (almost) full speed and have minimal code bloat, but users of the library still get the checks that they are passing valid input to the library's functions.

Of course, there's no point having the in contracts on private functions, since these will be called only from within the library.  The same applies to package.  But for public and protected functions, the in contract would be generated in this mode.

This would become a non-issue if we:
- move in contract checking to the caller side, as has been suggested on a number of occasions
- in some way make sure the in contract can be compiled with code that uses the library, if it isn't present in the .lib
- allow contracts to be specified without an implementation (issue 6549)

But since these changes don't seem to be forthcoming, the improvement I am suggesting here might be worth implementing at this time.
Comment 1 basile-z 2020-06-13 17:53:19 UTC
https://dlang.org/dmd-linux.html#switch-check
Comment 2 Stewart Gordon 2020-06-13 21:55:04 UTC
At first I thought this was a Linux-specific feature - but I see now it's in the Windows, OSX (which is called macOS now) and FreeBSD versions alike.  Any idea when this feature was added?  Furthermore, does this switch on in contracts for all functions, or does it exclude private functions?
Comment 3 basile-z 2020-06-13 22:40:27 UTC
(In reply to Stewart Gordon from comment #2)
> At first I thought this was a Linux-specific feature - but I see now it's in
> the Windows, OSX (which is called macOS now) and FreeBSD versions alike. 
> Any idea when this feature was added?  Furthermore, does this switch on in
> contracts for all functions, or does it exclude private functions?

it was added in version 2.084:

https://dlang.org/changelog/2.084.0.html#check-switch

Sorry I missed the "private" criterion... maybe reopen if the check feature is not sufficient.