Bug 23452 – Noncopyable variable can be silently passed to a function with variadic args

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2022-11-03T08:39:17Z
Last change time
2024-12-13T19:25:24Z
Keywords
pull
Assigned to
No Owner
Creator
Tomáš Chaloupka
Moved to GitHub: dmd#20176 →

Comments

Comment #0 by chalucha — 2022-11-03T08:39:17Z
```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.
Comment #1 by johan_forsberg_86 — 2022-11-03T22:49:08Z
Side note, if changed to ```d void test(Foo[] foos) ``` And passed that way, you get the Error: struct `Foo` is not copyable because it has a disabled postblit. So it seems (potentially) limited to vararg.
Comment #2 by dlang-bot — 2022-11-04T13:40:04Z
@RazvanN7 created dlang/dmd pull request #14616 "Fix Issue 23452 - Noncopyable variable can be silently passed to a function with variadic args" fixing this issue: - Fix Issue 23452 - Noncopyable variable can be silently passed to a function with variadic args https://github.com/dlang/dmd/pull/14616
Comment #3 by robert.schadek — 2024-12-13T19:25:24Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/20176 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB