D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 11733 - Refuse int indexing of associative arrays with char keys
Summary: Refuse int indexing of associative arrays with char keys
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2013-12-12 10:11 UTC by bearophile_hugs
Modified: 2024-12-13 18:15 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2013-12-12 10:11:20 UTC
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 yebblies 2013-12-12 23:01:55 UTC
Why?  0 implicitly converts to both char and dchar.
Comment 2 bearophile_hugs 2013-12-13 04:46:25 UTC
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 yebblies 2013-12-13 06:19:24 UTC
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 dlangBugzillaToGithub 2024-12-13 18:15:10 UTC
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