Bug 21583 – `array` is not `@safe` for a certain combination of `iota` and data types

Status
NEW
Severity
normal
Priority
P3
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-01-26T05:42:04Z
Last change time
2024-12-01T16:38:19Z
Keywords
industry, safe
Assigned to
No Owner
Creator
Mathias LANG
Moved to GitHub: phobos#10454 →

Comments

Comment #0 by pro.mathias.lang — 2021-01-26T05:42:04Z
I'm fairly certain the following should compile: ``` import std.range, std.array, std.algorithm; @safe: struct Block { int[] data; } public struct Height { /// public ulong value; /// Provides implicit conversion to `ulong` public alias value this; } void main () @safe { auto x = getBlocksFrom(Height.init); } public auto getBlocksFrom (Height start_height) @safe nothrow { auto r = iota(start_height, Height(42)); auto r2 = r.map!(idx => const(Block).init); return r2.array; } ``` Bug disappear if: - The mapping is not to a `const(Block)`; - I pass `ulong` or similar instead of `Height`; - `Block` does not contain an array... Or if it's a `string`;
Comment #1 by bugzilla — 2021-05-02T10:59:46Z
The reason is the cast() here: emplaceRef!(Unqual!T)(bigData[len], cast() item); found in std.array: https://github.com/dlang/phobos/blob/a1ee4fd4fd02529f5849e317fe9eccb8280f4b37/std/array.d#L3466 (Error: cast from `const(Block)` to `Block` not allowed in safe code) If it is replaced by emplaceRef!(Unqual!T)(bigData[len], () @trusted { return cast() item; }() ); the example works. But I'm not sure if I want to trust that cast...
Comment #2 by robert.schadek — 2024-12-01T16:38:19Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/phobos/issues/10454 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB