www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 23452] New: Noncopyable variable can be silently passed to a

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

          Issue ID: 23452
           Summary: Noncopyable variable can be silently passed to a
                    function with variadic args
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: chalucha gmail.com

```D
import std.stdio;

struct Foo {
     disable this(this);
    int x;
}

void test(Foo[] foos...) {
    foreach (ref f; foos) {
        writeln(&f, ": ", f.x);
        f.x = 0;
    }
}

void main()
{
    Foo f1 = Foo(1);
    Foo f2 = Foo(2);
    writeln("f1: ", &f1);
    writeln("f2: ", &f2);
    test(f1, f2);
    writeln("f1: ", f1.x);
    writeln("f2: ", f2.x);
}
```

This can compile without any warning or error but at the same time foos aren't
passed as a reference.
At least it shouldn't allow to pass the noncopyable variables.

For a record a possible workaround that works:
```D
void test(ARGS...)(ARGS foos)
  if (ARGS.length >= 1 && allSameType!ARGS && is(ARGS[0] == Foo))
{ ... }
```

This would rightly complain that Foo is not copyable and with `(ref ARGS foos)`
we can also pass foos by reference when needed.

--
Nov 03 2022