Bug 5092 – pure nothrow should be ignored for unit tests
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2010-10-21T06:50:00Z
Last change time
2010-11-03T06:36:38Z
Keywords
patch
Assigned to
nobody
Creator
clugdbug
Comments
Comment #0 by clugdbug — 2010-10-21T06:50:18Z
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();
}