Bug 11494 – std.array.appender is not nothrow

Status
RESOLVED
Resolution
INVALID
Severity
minor
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-11-10T15:45:58Z
Last change time
2017-08-26T17:11:21Z
Keywords
pull
Assigned to
No Owner
Creator
Adam D. Ruppe

Comments

Comment #0 by destructionator — 2013-11-10T15:45:58Z
This propagates up to to!(array)(other_array) making it not work in nothrow too, despite the fact they otherwise don't throw anything except out of memory errors.
Comment #1 by destructionator — 2013-11-13T08:20:50Z
Comment #2 by monarchdodra — 2013-11-13T08:52:31Z
(In reply to comment #0) > This propagates up to to!(array)(other_array) making it not work in nothrow > too, despite the fact they otherwise don't throw anything except out of memory > errors. What do you mean? This works fine on my HEAD. It's even safe and pure: //---- import std.conv, std.array; void main() nothrow @safe pure { int[] arr; to!(int[])(arr); appender(arr); } //---- This is with 2.064.2. In 2.063.2 it's not @safe. Are you sure the problem in "to" isn't that the duplicated array contains objects with throwing postblits by any chance?
Comment #3 by destructionator — 2013-11-13T09:22:14Z
The specific thing that got this was here: import std.conv; import std.array; void main() nothrow @safe { string s = "foo"; auto b = to!(ubyte[])(s); } testarray.d(6): Error: 'std.conv.to!(ubyte[]).to!(string).to' is not nothrow Though, now the change I did in the pull isn't fixing it. When I first played with this, it was on my other computer which has an older dmd, so maybe something else has changed since then, but this test here still fails to compile on my desktop. I'm not sure what's going on here, maybe it is trying to parse the string. But the appender issue itself might be invalid and/or already fixed in the newest dmd though.
Comment #4 by monarchdodra — 2013-11-13T10:17:38Z
(In reply to comment #3) > I'm not sure what's going on here, maybe it is trying to parse the string. But > the appender issue itself might be invalid and/or already fixed in the newest > dmd though. It's trying to parse the string. If you want to "interpret" the string as ubytes, you can use "std.string.representation". This will "reinterpret" your string according to its type: string => immutable(ubyte)[] wchar[] => ushort[] dchar[] => uint[] However, this will not *allocate* a new array, and it is not safe either. It *is* nothrow though. If you want a new array, I'd recommend adding a ".dup", but that's not nothrow. I don't know any 1 liners that are @safe, pure and nothrow. My recommendation would be to make an explicit trusted helper that will do it for you. Eg: ubyte[] str2ubyte(in char[] str) pure nothrow { auto arr = uninitializedArray!(ubyte[])(str.length); arr[]=str.representation()[]; return arr; } You could templatize too, just like representation, so as to work on any width.