From 0e08634edb3f06c3879f679ca97e7dde5340b915 Mon Sep 17 00:00:00 2001 From: Walter Bright Date: Tue, 31 Dec 2013 21:18:10 -0800 Subject: [PATCH] fix Issue 11591 - std.typecons.Tuple -s with classes fails at runtime as associative array keys --- src/mtype.c | 13 +++++++++++++ test/fail_compilation/fail11591.d | 14 ++++++++++++++ test/runnable/sdtor.d | 5 +++++ test/runnable/test12.d | 1 + 4 files changed, 33 insertions(+) create mode 100644 test/fail_compilation/fail11591.d diff --git a/src/mtype.c b/src/mtype.c index 8d6246e3072f..a69b416f4330 100644 --- a/src/mtype.c +++ b/src/mtype.c @@ -4820,6 +4820,19 @@ printf("index->ito->ito = x%x\n", index->ito->ito); error(loc, "can't have associative array key of %s", index->toBasetype()->toChars()); case Terror: return Type::terror; + case Tstruct: + { + /* AA's need opCmp. Issue error if not correctly set up. + */ + TypeStruct *ts = (TypeStruct *)index->toBasetype(); + if (ts->sym->xcmp == ts->sym->xerrcmp) + { + error(loc, "associative array key type %s does not have 'const int opCmp(ref const %s)' member function", + index->toBasetype()->toChars(), ts->sym->toChars()); + return Type::terror; + } + break; + } } next = next->semantic(loc,sc)->merge2(); transitive(); diff --git a/test/fail_compilation/fail11591.d b/test/fail_compilation/fail11591.d new file mode 100644 index 000000000000..d10fc61a4f2b --- /dev/null +++ b/test/fail_compilation/fail11591.d @@ -0,0 +1,14 @@ +/* +TEST_OUTPUT: +--- +fail_compilation/fail11591.d(13): Error: associative array key type T does not have 'const int opCmp(ref const T)' member function +--- +*/ + +struct T { + bool opCmp(int i) { return false; } +} + +void main() { + int[T] aa; +} diff --git a/test/runnable/sdtor.d b/test/runnable/sdtor.d index f817cada59c1..e42bea702d7b 100644 --- a/test/runnable/sdtor.d +++ b/test/runnable/sdtor.d @@ -2415,6 +2415,11 @@ struct Test9386 printf("Deleted %.*s\n", name.length, name.ptr); op ~= "c"; } + + const int opCmp(ref const Test9386 t) + { + return op[0] - t.op[0]; + } } void test9386() diff --git a/test/runnable/test12.d b/test/runnable/test12.d index 4cb572b2fc99..37a3492675b8 100644 --- a/test/runnable/test12.d +++ b/test/runnable/test12.d @@ -914,6 +914,7 @@ struct Property struct Value { int a,b,c,d; + const int opCmp(ref const Value v) { return 0; } } struct PropTable