Skip to content

Commit

Permalink
Fix issue 18111: use fully qualified name for modules in unittest names
Browse files Browse the repository at this point in the history
  • Loading branch information
atilaneves authored and wilzbach committed Jan 5, 2018
1 parent 72f76d0 commit 3b551d3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
16 changes: 16 additions & 0 deletions src/dmd/dmodule.d
Expand Up @@ -1318,6 +1318,22 @@ extern (C++) final class Module : Package
{
v.visit(this);
}

/***********************************************
* Writes this module's fully-qualified name to buf
* Params:
* buf = The buffer to write to
*/
void fullyQualifiedName(ref OutBuffer buf)
{
buf.writestring(ident.toString());

for (auto package_ = parent; package_ !is null; package_ = package_.parent)
{
buf.prependstring(".");
buf.prependstring(package_.ident.toChars());
}
}
}

/***********************************************************
Expand Down
25 changes: 23 additions & 2 deletions src/dmd/func.d
Expand Up @@ -3468,8 +3468,10 @@ extern (C++) final class UnitTestDeclaration : FuncDeclaration
private static Identifier createIdentifier(Loc loc, Scope* sc)
{
OutBuffer buf;
auto index = sc ? sc._module.unitTestCounter++ : 0;
buf.printf("__unittest_%s_%u_%d", loc.filename, loc.linnum, index);
writeModuleNameOrFileName(buf, loc, sc);
buf.prependstring("__unittest_");
const index = sc ? sc._module.unitTestCounter++ : 0;
buf.printf("_%u_%d", loc.linnum, index);

// replace characters that demangle can't handle
auto str = buf.peekString;
Expand All @@ -3479,6 +3481,25 @@ extern (C++) final class UnitTestDeclaration : FuncDeclaration
return Identifier.idPool(buf.peekSlice());
}

/*************************************************************************
* Writes a module name to name a unittest. Tries to use the fully
* qualified name if possible to avoid mismatches when compiling separately.
* Otherwise uses the file name.
* Params:
* buf = The buffer to write to.
* loc = The location of the unit test declaration.
* scope = The scope of the unit test declaration.
*/
private static void writeModuleNameOrFileName(ref OutBuffer buf, Loc loc, Scope* scope_)
{
if (scope_ is null || scope_._module is null || scope_._module.ident is null)
{
buf.writestring(loc.filename);
return;
}
scope_._module.fullyQualifiedName(buf);
}

override AggregateDeclaration isThis()
{
return null;
Expand Down
8 changes: 4 additions & 4 deletions test/fail_compilation/fail7848.d
Expand Up @@ -3,11 +3,11 @@
/*
TEST_OUTPUT:
---
fail_compilation/fail7848.d(35): Error: pure function 'fail7848.C.__unittest_fail_compilation_fail7848_d_33_0' cannot call impure function 'fail7848.func'
fail_compilation/fail7848.d(35): Error: @safe function 'fail7848.C.__unittest_fail_compilation_fail7848_d_33_0' cannot call @system function 'fail7848.func'
fail_compilation/fail7848.d(35): Error: @nogc function 'fail7848.C.__unittest_fail_compilation_fail7848_d_33_0' cannot call non-@nogc function 'fail7848.func'
fail_compilation/fail7848.d(35): Error: pure function 'fail7848.C.__unittest_fail7848_33_0' cannot call impure function 'fail7848.func'
fail_compilation/fail7848.d(35): Error: @safe function 'fail7848.C.__unittest_fail7848_33_0' cannot call @system function 'fail7848.func'
fail_compilation/fail7848.d(35): Error: @nogc function 'fail7848.C.__unittest_fail7848_33_0' cannot call non-@nogc function 'fail7848.func'
fail_compilation/fail7848.d(35): Error: function `fail7848.func` is not nothrow
fail_compilation/fail7848.d(33): Error: nothrow function `fail7848.C.__unittest_fail_compilation_fail7848_d_33_0` may throw
fail_compilation/fail7848.d(33): Error: nothrow function `fail7848.C.__unittest_fail7848_33_0` may throw
fail_compilation/fail7848.d(40): Error: pure function 'fail7848.C.__invariant1' cannot call impure function 'fail7848.func'
fail_compilation/fail7848.d(40): Error: @safe function 'fail7848.C.__invariant1' cannot call @system function 'fail7848.func'
fail_compilation/fail7848.d(40): Error: @nogc function 'fail7848.C.__invariant1' cannot call non-@nogc function 'fail7848.func'
Expand Down
2 changes: 1 addition & 1 deletion test/fail_compilation/ice14424.d
Expand Up @@ -2,7 +2,7 @@
/*
TEST_OUTPUT:
---
fail_compilation/ice14424.d(12): Error: `tuple(__unittest_fail_compilation_imports_a14424_d_3_0)` has no effect
fail_compilation/ice14424.d(12): Error: `tuple(__unittest_imports_a14424_3_0)` has no effect
---
*/

Expand Down

0 comments on commit 3b551d3

Please sign in to comment.