[Issue 18199] Error with lambda in struct initializer

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Mar 18 13:04:42 UTC 2018


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

--- Comment #6 from John Belmonte <john at neggie.net> ---
Here is my understanding of the current code's intention.  When presented with
"MyType foo = { ... }", the parser must infer whether the RHS is a struct
initializer or a 0-parameter function literal without knowing anything about
MyType.  A contrived example of the two cases, respectively:


  // RHS is struct initializer (e.g. MyType is struct with a string field)
  static MyType foo = {
    "a" + "b",
  };

  // RHS is function literal (e.g. MyType is alias of void function())
  static MyType bar = {
    "a" + "b";
  };

So the implementation uses the presence of semicolon or return tokens to infer
that the RHS is a function literal.  However that solution yields the wrong
answer when a struct initializer happens to hold certain forms of function
literals.

To fix this the parser may require an isFunctionLiteral() at least handling the
"{ ... }" form.  A caveat with this approach is that a malformed function
literal will be parsed as a struct initializer, yielding confusing error
reports.

--


More information about the Digitalmars-d-bugs mailing list