[Issue 11648] rangeerror when adding element to associative array and value is implicitely converted by alias this
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sat Nov 30 09:29:24 PST 2013
https://d.puremagic.com/issues/show_bug.cgi?id=11648
Kenji Hara <k.hara.pg at gmail.com> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |RESOLVED
Resolution| |INVALID
--- Comment #1 from Kenji Hara <k.hara.pg at gmail.com> 2013-11-30 09:29:22 PST ---
(In reply to comment #0)
> dmd 2.063 compiles and runs this code correctly:
>
> //////////////////
> import std.stdio;
>
> struct S
> {
> int _payload;
> alias _payload this;
> }
>
> void main()
> {
> S[string] symbols;
> symbols["a"] = 1; // works if using S(1)
> writeln(symbols); // ["a":1]
> }
> ////////////////
>
> but dmd 2.064 causes a range error when executing:
>
> core.exception.RangeError at test(12): Range violation
This is intended behavior introduce by fixing bug 6178.
Test case in compiler test suite.
https://github.com/D-Programming-Language/dmd/pull/2539/files#diff-a137107b45f82e7d3b516e2aff79f1d5R940
First, 'alias this' does not work for object construction.
struct S { int val; alias val this; }
S s = 1; // compile error
And in your case, AA does not have a storage corresponding to the key "a" yet.
So, `symbols["a"] = 1;` should invoke _construction of S from integer 1_. But
as I shown, S cannot construct from int. So, compiler always treat the line as
_an assignment through alias this_. And of course, if the specified key does
not exist in AA, the indexing should throw RangeError.
--
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list