www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 17435] New: bogus "need 'this'" error with aggregate field

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 puremagic.com
          Reporter: ag0aep6g 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

--
May 25 2017