D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 19203 - alias this to a bool behaves both inconsistently and incorrectly with static assert
Summary: alias this to a bool behaves both inconsistently and incorrectly with static ...
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-08-29 04:11 UTC by Nicholas Wilson
Modified: 2018-10-05 11:34 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Nicholas Wilson 2018-08-29 04:11:23 UTC
struct BoolWithErr {
    bool b;
    string error;
    alias b this;
}

struct Foo {}

template hasPopBack(T) {
    static if (!is(typeof(T.init.popBack)))
        enum hasPopBack = BoolWithErr(false, T.stringof~" does not have popBack");
    else
        enum hasPopBack = BoolWithErr(true,"");
}

int main()
{   
    // Fine:
    auto a = isArrayLike!Foo && isArrayLike!Foo; // false
    enum b = isArrayLike!Foo && isArrayLike!Foo; // false
    pragma(msg, typeof(a)); // bool

    // Fine:
    // Error: static assert: b is false
    static assert(b);

    // Dubious: alias this b should work
    // Error: expression isArrayLike!(Foo) of type BoolWithErr does not have a boolean value
    //   while evaluating: static assert(isArrayLike!(Foo))
    static assert(isArrayLike!Foo);

    // Bad: typeof the expression is bool
    // Error: expression isArrayLike!(Foo) && isArrayLike!Foo of type BoolWithErr does not have a boolean value
    //    while evaluating: static assert(isArrayLike!(Foo) && isArrayLike!Foo)
    static assert(isArrayLike!Foo && isArrayLike!Foo);

    return 0;
}
Comment 1 Nicholas Wilson 2018-08-29 04:23:32 UTC
s/isArrayLike/hasPopBack
Comment 2 github-bugzilla 2018-10-05 11:34:05 UTC
Commits pushed to master at https://github.com/dlang/dmd

https://github.com/dlang/dmd/commit/575458309f4ea897b743081a6a26bd6b7a8eba2e
Fix issue 19203

https://github.com/dlang/dmd/commit/0490ffc24ecb0471302781a8923aa7bf249a9079
Merge pull request #8634 from thewilsonator/staticassertaliasthisbool

Fix issue 19203 - alias this to a bool behaves both inconsistently and incorrectly with static assert
merged-on-behalf-of: Razvan Nitu <RazvanN7@users.noreply.github.com>