This compiles correctly:
auto ignoreDlg(void function() dlg) {}
@nogc void foo() { ignoreDlg({ new int(5); }); }
But this gives an incorrect error about the "new" violating @nogc:
auto ignoreDlg(void function() dlg) {}
@nogc: void foo() { ignoreDlg({ new int(5); }); }
Comment #1 by ketmar — 2017-12-20T13:34:43Z
it's not *technically* incorrect: `@nogc:` affects *all* followind definitions, including lambdas. thus, in the second case, we actually have `auto __lambda () @nogc { new int(5); }`, and compiler complains.
Comment #2 by ketmar — 2017-12-20T13:35:28Z
`return new int(5);`, of course, sorry.
Comment #3 by eyal — 2017-12-20T15:39:07Z
It does not affect other kinds of nested scopes.
For example, this does compile:
auto ignoreDlg(void function() dlg) {}
@nogc: struct S { void foo() { ignoreDlg(() { new int(5); }); } }
Comment #4 by ketmar — 2017-12-20T15:47:27Z
yes, `struct` (and `class`, i believe) isn't affected. outer @nogc for *struct* (and class) doesn't change member attrs, only inner @nogc does. this is the logic compiler applies. ;-)
i'm still not sure if `@nogc` should do the same thing as `@nogc:` or not, tho. it prolly should, but let's wait for some compiler dev to clarify the things.
Comment #5 by eyal — 2017-12-20T16:31:09Z
It makes no sense at all for "@nogc" to apply to statements within a statement block of a function.
It would only make sense if it applied recursively to *all* nested scopes.