Skip to content
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

Merged

Conversation

MoonlightSentinel
Copy link
Contributor

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


Blocking dlang/dmd#12142

@dlang-bot
Copy link
Contributor

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 verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

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

Auto-close Bugzilla Severity Description
21570 normal __traits(isStaticArray, ...) accepts enums with static array as base type

Testing this PR locally

If 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)));
Copy link
Member

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
@MoonlightSentinel MoonlightSentinel force-pushed the isAutoDecodableString-enum branch from 66e3aac to 7c13ddf Compare January 21, 2021 20:37
@PetarKirov
Copy link
Member

PetarKirov commented Jan 21, 2021

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:

string		= true	| isAutodecodableString!(S!U) = false
wstring		= true	| isAutodecodableString!(S!U) = false
dstring		= false	| isAutodecodableString!(S!U) = false
char[2]		= false	| isAutodecodableString!(S!U) = false
const(char[2])	= false	| isAutodecodableString!(S!U) = false
const(char[2])	= false	| isAutodecodableString!(S!U) = false
wchar[2]	= false	| isAutodecodableString!(S!U) = false
dchar[2]	= false	| isAutodecodableString!(S!U) = false
E1		= false	| isAutodecodableString!(S!U) = false
E2		= false	| isAutodecodableString!(S!U) = false
E3		= false	| isAutodecodableString!(S!U) = false

https://run.dlang.io/is/7kZkPR

@MoonlightSentinel
Copy link
Contributor Author

That implementation changes the behaviour, e.g. isAutodecodableString!E1 currently yields true.

@PetarKirov
Copy link
Member

That implementation changes the behaviour, e.g. isAutodecodableString!E1 currently yields true.

I didn't read the unittest right below the current implementation 🤦 and I thought that we want to exclude enum and alias this types, sorry for the confusion.

@dlang-bot dlang-bot merged commit ad8e138 into dlang:master Jan 21, 2021
@MoonlightSentinel MoonlightSentinel deleted the isAutoDecodableString-enum branch January 22, 2021 13:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants