Skip to content

Commit

Permalink
Merge pull request #6494 from BBasile/issue-13741
Browse files Browse the repository at this point in the history
fix issue 13741 - std.traits.moduleName does not work with functions that have parameters
merged-on-behalf-of: MetaLang <MetaLang@users.noreply.github.com>
  • Loading branch information
dlang-bot committed May 19, 2018
2 parents b62baca + 593a4e2 commit 09816a2
Showing 1 changed file with 40 additions and 5 deletions.
45 changes: 40 additions & 5 deletions std/traits.d
Expand Up @@ -489,12 +489,14 @@ template packageName(alias T)
{
import std.algorithm.searching : startsWith;

enum bool isNotFunc = !isSomeFunction!(T);

static if (__traits(compiles, parentOf!T))
enum parent = packageName!(parentOf!T);
else
enum string parent = null;

static if (T.stringof.startsWith("package "))
static if (isNotFunc && T.stringof.startsWith("package "))
enum packageName = (parent.length ? parent ~ '.' : "") ~ T.stringof[8 .. $];
else static if (parent)
enum packageName = parent;
Expand All @@ -512,8 +514,7 @@ template packageName(alias T)
{
import std.array;

// Commented out because of dmd @@@BUG8922@@@
// static assert(packageName!std == "std"); // this package (currently: "std.std")
static assert(packageName!std == "std");
static assert(packageName!(std.traits) == "std"); // this module
static assert(packageName!packageName == "std"); // symbol in this module
static assert(packageName!(std.array) == "std"); // other module from same package
Expand All @@ -535,16 +536,38 @@ version (none) @safe unittest //Please uncomment me when changing packageName to
static assert(packageName!Barrier == "core.sync");
}

///
@safe unittest
{
static assert(packageName!moduleName == "std");
}

@safe unittest // issue 13741
{
import std.ascii : isWhite;
static assert(packageName!(isWhite) == "std");

struct Foo{void opCall(int){}}
static assert(packageName!(Foo.opCall) == "std");

@property void function(int) vf;
static assert(packageName!(vf) == "std");
}

/**
* Get the module name (including package) for the given symbol.
*/
template moduleName(alias T)
{
import std.algorithm.searching : startsWith;

static assert(!T.stringof.startsWith("package "), "cannot get the module name for a package");
enum bool isNotFunc = !isSomeFunction!(T);

static if (T.stringof.startsWith("module "))
static if (isNotFunc)
static assert(!T.stringof.startsWith("package "),
"cannot get the module name for a package");

static if (isNotFunc && T.stringof.startsWith("module "))
{
static if (__traits(compiles, packageName!T))
enum packagePrefix = packageName!T ~ '.';
Expand Down Expand Up @@ -582,6 +605,18 @@ template moduleName(alias T)
static assert(moduleName!(X12287!int.i) == "std.traits");
}

@safe unittest // issue 13741
{
import std.ascii : isWhite;
static assert(moduleName!(isWhite) == "std.ascii");

struct Foo{void opCall(int){}}
static assert(moduleName!(Foo.opCall) == "std.traits");

@property void function(int) vf;
static assert(moduleName!(vf) == "std.traits");
}

version (none) @safe unittest //Please uncomment me when changing moduleName to test global imports
{
import core.sync.barrier; // global import
Expand Down

0 comments on commit 09816a2

Please sign in to comment.