diff --git a/src/dmd/semantic3.d b/src/dmd/semantic3.d index 1b7fa139014f..3e594aa341ae 100644 --- a/src/dmd/semantic3.d +++ b/src/dmd/semantic3.d @@ -456,8 +456,14 @@ private extern(C++) final class Semantic3Visitor : Visitor stc |= STC.scope_; } } - if (funcdecl.flags & FUNCFLAG.inferScope && !(fparam.storageClass & STC.scope_)) + + // infer scope for lambdas even without -preview=dip1000 + // See https://issues.dlang.org/show_bug.cgi?id=20362 + const isLambda = funcdecl.isFuncLiteralDeclaration; + + if ((funcdecl.flags & FUNCFLAG.inferScope || isLambda) && !(fparam.storageClass & STC.scope_)) stc |= STC.maybescope; + stc |= fparam.storageClass & (STC.in_ | STC.out_ | STC.ref_ | STC.return_ | STC.scope_ | STC.lazy_ | STC.final_ | STC.TYPECTOR | STC.nodtor); v.storage_class = stc; v.dsymbolSemantic(sc2); diff --git a/test/compilable/issue20362.d b/test/compilable/issue20362.d new file mode 100644 index 000000000000..b24bb8da3541 --- /dev/null +++ b/test/compilable/issue20362.d @@ -0,0 +1,8 @@ +void main() { + string str; + stringify((chars) {str ~= chars; }); +} + +void stringify(scope void delegate(scope const char[]) sink) { + sink("oops"); +}