Bug 12030 – Detect some wrong array slice assignments at compile time

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-01-29T08:27:22Z
Last change time
2022-08-22T11:21:37Z
Keywords
accepts-invalid
Assigned to
No Owner
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2014-01-29T08:27:22Z
This contains two related enhancement requests. Some missed compile-time tests: void main() { ubyte[10] a; ubyte[20] b; b[1 .. a.length + 2] = a[]; } With dmd 2.065beta compiles with no errors and then gives at run-time: object.Error: Array lengths don't match for copy: 10 != 11 void main() { int[10] a, b; b[$-5 .. $] = a[0 .. 4]; } With dmd 2.065beta compiles with no errors and then gives at run-time: object.Error: Array lengths don't match for copy: 4 != 5 I'd like those two programs to give compile-time errors. D is a statically typed language, and I think there's there is enough compile-time information to avoid errors at run-time. Second enhancement request: once such tests are done at compile-time, there is NO need to perform them again at run-time, speeding up the code a little.
Comment #1 by dlang-bugzilla — 2017-07-05T20:21:21Z
(In reply to bearophile_hugs from comment #0) > This contains two related enhancement requests. > > Some missed compile-time tests: > > void main() { > ubyte[10] a; > ubyte[20] b; > b[1 .. a.length + 2] = a[]; > } Since https://github.com/dlang/dmd/pull/3485 this no longer compiles: test.d(4): Error: mismatched array lengths, 11 and 10
Comment #2 by razvan.nitu1305 — 2022-08-22T11:21:37Z
Both pieces of code presented in the original bug report now fail to compile. Closing as fixed.