diff --git a/src/mapping.c b/src/mapping.c
index 54675d3732feb591643a87b95bf0533deeb90398..170446cf599b0596c529a2c5052b37a2b8a77db5 100644
--- a/src/mapping.c
+++ b/src/mapping.c
@@ -117,6 +117,10 @@ void mapping_free_keypair(struct mapping_data *md, struct keypair *k)
   FREE_KEYPAIR(md, k);
 }
 
+static INLINE int check_type_contains(TYPE_FIELD types, const struct svalue * s) {
+    return (TYPEOF(*s) == PIKE_T_OBJECT || types & (BIT_OBJECT|(1 << TYPEOF(*s))));
+}
+
 #ifdef PIKE_DEBUG
 
 /** This function checks that the type field isn't lacking any bits.
@@ -549,7 +553,7 @@ struct mapping_data *copy_mapping_data(struct mapping_data *md)
   {								\
     h=h2 & (md->hashsize - 1);					\
     DO_IF_DEBUG( if(d_flag > 1) check_mapping_type_fields(m); ) \
-    if(md->ind_types & ((1 << TYPEOF(*key)) | BIT_OBJECT))	\
+    if(check_type_contains(md->ind_types, key))			\
     {								\
       for(prev= md->hash + h;(k=*prev);prev=&k->next)		\
       {								\
@@ -572,7 +576,7 @@ struct mapping_data *copy_mapping_data(struct mapping_data *md)
   {								\
     h=h2 & (md->hashsize-1);					\
     DO_IF_DEBUG( if(d_flag > 1) check_mapping_type_fields(m); ) \
-    if(md->ind_types & ((1 << TYPEOF(*key)) | BIT_OBJECT))	\
+    if(check_type_contains(md->ind_types, key))			\
     {								\
       k2=omd->hash[h2 & (omd->hashsize - 1)];			        \
       prev= md->hash + h;					\
diff --git a/src/testsuite.in b/src/testsuite.in
index b4f6c85d29a1c4fcc65940b69ae40eb82a608382..6a686a4e5cae213ab6af492846f483e9e003e0e8 100644
--- a/src/testsuite.in
+++ b/src/testsuite.in
@@ -5779,6 +5779,24 @@ void create()
 
 ]])
 
+test_any([[
+    mapping m = ([ "foo" : 1 ]);
+    class A {
+	int __hash() { return hash_value("foo"); }
+	int `==(mixed o) { return o == "foo"; }
+    };
+    return m[A()];
+]], 1)
+
+test_any([[
+    mapping m = ([ "foo" : 1 ]);
+    class A {
+	int __hash() { return hash_value("foo"); }
+	int `==(mixed o) { return o == "foo"; }
+    };
+    return sizeof(m - ([ A() : 1 ]));
+]], 0)
+
 test_equal([[ `+( ([1:2]) )]],[[ ([1:2]) ]])
 test_false( `+( ([1:2]) ) == ([1:2]) )
 test_equal([[ `+( ([1:2]), ([1:2])  )]],[[ ([1:2]) ]])