Bug 11733 – Refuse int indexing of associative arrays with char keys

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-12-12T10:11:20Z
Last change time
2024-12-13T18:15:10Z
Keywords
accepts-invalid
Assigned to
No Owner
Creator
bearophile_hugs
Moved to GitHub: dmd#18738 →

Comments

Comment #0 by bearophile_hugs — 2013-12-12T10:11:20Z
I think this code should be disallowed: void main() { int[char] aa1; int[dchar] aa2; auto r1 = aa1[0]; auto r2 = aa2[0]; } dmd 2.065alpha compiles it with no errors, and gives at run-time: core.exception.RangeError@temp(4): Range violation
Comment #1 by yebblies — 2013-12-12T23:01:55Z
Why? 0 implicitly converts to both char and dchar.
Comment #2 by bearophile_hugs — 2013-12-13T04:46:25Z
This is not as important as other changes I have proposed elsewhere. (In reply to comment #1) > Why? 0 implicitly converts to both char and dchar. Because int and char are two different types. It makes code less clear, and in D there is a simple syntax to express that: auto r1 = aa1['\0']; auto r2 = aa2['\0']; There is no ambiguity here, '\0' is clearly a char for the person that reads the code. Using 0 instead of '\0' seems a weakly typing archaism coming from C.
Comment #3 by yebblies — 2013-12-13T06:19:24Z
There is no ambiguity anyway. Asking for int not to convert to char is not new, and I vaguely recall it was rejected on the grounds that char x = 'a'+7; should work. I don't if there is an existing report for that.
Comment #4 by robert.schadek — 2024-12-13T18:15:10Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18738 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB