http://d.puremagic.com/issues/show_bug.cgi?id=5212
Summary: Safer typesafe variadics Product: D Version: D2 Platform: All OS/Version: All Status: NEW Severity: enhancement Priority: P2 Component: DMD AssignedTo: nob...@puremagic.com ReportedBy: bearophile_h...@eml.cc --- Comment #0 from bearophile_h...@eml.cc 2010-11-13 10:13:23 PST --- With DMD 2.050 this D2 program usually asserts at run-time: class Foo { int[] args; this(int[] args_...) { args = args_; } } Foo foo() { return new Foo(1, 2, 3); // passes stack data to Foo } void main() { assert(foo().args == [1, 2, 3]); } That's the root cause of a bug found by a person in the D.learn group: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=22697 Just adding a dup inside the this() avoids the bug. Also, the bug is avoided if Foo receives arguments like this: Foo foo() { return new Foo([1, 2, 3]); } So this nature of typesafe variadic arguments is bug-prone. There are various ways to avoid this problem. One possible solution is to modify the semantics of this kind of arguments pass, so the code inside the function foo always see an args array allocated on the heap (it may perform a run-time test, and dup the given arguments only if the data is on the stack, so in the second example it doesn't dup and saves a little of RAM and running time). To restore the original efficient semantics of the typesafe variadics a "scope" attribute may be added (as the one used to avoid a closure): class Foo { int[] args; this(scope int[] args_...) { // unsafe but fast args = args_; } } This is safer than the current semantics because, as usual in D design, the safe way is the built-in one and the faster one is on request. This is just one of the possible ways to avoid this problem. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------