Bug 8892 – Wrong diagnostic for static array assignment

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-10-25T09:57:00Z
Last change time
2015-12-09T12:23:08Z
Keywords
diagnostic, pull
Assigned to
andrej.mitrovich
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2012-10-25T09:57:53Z
struct Foo { char[3] data; } int bar(Foo f) { return f.data[0]; } void main() { auto f = Foo(['A', 'B']); } DMD 2.061alpha: test.d(8): Error: cannot implicitly convert expression (['A','B']) of type char[] to char A better error message is: test.d(8): Error: cannot implicitly convert expression (['A','B']) of type char[] to char[4] Or even something like: test.d(8): Error: cannot implicitly convert expression (['A','B']) of type char[] to char[4] ('Foo.data' field)
Comment #1 by andrej.mitrovich — 2012-10-25T10:47:34Z
(In reply to comment #0) > A better error message is: > > test.d(8): Error: cannot implicitly convert expression (['A','B']) of type > char[] to char[4] Don't you mean char[3]? Also the 'bar' function is not necessary for test-case.
Comment #2 by bearophile_hugs — 2012-10-25T11:14:47Z
(In reply to comment #1) > Don't you mean char[3]? Right, sorry (this code comes from Issue 8893 ). > Also the 'bar' function is not necessary for test-case. Right, shorter test case: struct Foo { char[2] data; } void main() { auto f = Foo(['A']); }
Comment #3 by andrej.mitrovich — 2012-11-29T14:49:23Z
*** Issue 8918 has been marked as a duplicate of this issue. ***
Comment #4 by andrej.mitrovich — 2013-01-26T14:35:20Z
(In reply to comment #2) > struct Foo { > char[2] data; > } > void main() { > auto f = Foo(['A']); > } What I don't understand is why the above fails at compile-time but the following compiles (it fails at runtime): char[2] data = ['A']; $ object.Error: lengths don't match for array copy, 2 = 1 I think this case should also an accepts-invalid, because the compiler can see at compile-time that the lengths don't match.
Comment #5 by andrej.mitrovich — 2013-01-26T14:44:44Z
Comment #6 by k.hara.pg — 2013-01-27T01:16:38Z
(In reply to comment #4) > What I don't understand is why the above fails at compile-time but the > following compiles (it fails at runtime): > > char[2] data = ['A']; > > $ object.Error: lengths don't match for array copy, 2 = 1 > > I think this case should also an accepts-invalid, because the compiler can see > at compile-time that the lengths don't match. It is known inconsistency around static array initialization. int[3] garr = [1,2]; // When creating data section, garr[2] is filled by 0 void main() { int[3] larr = [1,2]; // This is translated to runtime element-wise blit assign: // larr[0..3] = [1,2] // and fails at runtime. } When we based on the issue, following code still have similar issue. struct Foo { char[2] data; } void main() { auto f = Foo(['A']); // 1. should be compile time error, array length mismatch, or // 2. the initializer should be translated to Foo(['A', char.init]) ? }
Comment #7 by github-bugzilla — 2013-01-27T01:32:19Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/646ce34fae3a1e12032bb7a2343e54daf0074db0 Fixes Issue 8892 - Better diagnostic for static array assignment. https://github.com/D-Programming-Language/dmd/commit/c316e2e6d059b7e6fdc6ae8616e72dfda9dfa8f6 Merge pull request #1558 from AndrejMitrovic/Fix8892 Issue 8892 - Better diagnostic for static array assignment
Comment #8 by k.hara.pg — 2013-01-27T07:01:24Z
Error message is now improved.
Comment #9 by github-bugzilla — 2013-02-11T12:20:20Z
Comment #10 by github-bugzilla — 2013-02-11T22:49:48Z
Comment #11 by k.hara.pg — 2015-12-09T12:23:08Z
*** Issue 4945 has been marked as a duplicate of this issue. ***