Skip to content

Commit

Permalink
Merge pull request #8092 from dlang/revert-8055-issue_9489
Browse files Browse the repository at this point in the history
Revert "Fix Issue 9489 - writeln of struct with disabled copy ctor"
merged-on-behalf-of: Razvan Nitu <RazvanN7@users.noreply.github.com>
  • Loading branch information
dlang-bot authored May 18, 2021
2 parents 7ff6bb7 + c162202 commit 862c392
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 70 deletions.
4 changes: 2 additions & 2 deletions std/format/internal/write.d
Original file line number Diff line number Diff line change
Expand Up @@ -3265,12 +3265,12 @@ if ((!is(StringTypeOf!T) || hasToString!(T, Char)) && !is(CharTypeOf!T) || is(T
}

// Fix for https://issues.dlang.org/show_bug.cgi?id=1591
int getNthInt(string kind, A...)(uint index, auto ref A args)
int getNthInt(string kind, A...)(uint index, A args)
{
return getNth!(kind, isIntegral, int)(index, args);
}

T getNth(string kind, alias Condition, T, A...)(uint index, auto ref A args)
T getNth(string kind, alias Condition, T, A...)(uint index, A args)
{
import std.conv : text, to;
import std.format : FormatException;
Expand Down
2 changes: 1 addition & 1 deletion std/format/write.d
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ Note:
implementation there are some cases where allocations occur.
See $(REF_ALTTEXT $(D sformat), sformat, std, format) for more details.
*/
uint formattedWrite(Writer, Char, Args...)(auto ref Writer w, const scope Char[] fmt, auto ref Args args)
uint formattedWrite(Writer, Char, Args...)(auto ref Writer w, const scope Char[] fmt, Args args)
{
import std.conv : text;
import std.format : enforceFmt, FormatException;
Expand Down
90 changes: 23 additions & 67 deletions std/stdio.d
Original file line number Diff line number Diff line change
Expand Up @@ -1700,46 +1700,46 @@ Writes its arguments in text format to the file.
Throws: `Exception` if the file is not opened.
`ErrnoException` on an error writing to the file.
*/
void write(S...)(auto ref S args)
void write(S...)(S args)
{
import std.traits : isBoolean, isIntegral, isAggregateType;
import std.utf : UTFException;
auto w = lockingTextWriter();
static foreach (i; 0 .. args.length)
foreach (arg; args)
{
try
{
alias A = typeof(args[i]);
alias A = typeof(arg);
static if (isAggregateType!A || is(A == enum))
{
import std.format.write : formattedWrite;

formattedWrite(w, "%s", args[i]);
formattedWrite(w, "%s", arg);
}
else static if (isSomeString!A)
{
put(w, args[i]);
put(w, arg);
}
else static if (isIntegral!A)
{
import std.conv : toTextRange;

toTextRange(args[i], w);
toTextRange(arg, w);
}
else static if (isBoolean!A)
{
put(w, args[i] ? "true" : "false");
put(w, arg ? "true" : "false");
}
else static if (isSomeChar!A)
{
put(w, args[i]);
put(w, arg);
}
else
{
import std.format.write : formattedWrite;

// Most general case
formattedWrite(w, "%s", args[i]);
formattedWrite(w, "%s", arg);
}
}
catch (UTFException e)
Expand All @@ -1758,7 +1758,7 @@ Writes its arguments in text format to the file, followed by a newline.
Throws: `Exception` if the file is not opened.
`ErrnoException` on an error writing to the file.
*/
void writeln(S...)(auto ref S args)
void writeln(S...)(S args)
{
write(args, '\n');
}
Expand All @@ -1776,7 +1776,7 @@ args = Items to write.
Throws: `Exception` if the file is not opened.
`ErrnoException` on an error writing to the file.
*/
void writef(alias fmt, A...)(auto ref A args)
void writef(alias fmt, A...)(A args)
if (isSomeString!(typeof(fmt)))
{
import std.format : checkFormatException;
Expand All @@ -1787,15 +1787,15 @@ Throws: `Exception` if the file is not opened.
}

/// ditto
void writef(Char, A...)(in Char[] fmt, auto ref A args)
void writef(Char, A...)(in Char[] fmt, A args)
{
import std.format.write : formattedWrite;

formattedWrite(lockingTextWriter(), fmt, args);
}

/// Equivalent to `file.writef(fmt, args, '\n')`.
void writefln(alias fmt, A...)(auto ref A args)
void writefln(alias fmt, A...)(A args)
if (isSomeString!(typeof(fmt)))
{
import std.format : checkFormatException;
Expand All @@ -1806,7 +1806,7 @@ Throws: `Exception` if the file is not opened.
}

/// ditto
void writefln(Char, A...)(in Char[] fmt, auto ref A args)
void writefln(Char, A...)(in Char[] fmt, A args)
{
import std.format.write : formattedWrite;

Expand Down Expand Up @@ -4160,7 +4160,7 @@ void main()
}
---
*/
void write(T...)(auto ref T args)
void write(T...)(T args)
if (!is(T[0] : File))
{
trustedStdout.write(args);
Expand Down Expand Up @@ -4209,7 +4209,7 @@ void main()
}
---
*/
void writeln(T...)(auto ref T args)
void writeln(T...)(T args)
{
static if (T.length == 0)
{
Expand Down Expand Up @@ -4356,57 +4356,13 @@ void writeln(T...)(auto ref T args)
useInit(stdout.lockingTextWriter());
}

// https://issues.dlang.org/show_bug.cgi?id=9489
@system unittest
{
static import std.file;
import std.typecons : scoped;

auto deleteme = testFilename();
auto f = File(deleteme, "w");
scope(exit) { std.file.remove(deleteme); }

class Foo
{
int x;
this(int x_) { this.x = x_; }
override string toString() { return "xxx"; }
}

auto foo = scoped!Foo(100);
f.writeln(foo.x);
f.writeln(foo);
f.close();

version (Windows)
assert(cast(char[]) std.file.read(deleteme) == "100\r\nxxx\r\n");
else
assert(cast(char[]) std.file.read(deleteme) == "100\nxxx\n");
// https://issues.dlang.org/show_bug.cgi?id=21920
void function(string) printer = &writeln!string;
if (false) printer("Hello");
}

// https://issues.dlang.org/show_bug.cgi?id=9489 (reduced test case)
@system unittest
{
static import std.file;

auto deleteme = testFilename();
auto f = File(deleteme, "w");
scope(exit) { std.file.remove(deleteme); }

struct S
{
@disable this(this);
}

S s;
f.writeln(s);
f.close();

version (Windows)
assert(cast(char[]) std.file.read(deleteme) == "S()\r\n");
else
assert(cast(char[]) std.file.read(deleteme) == "S()\n");
}

/***********************************
Writes formatted data to standard output (without a trailing newline).
Expand All @@ -4431,7 +4387,7 @@ stderr.writef("%s", "message");
------
*/
void writef(alias fmt, A...)(auto ref A args)
void writef(alias fmt, A...)(A args)
if (isSomeString!(typeof(fmt)))
{
import std.format : checkFormatException;
Expand All @@ -4442,7 +4398,7 @@ if (isSomeString!(typeof(fmt)))
}

/// ditto
void writef(Char, A...)(in Char[] fmt, auto ref A args)
void writef(Char, A...)(in Char[] fmt, A args)
{
trustedStdout.writef(fmt, args);
}
Expand Down Expand Up @@ -4472,7 +4428,7 @@ void writef(Char, A...)(in Char[] fmt, auto ref A args)
/***********************************
* Equivalent to $(D writef(fmt, args, '\n')).
*/
void writefln(alias fmt, A...)(auto ref A args)
void writefln(alias fmt, A...)(A args)
if (isSomeString!(typeof(fmt)))
{
import std.format : checkFormatException;
Expand All @@ -4483,7 +4439,7 @@ if (isSomeString!(typeof(fmt)))
}

/// ditto
void writefln(Char, A...)(in Char[] fmt, auto ref A args)
void writefln(Char, A...)(in Char[] fmt, A args)
{
trustedStdout.writefln(fmt, args);
}
Expand Down

0 comments on commit 862c392

Please sign in to comment.