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

Fix #10607 - Structs lines and ByLineCopy cannot be usefully constructed in @safe code #9035

Merged
merged 1 commit into from
Jan 1, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions std/stdio.d
Original file line number Diff line number Diff line change
Expand Up @@ -2409,13 +2409,13 @@ void main()
private struct ByLineCopy(Char, Terminator)
{
private:
import std.typecons : RefCounted, RefCountedAutoInitialize;
import std.typecons : SafeRefCounted, RefCountedAutoInitialize;

/* Ref-counting stops the source range's ByLineCopyImpl
* from getting out of sync after the range is copied, e.g.
* when accessing range.front, then using std.range.take,
* then accessing range.front again. */
alias Impl = RefCounted!(ByLineCopyImpl!(Char, Terminator),
alias Impl = SafeRefCounted!(ByLineCopyImpl!(Char, Terminator),
RefCountedAutoInitialize.no);
Impl impl;

Expand Down Expand Up @@ -4626,7 +4626,7 @@ struct lines
f = File to read lines from.
terminator = Line separator (`'\n'` by default).
*/
this(File f, dchar terminator = '\n')
this(File f, dchar terminator = '\n') @safe
{
this.f = f;
this.terminator = terminator;
Expand Down Expand Up @@ -4721,6 +4721,47 @@ struct lines
}
}

@safe unittest
{
/*
As pointed out in <https://github.com/dlang/phobos/issues/10605>,
it's a pity that `byLine()` & co. aren't @safe to use yet.

This is a first attempt at working towards that goal.
For now, this test doesn't do much; as there isn't much to do safely yet.
*/
auto deleteMe = testFilename();
scope(exit) { imported!"std.file".remove(deleteMe); }

// Setup
{
auto f = File(deleteMe, "w");
scope(exit) { f.close(); }
foreach (i; 1 .. 11)
f.writeln(i);
}

// Actual tests
{
auto f = File(deleteMe, "r");
scope(exit) { f.close(); }

auto myLines = lines(f);
foreach (string line; myLines)
continue;

auto myByLineCopy = f.byLineCopy; // but cannot safely iterate yet
/*
still `@system`:
- cannot call `@system` function `std.stdio.File.ByLineCopy!(immutable(char), char).ByLineCopy.empty`
- cannot call `@system` function `std.stdio.File.ByLineCopy!(immutable(char), char).ByLineCopy.popFront`
- cannot call `@system` function `std.stdio.File.ByLineCopy!(immutable(char), char).ByLineCopy.front`
*/
//foreach (line; myByLineCopy)
// continue;
}
}

@system unittest
{
static import std.file;
Expand Down
Loading