Bug 16642 – byCodeUnit doesn't work AutodecodableStrings unless they're actually strings or alias a variable that's a string
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-10-27T08:09:44Z
Last change time
2018-01-05T13:27:38Z
Assigned to
No Owner
Creator
Jonathan M Davis
Comments
Comment #0 by issues.dlang — 2016-10-27T08:09:44Z
This code fails to compile
import std.utf;
struct S
{
string s;
string str() { return s; }
alias str this;
}
void main()
{
auto s = S("hello");
auto range = byCodeUnit(s);
}
Similarly, this fails to compile
import std.utf;
enum E { a = "hello" }
void main()
{
auto s = E.a;
auto range = byCodeUnit(s);
}
Also, this compiles but uses the alias rather than the range API and thus fails the assertion:
import std.range.primitives;
import std.utf;
static struct RangeAndStringish
{
string data;
string s;
alias s this;
bool empty() { return data.empty; }
char front() { return data[0]; }
void popFront() { data = data[1 .. $]; }
}
void main()
{
auto fn = RangeAndStringish("test.d", "other");
auto x = fn.byCodeUnit();
assert(x.front == 't');
}
I suppose that it could be argued that this last case is doing the right thing - or at least that it's ambiguous, but normally, an implicit conversion is only supposed to be used if the type itself doesn't work directly, and in this case, the type itself would work without the implicit conversion.
Comment #2 by github-bugzilla — 2017-03-10T21:02:59Z
Commits pushed to master at https://github.com/dlang/phoboshttps://github.com/dlang/phobos/commit/6e3d41cfe92d0af0360002db6436d2ec98f503af
Fix issue 16642: byCodeUnit doesn't work properly with alias this.
If a template is going to allow implicit conversions, it really needs to
force the conversion in order to avoid subtle bugs. This fixes
byCodeUnit so that it forces the conversion to a string type for types
that convert implicitly. It also fixes it so that types which implicitly
convert to a string type but are also ranges of characters without the
conversion will not be converted by byCodeUnit.
https://github.com/dlang/phobos/commit/13b3da64a81360e96bab3d1bb09a3a42b751da36
Merge pull request #4879 from jmdavis/issue_16642
Fix Issue 16642 - byCodeUnit doesn't work properly with alias this.
Comment #3 by github-bugzilla — 2017-03-22T12:22:32Z