Bug 8329 – foreach over string with dchar as element isn't nothrow
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-06-30T18:31:00Z
Last change time
2015-06-09T05:15:17Z
Assigned to
nobody
Creator
issues.dlang
Comments
Comment #0 by issues.dlang — 2012-06-30T18:31:02Z
This code fails to compile:
import std.stdio;
void main() nothrow
{
foreach(dchar c; "hello world")
{}
}
giving the error
q.d(5): Error: _aApplycd1 is not nothrow
q.d(3): Error: function D main 'main' is nothrow yet may throw
if the dchar is removed, then it compiles just fine. Clearly, there's an issue with the apply function which implements iterating over strings as characters other than the character type of the string, since this really should be nothrow, but it's not.
Comment #1 by k.hara.pg — 2012-06-30T22:22:23Z
(In reply to comment #0)
> This code fails to compile:
>
> import std.stdio;
>
> void main() nothrow
> {
> foreach(dchar c; "hello world")
> {}
> }
>
> giving the error
>
> q.d(5): Error: _aApplycd1 is not nothrow
> q.d(3): Error: function D main 'main' is nothrow yet may throw
>
> if the dchar is removed, then it compiles just fine. Clearly, there's an issue
> with the apply function which implements iterating over strings as characters
> other than the character type of the string, since this really should be
> nothrow, but it's not.
I think it should not be nothrow. Iterating string(== immutable(char)[]) as a range of dchar runs decoding of UTF-8, and if given string has invalid UTF-8 sequence, it should throw UnicodeException (it's declared in core.exception).
On the other hand, such a transcoding iteration should be pure and @safe. It's enough worth.