www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 18666] New: varargs foreach variable can't be assigned to

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

          Issue ID: 18666
           Summary: varargs foreach variable can't be assigned to local
                    variable within foreach loop in  safe code
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: dhasenan gmail.com

Test case:

---
 safe
void void register(string[] names...)
{
    foreach (name; names) auto c = name;
}
---

Expected result: the code should compile (and do nothing).

Actual result: "Error: scope variable name assigned to c with longer lifetime"

In point of fact, `c` has a (marginally) shorter lifetime than `name`, being
introduced later in the same scope, or in a nested scope, depending on how the
compiler arranges things.

This only applies to  safe code. It only applies to typesafe varargs arrays.
Using `scope` rather than `auto` doesn't change anything.

DMD64 D Compiler v2.079.0 on Ubuntu 16.04

--
Mar 26 2018