Skip to content

Commit

Permalink
Merge pull request #4833 from rcorre/exists_enum
Browse files Browse the repository at this point in the history
Ensure std.file.exists(StringEnum.name) compiles.
  • Loading branch information
jmdavis committed Nov 5, 2016
2 parents bf2550f + 10cd84a commit 607a755
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
5 changes: 5 additions & 0 deletions std/file.d
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,11 @@ private bool existsImpl(const(FSChar)* namez) @trusted nothrow @nogc
assert(exists(deleteme));
}

@safe unittest // Bugzilla 16573
{
enum S : string { foo = "foo" }
assert(__traits(compiles, S.foo.exists));
}

/++
Returns the attributes of the given file.
Expand Down
25 changes: 21 additions & 4 deletions std/traits.d
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
* $(LREF isFloatingPoint)
* $(LREF isIntegral)
* $(LREF isNarrowString)
* $(LREF isConvertibleToString)
* $(LREF isNumeric)
* $(LREF isPointer)
* $(LREF isScalarType)
Expand Down Expand Up @@ -5330,25 +5331,41 @@ enum bool isNarrowString(T) = (is(T : const char[]) || is(T : const wchar[])) &&
}


/*
Detect whether $(D T) is a struct or static array that is implicitly
convertible to a string.
/**
* Detect whether $(D T) is a struct, static array, or enum that is implicitly
* convertible to a string.
*/
template isConvertibleToString(T)
{
enum isConvertibleToString = (isAggregateType!T || isStaticArray!T) && is(StringTypeOf!T);
enum isConvertibleToString =
(isAggregateType!T || isStaticArray!T || is(T == enum))
&& is(StringTypeOf!T);
}

///
@safe unittest
{
static struct AliasedString
{
string s;
alias s this;
}

enum StringEnum { a = "foo" }

assert(!isConvertibleToString!string);
assert(isConvertibleToString!AliasedString);
assert(isConvertibleToString!StringEnum);
assert(isConvertibleToString!(char[25]));
assert(!isConvertibleToString!(char[]));
}

@safe unittest // Bugzilla 16573
{
enum I : int { foo = 1 }
enum S : string { foo = "foo" }
assert(!isConvertibleToString!I);
assert(isConvertibleToString!S);
}

package template convertToString(T)
Expand Down

0 comments on commit 607a755

Please sign in to comment.