D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3326 - $ cannot be used in delegate literals
Summary: $ cannot be used in delegate literals
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other All
: P4 minor
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2009-09-17 07:46 UTC by Kasumi Hanazuki
Modified: 2022-12-19 15:02 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Kasumi Hanazuki 2009-09-17 07:46:16 UTC
Windows DMD 2.032

Evaluating $ in a delegate literal in an array indexing or slicing
throws access violation Error.

----

void main() {
    int[] a = [1];
    
    auto x = a[{ auto n = $; return 0; }()];
        // object.Error: Access Violation
    
    auto y = a[{ auto n = $; return 0; }() .. $];
        // object.Error: Access Violation
}
Comment 1 Don 2010-07-23 00:05:49 UTC
This worked in 2.000 and all versions of D1, Failed with access violation in 2.012. Bumping up to regression.
Comment 2 Don 2010-08-06 00:11:01 UTC
This isn't a regression, it never worked properly (on D1, it doesn't segfault, but the generated code is wrong).
It's happening because __dollar isn't a real variable in the parent scope. I think to fix this properly, either the implementation of __dollar would need to change, OR the delegate literal would need to be inlined; but this case is so obscure, it doesn't matter much.

To turn this from wrong-code into a rather obscure rejects-valid:
declaration.c, VarDeclaration::checkNestedReference(), line 1441 (D2 svn 599):

            fdv->closureVars.push(this);
          L2: ;
+            // __dollar creates problems because it isn't a real variable
+            if (ident == Id::dollar)
+                ::error(loc, "cannnot use $ inside a function literal (Bugzilla 3326)");
Comment 3 Walter Bright 2010-08-08 22:30:46 UTC
http://www.dsource.org/projects/dmd/changeset/608
Comment 4 yebblies 2011-07-01 22:11:16 UTC
Reopening as this hasn't been fixed, just turned into a rejects-valid
Comment 5 RazvanN 2022-09-08 07:31:43 UTC
The code compiles and runs successfully today.
Comment 6 RazvanN 2022-09-08 07:37:01 UTC
Actually, I wrongfully compiled. This is still reproducible.
Comment 7 RazvanN 2022-12-19 15:02:39 UTC
This has been fixed. At most this could be seen as an enhancement. However, the use case is really narrow.