-
-
Notifications
You must be signed in to change notification settings - Fork 706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make isAutoDecodableString independent of issue 21570 #7760
Make isAutoDecodableString independent of issue 21570 #7760
Conversation
|
Thanks for your pull request and interest in making D better, @MoonlightSentinel! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please see CONTRIBUTING.md for more information. If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment. Bugzilla references
Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub run digger -- build "master + phobos#7760" |
std/traits.d
Outdated
| @@ -6770,7 +6770,8 @@ template isAutodecodableString(T) | |||
| import std.range.primitives : autodecodeStrings; | |||
|
|
|||
| enum isAutodecodableString = autodecodeStrings && | |||
| (is(T : const char[]) || is(T : const wchar[])) && !isStaticArray!T; | |||
| ((is(T : const char[]) && !is(T : const char[n], size_t n)) || | |||
| (is(T : const wchar[]) && !is(T : const char[n], size_t n))); | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should work:
(is(T : const char[]) || is(T : const wchar[])) && !is(T : U[n], size_t n);The current implementation relies on issue 21570 to reject enums with static arrays as their base type. Use another is-expression instead of `isStaticArray` to detect types that are (convertible to) static arrays. See https://issues.dlang.org/show_bug.cgi?id=21570
66e3aac to
7c13ddf
Compare
|
After some testing, I think this is the cleanest solution: template isAutodecodableString(T)
{
import std.range.primitives : autodecodeStrings;
enum isAutodecodableString = autodecodeStrings &&
is(immutable T == immutable char[]) ||
is(immutable T == immutable wchar[]);
}
unittest
{
enum E1 : string { s = "asd" }
enum E2 : const(char[3]) { s = "asd" }
enum E3 : wstring { s = "asd" }
static struct S(T)
{
T get() { return T.init; }
alias get this;
}
import std.meta : AliasSeq;
static foreach (i, T; AliasSeq!(
string[true], wstring[true], dstring[false],
char[2][false], const char[2][false], const(char)[2][false], wchar[2][false], dchar[2][false],
E1[false], E2[false], E3[false],
))
{{
static if (is(T == U[expected], U, size_t expected))
{{
pragma(msg, U, i > 3 && i < 8 ? "\t= " : "\t\t= ", isAutodecodableString!U, "\t| isAutodecodableString!(S!U) = ", isAutodecodableString!(S!U));
static assert(isAutodecodableString!U == expected);
static assert(!isAutodecodableString!(S!U));
}}
else
static assert(0);
}}
}Output: |
|
That implementation changes the behaviour, e.g. |
I didn't read the unittest right below the current implementation 🤦 and I thought that we want to exclude |
The current implementation relies on issue 21570 to reject enums with
static arrays as their base type.
Use another is-expression instead of
isStaticArrayto detect typesthat are (convertible to) static arrays.
See https://issues.dlang.org/show_bug.cgi?id=21570
Blocking dlang/dmd#12142