Thread overview
How to pass noncopyable variadic arguments with ref?
Oct 20, 2022
tchaloupka
Oct 20, 2022
Imperatorn
Oct 20, 2022
user1234
Oct 20, 2022
user1234
Oct 21, 2022
ryuukk_
Nov 03, 2022
tchaloupka
October 20, 2022

Hi,
I've found strange behavior where:

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);
}

Compiles fine (no error on passing noncopyable arguments to the function), but there are other objects passed to the function as they aren't cleared out in the caller scope.

Shouldn't it at least protest that objects can't be passed to the function as they aren't copyable?

October 20, 2022

On Thursday, 20 October 2022 at 14:03:10 UTC, tchaloupka wrote:

>

Hi,
I've found strange behavior where:

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);
}

Compiles fine (no error on passing noncopyable arguments to the function), but there are other objects passed to the function as they aren't cleared out in the caller scope.

Shouldn't it at least protest that objects can't be passed to the function as they aren't copyable?

Have you looked at the ast?

October 20, 2022

On Thursday, 20 October 2022 at 14:03:10 UTC, tchaloupka wrote:

>

Hi,
I've found strange behavior where:
[...]
Shouldn't it at least protest that objects can't be passed to the function as they aren't copyable?

it's clearly a compiler bug to me. Something is not checked when the call is verified.

October 20, 2022

On Thursday, 20 October 2022 at 16:34:34 UTC, user1234 wrote:

>

On Thursday, 20 October 2022 at 14:03:10 UTC, tchaloupka wrote:

>

Hi,
I've found strange behavior where:
[...]
Shouldn't it at least protest that objects can't be passed to the function as they aren't copyable?

it's clearly a compiler bug to me. Something is not checked when the call is verified.

however (forgot to say) this form of variadic was proposed for deprecation.
So maybe the bug is more an argument to drop them.

October 21, 2022

On Thursday, 20 October 2022 at 14:03:10 UTC, tchaloupka wrote:

>

Hi,
I've found strange behavior where:

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);
}

Compiles fine (no error on passing noncopyable arguments to the function), but there are other objects passed to the function as they aren't cleared out in the caller scope.

Shouldn't it at least protest that objects can't be passed to the function as they aren't copyable?

void test(Foo..)(Foo foos)

I don't know if that's the 1:1 alternative, but that doesn't compile

onlineapp.d(23): Error: struct `onlineapp.Foo` is not copyable because it has a disabled postblit
November 03, 2022

On Friday, 21 October 2022 at 12:05:28 UTC, ryuukk_ wrote:

>

On Thursday, 20 October 2022 at 14:03:10 UTC, tchaloupka wrote:

void test(Foo..)(Foo foos)

I don't know if that's the 1:1 alternative, but that doesn't compile

onlineapp.d(23): Error: struct `onlineapp.Foo` is not copyable because it has a disabled postblit

Yeah, I've ended up with this kind of workaround too.
Posted a bug report: https://issues.dlang.org/show_bug.cgi?id=23452