Bug 11636 – -unittest flag preserves assert() statements but still allows in/out contracts to be stripped

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-11-28T14:51:13Z
Last change time
2024-12-13T18:14:32Z
Assigned to
No Owner
Creator
Joseph Rushton Wakeling
Moved to GitHub: dmd#18725 →

Attachments

IDFilenameSummaryContent-TypeSize
1296contract.dCode to illustrate the bug: try running with -unittest, -release, and -unittest -release flagstext/x-dsrc281

Comments

Comment #0 by joseph.wakeling — 2013-11-28T14:51:13Z
Created attachment 1296 Code to illustrate the bug: try running with -unittest, -release, and -unittest -release flags Adding the -unittest flag to a build means that any assert() statements in the code will be preserved, even if the -release flag is also used. However, -release will still strip out in/out contracts even if the -unittest flag is used. This can lead to unittest failures when e.g. the unittest checks that an in-contract is respected by looking for an AssertError. The attached code illustrates this: if run with rdmd -unittest contract.d it runs without error, as the call to foo in main does not fail the in-contract assert, and the unittest catches the AssertError. Similarly, if run with rdmd -release contract.d it also runs without error, as the unittests are not called and the in-contract is stripped out, meaning foo will run even with incorrect input. However, when run with rdmd -unittest -release contract.d it will fail with an error: [email protected](27): assertThrown failed: No AssertError was thrown. which triggers because, while asserts are still active, the in-contract has still been stripped, and hence the assert it contains is not called, and an AssertError is not thrown. This inconsistency should surely be fixed -- if -unittest requires assert statements to be present, it should require _all_ assert statements be present, including those in in- and out-contracts.
Comment #1 by joseph.wakeling — 2013-11-28T15:02:56Z
(In reply to comment #0) > This inconsistency should surely be fixed -- if -unittest requires assert > statements to be present, it should require _all_ assert statements be present, > including those in in- and out-contracts. Alternatively, if the point of view is that -unittest -release should effectively test code "as it is with the -release flag active" (i.e. so contracts _should_ be stripped), there needs to be some way of checking if -release has been used, and conditionally selecting different unittests accordingly, e.g.: version(release) assert(/* something appropriate to -release mode */); else assert(/* something else */); ... but code being tested "as it is with the -release flag active" doesn't seem to me to sit well with keeping in-body assert() statements active.
Comment #2 by robert.schadek — 2024-12-13T18:14:32Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18725 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB