[Issue 18666] New: varargs foreach variable can't be assigned to local variable within foreach loop in @safe code

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Mar 26 23:07:01 UTC 2018


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 at puremagic.com
          Reporter: dhasenan at 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

--


More information about the Digitalmars-d-bugs mailing list