Bug 11648 – rangeerror when adding element to associative array and value is implicitely converted by alias this

Status
RESOLVED
Resolution
INVALID
Severity
regression
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-11-30T06:33:00Z
Last change time
2013-11-30T09:43:42Z
Keywords
wrong-code
Assigned to
nobody
Creator
r.sagitario

Comments

Comment #0 by r.sagitario — 2013-11-30T06:33:16Z
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@test(12): Range violation
Comment #1 by k.hara.pg — 2013-11-30T09:29:22Z
(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@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.
Comment #2 by r.sagitario — 2013-11-30T09:43:42Z
Thanks for explanation.