Skip to content

Commit

Permalink
Fix issue 17968 - When generating xtoHash function for a struct, use
Browse files Browse the repository at this point in the history
typeid(const(Object)) for class members instead of the derived type to avoid
pulling in ungenerated symbols
  • Loading branch information
schveiguy committed May 24, 2018
1 parent b625e01 commit 6fefecf
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/dmd/clone.d
Expand Up @@ -765,7 +765,10 @@ extern (C++) FuncDeclaration buildXtoHash(StructDeclaration sd, Scope* sc)
const(char)* code =
"size_t h = 0;" ~
"foreach (i, T; typeof(p.tupleof))" ~
" h = h * 33 + typeid(T).getHash(cast(const void*)&p.tupleof[i]);" ~
" static if(is(T* : const(Object)*)) " ~
" h = h * 33 + typeid(const(Object)).getHash(cast(const void*)&p.tupleof[i]);" ~
" else " ~
" h = h * 33 + typeid(T).getHash(cast(const void*)&p.tupleof[i]);" ~
"return h;";
fop.fbody = new CompileStatement(loc, new StringExp(loc, cast(char*)code));
Scope* sc2 = sc.push();
Expand Down
12 changes: 12 additions & 0 deletions test/runnable/extra-files/test17968.d
@@ -0,0 +1,12 @@
import test17968a;

void main()
{
auto r = fun2.fun1;
// just check that getHash works (doesn't throw).
typeid(r).getHash(&r);

// try gethash when member is null.
r.t = null;
typeid(r).getHash(&r);
}
16 changes: 16 additions & 0 deletions test/runnable/extra-files/test17968a.d
@@ -0,0 +1,16 @@
struct S(T)
{
T t;
}

class C(T) { }

auto fun1(T)(T t)
{
return S!T(t);
}

auto fun2()
{
return new C!int;
}
9 changes: 9 additions & 0 deletions test/runnable/test17968.sh
@@ -0,0 +1,9 @@
#!/usr/bin/env bash


$DMD -m${MODEL} -I${EXTRA_FILES} -of${OUTPUT_BASE}a${OBJ} -c ${EXTRA_FILES}${SEP}test17968a.d
$DMD -m${MODEL} -I${EXTRA_FILES} -of${OUTPUT_BASE}${EXE} ${EXTRA_FILES}${SEP}test17968.d ${OUTPUT_BASE}a${OBJ}

${OUTPUT_BASE}${EXE}

rm ${OUTPUT_BASE}{a${OBJ},${EXE}}

0 comments on commit 6fefecf

Please sign in to comment.