D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5092 - pure nothrow should be ignored for unit tests
Summary: pure nothrow should be ignored for unit tests
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Windows
: P2 enhancement
Assignee: No Owner
URL:
Keywords: patch
Depends on:
Blocks:
 
Reported: 2010-10-21 06:50 UTC by Don
Modified: 2010-11-03 06:36 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Don 2010-10-21 06:50:18 UTC
import std.stdio;

pure nothrow:

int foo(int z) { return z*2; }

unittest {	
    writeln("testing foo");
    assert(foo(4) == 8);
}

---
This won't compile, because the unit test calls writeln which is impure and may throw. 

It makes no sense for a unittest to be nothrow. And it's really a nuisance.

And if a unittest isn't conceptually pure, you have a big problem anyway -- the program behaviour will change depending on whether unittests are run, or not.

PATCH: func.c, around line 3460

void UnitTestDeclaration::semantic(Scope *sc)
{
    if (global.params.useUnitTests)
    {
        if (!type)
            type = new TypeFunction(NULL, Type::tvoid, FALSE, LINKd);
        Scope *sc2 = sc->push();
+        // It makes no sense for unit tests to be pure or nothrow.
+        sc2->stc &= ~(STCnothrow | STCpure);
        sc2->linkage = LINKd;
        FuncDeclaration::semantic(sc2);
        sc2->pop();
    }
Comment 1 Don 2010-11-03 06:36:38 UTC
Fixed svn 736.