Skip to content

Commit fdcb91d

Browse files
committed
Fix #10605 - Make File.byLine usable in @safe contexts
1 parent 336bed6 commit fdcb91d

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

std/stdio.d

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2169,13 +2169,13 @@ Allows to directly use range operations on lines of a file.
21692169
private struct ByLineImpl(Char, Terminator)
21702170
{
21712171
private:
2172-
import std.typecons : RefCounted, RefCountedAutoInitialize;
2172+
import std.typecons : borrow, RefCountedAutoInitialize, SafeRefCounted;
21732173

21742174
/* Ref-counting stops the source range's Impl
21752175
* from getting out of sync after the range is copied, e.g.
21762176
* when accessing range.front, then using std.range.take,
21772177
* then accessing range.front again. */
2178-
alias PImpl = RefCounted!(Impl, RefCountedAutoInitialize.no);
2178+
alias PImpl = SafeRefCounted!(Impl, RefCountedAutoInitialize.no);
21792179
PImpl impl;
21802180

21812181
static if (isScalarType!Terminator)
@@ -2190,19 +2190,24 @@ Allows to directly use range operations on lines of a file.
21902190
impl = PImpl(f, kt, terminator);
21912191
}
21922192

2193-
@property bool empty()
2193+
/* Verifiably `@safe` when built with -preview=DIP1000. */
2194+
@property bool empty() @trusted
21942195
{
2195-
return impl.refCountedPayload.empty;
2196+
// Using `ref` is actually necessary here.
2197+
return impl.borrow!((ref i) => i.empty);
21962198
}
21972199

2198-
@property Char[] front()
2200+
/* Verifiably `@safe` when built with -preview=DIP1000. */
2201+
@property Char[] front() @trusted
21992202
{
2200-
return impl.refCountedPayload.front;
2203+
// Using `ref` is likely optional here.
2204+
return impl.borrow!((ref i) => i.front);
22012205
}
22022206

2203-
void popFront()
2207+
/* Verifiably `@safe` when built with -preview=DIP1000. */
2208+
void popFront() @trusted
22042209
{
2205-
impl.refCountedPayload.popFront();
2210+
return impl.borrow!((ref i) => i.popFront());
22062211
}
22072212

22082213
private:
@@ -2216,7 +2221,7 @@ Allows to directly use range operations on lines of a file.
22162221
KeepTerminator keepTerminator;
22172222
bool haveLine;
22182223

2219-
public:
2224+
public @safe:
22202225
this(File f, KeepTerminator kt, Terminator terminator)
22212226
{
22222227
file = f;
@@ -2243,7 +2248,7 @@ Allows to directly use range operations on lines of a file.
22432248
haveLine = false;
22442249
}
22452250

2246-
private:
2251+
private @safe:
22472252
void needLine()
22482253
{
22492254
if (haveLine)
@@ -2375,7 +2380,7 @@ void main()
23752380
return ByLineImpl!(Char, Terminator)(this, keepTerminator, terminator);
23762381
}
23772382

2378-
@system unittest
2383+
@safe unittest
23792384
{
23802385
static import std.file;
23812386
auto deleteme = testFilename();
@@ -2393,7 +2398,7 @@ void main()
23932398
}
23942399

23952400
// https://issues.dlang.org/show_bug.cgi?id=19980
2396-
@system unittest
2401+
@safe unittest
23972402
{
23982403
static import std.file;
23992404
auto deleteme = testFilename();
@@ -2541,12 +2546,11 @@ $(REF readText, std,file)
25412546
is(typeof(File("").byLineCopy!(char, char).front) == char[]));
25422547
}
25432548

2544-
@system unittest
2549+
@safe unittest
25452550
{
25462551
import std.algorithm.comparison : equal;
25472552
static import std.file;
25482553

2549-
scope(failure) printf("Failed test at line %d\n", __LINE__);
25502554
auto deleteme = testFilename();
25512555
std.file.write(deleteme, "");
25522556
scope(success) std.file.remove(deleteme);
@@ -2620,7 +2624,7 @@ $(REF readText, std,file)
26202624
test("sue\r", ["sue\r"], kt, '\r');
26212625
}
26222626

2623-
@system unittest
2627+
@safe unittest
26242628
{
26252629
import std.algorithm.comparison : equal;
26262630
import std.range : drop, take;
@@ -4765,6 +4769,15 @@ struct lines
47654769
foreach (line; myByLineCopy)
47664770
continue;
47674771
}
4772+
4773+
{
4774+
auto f = File(deleteMe, "r");
4775+
scope(exit) { f.close(); }
4776+
4777+
auto myByLine = f.byLine;
4778+
foreach (line; myByLine)
4779+
continue;
4780+
}
47684781
}
47694782

47704783
@system unittest

0 commit comments

Comments
 (0)