D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 9489 - writeln of struct with disabled copy ctor
Summary: writeln of struct with disabled copy ctor
Status: REOPENED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86 Windows
: P3 normal
Assignee: No Owner
URL:
Keywords: rejects-valid
: 20632 (view as issue list)
Depends on:
Blocks:
 
Reported: 2013-02-09 04:52 UTC by bearophile_hugs
Modified: 2024-12-01 16:16 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2013-02-09 04:52:58 UTC
import std.stdio: writeln;
import std.conv: text;
import std.typecons: scoped;
class Foo {
    int x;
    this(int x_) { this.x = x_; }
    override string toString() { return text(x); }
}
void main() {
    auto f = scoped!Foo(100);
    writeln(f.x); // OK
    writeln(f); // Error
    typeof(scoped!Foo(1))[10] foos;
    foreach (i, ref fi; foos)
        fi.x = i * 10;
    writeln(foos); // Error
}



dmd 2.062beta gives:


...\dmd2\src\phobos\std\stdio.d(709): Error: struct std.typecons.scoped!(Foo, int).scoped.Scoped!(Foo).Scoped is not copyable because it is annotated with @disable
...\dmd2\src\phobos\std\format.d(414): Error: struct std.typecons.scoped!(Foo, int).scoped.Scoped!(Foo).Scoped is not copyable because it is annotated with @disable
...\dmd2\src\phobos\std\format.d(437): Error: struct std.typecons.scoped!(Foo, int).scoped.Scoped!(Foo).Scoped is not copyable because it is annotated with @disable
...\dmd2\src\phobos\std\format.d(451): Error: struct std.typecons.scoped!(Foo, int).scoped.Scoped!(Foo).Scoped is not copyable because it is annotated with @disable
...\dmd2\src\phobos\std\format.d(463): Error: struct std.typecons.scoped!(Foo, int).scoped.Scoped!(Foo).Scoped is not copyable because it is annotated with @disable
...\dmd2\src\phobos\std\format.d(475): Error: struct std.typecons.scoped!(Foo, int).scoped.Scoped!(Foo).Scoped is not copyable because it is annotated with @disable
...\dmd2\src\phobos\std\format.d(489): Error: struct std.typecons.scoped!(Foo, int).scoped.Scoped!(Foo).Scoped is not copyable because it is annotated with @disable
...\dmd2\src\phobos\std\format.d(498): Error: struct std.typecons.scoped!(Foo, int).scoped.Scoped!(Foo).Scoped is not copyable because it is annotated with @disable
...\dmd2\src\phobos\std\stdio.d(714): Error: template instance std.format.formattedWrite!(LockingTextWriter, char, Scoped!(Foo)) error instantiating
...\dmd2\src\phobos\std\stdio.d(1620):        instantiated from here: write!(Scoped!(Foo),char)
temp.d(12):        instantiated from here: writeln!(Scoped!(Foo))
...\dmd2\src\phobos\std\stdio.d(714): Error: struct std.typecons.scoped!(Foo, int).scoped.Scoped!(Foo).Scoped is not copyable because it is annotated with @disable
...\dmd2\src\phobos\std\stdio.d(1620): Error: template instance std.stdio.File.write!(Scoped!(Foo),char) error instantiating
temp.d(12):        instantiated from here: writeln!(Scoped!(Foo))
...\dmd2\src\phobos\std\stdio.d(1620): Error: struct std.typecons.scoped!(Foo, int).scoped.Scoped!(Foo).Scoped is not copyable because it is annotated with @disable
temp.d(12): Error: template instance std.stdio.writeln!(Scoped!(Foo)) error instantiating
temp.d(12): Error: struct std.typecons.scoped!(Foo, int).scoped.Scoped!(Foo).Scoped is not copyable because it is annotated with @disable
...\dmd2\src\phobos\std\format.d(1821): Error: static assert  (isInputRange!(Scoped!(Foo)[])) is false
...\dmd2\src\phobos\std\format.d(1789):        instantiated from here: formatValue!(LockingTextWriter, Scoped!(Foo)[], char)
...\dmd2\src\phobos\std\format.d(2953):        instantiated from here: formatValue!(LockingTextWriter, Scoped!(Foo)[10u], char)
...\dmd2\src\phobos\std\format.d(416):        instantiated from here: formatGeneric!(LockingTextWriter, Scoped!(Foo)[10u], char)
...\dmd2\src\phobos\std\stdio.d(735):        ... (1 instantiations, -v to show) ...
...\dmd2\src\phobos\std\stdio.d(1620):        instantiated from here: write!(Scoped!(Foo)[10u],char)
temp.d(16):        instantiated from here: writeln!(Scoped!(Foo)[10u])
Comment 1 Andrej Mitrovic 2013-02-09 05:44:27 UTC
Unrelated to scoped classes, the root cause is that writeln() takes its arguments by value:

import std.stdio;

struct S
{
    @disable this(this);
}

void main()
{
    S s;
    writeln(s);  // error
}
Comment 2 Andrej Mitrovic 2013-02-18 20:10:35 UTC
As a workaround you can use this:

import std.stdio;
import std.string;

struct Wrap(T)
{
    T* t;
    auto ref opDispatch(string s, T...)(T args)
    {
        mixin(format("return t.%s(args);", s));
    }
}

auto wrap(T)(T* t)
{
    return Wrap!T(t);
}

struct S
{
    @disable this(this);
    int x;
    float y;

    string toString()
    {
        return format("S { x: %s, y: %s }", x, y);
    }
}

void main()
{
    S s = S(1, 2.0);
    writeln(wrap(&s));
}

(std.typecons.Proxy doesn't seem to help here, it won't work with pointers)

Generally speaking this is a gigantic problem to fix. Pretty much everything in Phobos expects structs to be copyable. Phobos could try to take arguments by ref (or rather auto ref for compatibility), but this would have to be applied to all functions.

I think the safest thing we can do is ask the user to use this sort of wrapper type.

Maybe writeln/format could be the exception to the rule and take all arguments by auto ref and then internally use wrappers for @disable structs to pass them to other internal formatting functions. This would make it convenient to print @disable structs since it's common to print things..
Comment 3 hsteoh 2013-03-15 10:58:55 UTC
This issue is not just limited to printing. Structs that have @disabled this(this) are currently highly-crippled, because almost everything in Phobos assumes that structs can be passed by value. A struct range with @disabled this(this) is unusable with any range function unless you do something really ugly like (&range).cycle(), etc., which is a kind of hack (taking advantage of D's automatic dereference when a pointer is used with the . operator).

Auto ref would solve a lot of these problems.
Comment 4 Berni44 2021-03-28 10:20:26 UTC
*** Issue 20632 has been marked as a duplicate of this issue. ***
Comment 5 Dlang Bot 2021-05-08 14:19:49 UTC
@berni44 created dlang/phobos pull request #8055 "Fix partly Issue 9489 - writeln of struct with disabled copy ctor" mentioning this issue:

- Fix partly Issue 9489 - writeln of struct with disabled copy ctor

https://github.com/dlang/phobos/pull/8055
Comment 6 Dlang Bot 2021-05-12 07:42:06 UTC
dlang/phobos pull request #8055 "Fix Issue 9489 - writeln of struct with disabled copy ctor" was merged into master:

- ef30be7c7376cb4aef99d851234c1197f290cea7 by berni44:
  Fix Issue 9489 - writeln of struct with disabled copy ctor

https://github.com/dlang/phobos/pull/8055
Comment 7 Dlang Bot 2021-05-18 06:49:46 UTC
dlang/phobos pull request #8092 "Revert "Fix Issue 9489 - writeln of struct with disabled copy ctor"" was merged into master:

- 116d33b63516c596f85b84793812cd97cb37578d by Iain Buclaw:
  Revert "Fix Issue 9489 - writeln of struct with disabled copy ctor"
  
  This reverts commit 838da632cbaf4a1888dd7a76754be9fb50520e25.

https://github.com/dlang/phobos/pull/8092
Comment 8 Iain Buclaw 2021-05-18 07:46:45 UTC
Fix was reverted.
Comment 9 Dlang Bot 2021-05-18 22:54:00 UTC
@dkorpel updated dlang/phobos pull request #8066 "improve documentation of SortedRange.release" mentioning this issue:

- Revert "Fix Issue 9489 - writeln of struct with disabled copy ctor"
  
  This reverts commit 838da632cbaf4a1888dd7a76754be9fb50520e25.

https://github.com/dlang/phobos/pull/8066
Comment 10 dlangBugzillaToGithub 2024-12-01 16:16:24 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/phobos/issues/9950

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB