Bug 13882 – Use compile-time length check in std.algorithm.copy

Status
NEW
Severity
enhancement
Priority
P4
Component
phobos
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2014-12-20T14:05:25Z
Last change time
2024-12-01T16:23:25Z
Keywords
pull
Assigned to
No Owner
Creator
bearophile_hugs
Moved to GitHub: phobos#10107 →

Comments

Comment #0 by bearophile_hugs — 2014-12-20T14:05:25Z
Using std.algorithm.copy on two sequences that have compile-time length compiles with no errors and always excepts at run-time (when the source is longer than the destination): void main() pure @safe nothrow @nogc { import std.algorithm: copy; int[6] source; int[5] dest; //copy(source, dest); // Not supported yet. copy(source[], dest[]); // No compilation error } This means the D type system here has compile-time knowledge, that we throw away, turning a stronger static typing in an always run-time error. So I suggest to std.algorithm.copy use the compile-time lengths when they are available: void main() pure @safe nothrow @nogc { import std.algorithm: copy; int[6] source; int[5] dest; copy(source, dest); // Compile-time length mismatch error. } Another usage example, using the improved std.range.takeExactly from Issue 13881 : void main() pure @safe nothrow @nogc { import std.algorithm: copy; import std.range: takeExactly; int[6] source; int[5] dest; copy(source.takeExactly!5, dest[]); // OK }
Comment #1 by yebblies — 2015-01-24T15:50:55Z
Implementing issue 13185 would make this possible, without specializing for static arrays. Length of slices of static arrays can be found via const-folding, and if matching length was checked in an in-contract it could be reported at compile-time.
Comment #2 by dlang-bot — 2019-05-02T21:09:56Z
@n8sh created dlang/phobos pull request #6991 "Fix Issue 13882 - Use compile-time length check in std.algorithm.copy" fixing this issue: - Fix Issue 13882 - Use compile-time length check in std.algorithm.copy https://github.com/dlang/phobos/pull/6991
Comment #3 by robert.schadek — 2024-12-01T16:23:25Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/phobos/issues/10107 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB