Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

writeln of struct with disabled copy ctor #9950

Open
dlangBugzillaToGithub opened this issue Feb 9, 2013 · 9 comments
Open

writeln of struct with disabled copy ctor #9950

dlangBugzillaToGithub opened this issue Feb 9, 2013 · 9 comments

Comments

@dlangBugzillaToGithub
Copy link

bearophile_hugs reported this on 2013-02-09T04:52:58Z

Transfered from https://issues.dlang.org/show_bug.cgi?id=9489

CC List

Description

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])
@dlangBugzillaToGithub
Copy link
Author

andrej.mitrovich (@AndrejMitrovic) commented on 2013-02-09T05:44:27Z

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
}

@dlangBugzillaToGithub
Copy link
Author

andrej.mitrovich (@AndrejMitrovic) commented on 2013-02-18T20:10:35Z

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..

@dlangBugzillaToGithub
Copy link
Author

hsteoh commented on 2013-03-15T10:58:55Z

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.

@dlangBugzillaToGithub
Copy link
Author

bugzilla (@WalterBright) commented on 2021-03-28T10:20:26Z

*** Issue 20632 has been marked as a duplicate of this issue. ***

@dlangBugzillaToGithub
Copy link
Author

dlang-bot commented on 2021-05-08T14:19:49Z

@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

@dlangBugzillaToGithub
Copy link
Author

dlang-bot commented on 2021-05-12T07:42:06Z

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

@dlangBugzillaToGithub
Copy link
Author

dlang-bot commented on 2021-05-18T06:49:46Z

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

@dlangBugzillaToGithub
Copy link
Author

ibuclaw (@ibuclaw) commented on 2021-05-18T07:46:45Z

Fix was reverted.

@dlangBugzillaToGithub
Copy link
Author

dlang-bot commented on 2021-05-18T22:54:00Z

@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

@thewilsonator thewilsonator removed P3 OS:Windows Issues Specific to Windows Arch:x86 Issues specific to x86 labels Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants