Bug 18666 – varargs foreach variable can't be assigned to local variable within foreach loop in @safe code
Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2018-03-26T23:07:01Z
Last change time
2023-05-09T14:38:54Z
Keywords
rejects-valid, safe
Assigned to
No Owner
Creator
Neia Neutuladh
Comments
Comment #0 by dhasenan — 2018-03-26T23:07:01Z
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
Comment #1 by razvan.nitu1305 — 2023-05-09T14:38:54Z
This seems to have been fixed. I cannot reproduce.