[Issue 17435] New: bogus "need 'this'" error with aggregate field passed in alias parameter

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Thu May 25 13:39:37 PDT 2017


https://issues.dlang.org/show_bug.cgi?id=17435

          Issue ID: 17435
           Summary: bogus "need 'this'" error with aggregate field passed
                    in alias parameter
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: ag0aep6g at gmail.com

This should compile:

----
struct S { int field; }
bool ft(alias ignored)() { return true; }
alias f = ft!(S.field); /* accepted */
enum e = f(); /* Error: need 'this' for 'ft' of type 'bool()' */
----

Works when `ft` is marked as `static`, which should be a NOP:

----
struct S { int field; }
static bool ft(alias ignored)() { return true; }
alias f = ft!(S.field);
enum e = f(); /* no error */
----

Curiously, taking a function pointer and calling it also works, but only at run
time and only when done in two steps:

----
struct S { int field; }
bool ft(alias ignored)() { return true; }
alias f = ft!(S.field);

void main()
{
    bool function() fptr = &f; /* accepted */
    assert(fptr()); /* accepted and passes */

    /+ These fail; should all work:
    assert(f()); /* Error: need 'this' for 'ft' of type 'pure nothrow @nogc
@safe bool()' */
    assert((&f)()); /* ditto */
    static bool function() fptr2 = &f; /* Error: non-constant expression & ft
*/
    +/
}
----

Related thread in D.learn:
http://forum.dlang.org/post/cgkwbfspmljbhrmqqmkc@forum.dlang.org

--


More information about the Digitalmars-d-bugs mailing list