]> Cypherpunks repositories - gostls13.git/commitdiff
gc: allow use of unsafe.Pointer in generated code
authorRuss Cox <rsc@golang.org>
Tue, 20 Dec 2011 21:25:57 +0000 (16:25 -0500)
committerRuss Cox <rsc@golang.org>
Tue, 20 Dec 2011 21:25:57 +0000 (16:25 -0500)
The functions we generate to implement == on structs
or arrays may need to refer to unsafe.Pointer even in
safe mode, in order to handle unexported fields contained
in other packages' structs.

R=ken2
CC=golang-dev
https://golang.org/cl/5505046

src/cmd/gc/subr.c

index 07f97da2de073f725e34d70f199c3ff3ec99dcdf..96616d88ee1d9862d95b30c7eb70817246131d2f 100644 (file)
@@ -2487,6 +2487,7 @@ genhash(Sym *sym, Type *t)
        Node *n, *fn, *np, *nh, *ni, *call, *nx, *na, *tfn;
        Node *hashel;
        Type *first, *t1;
+       int old_safemode;
        int64 size;
 
        if(debug['r'])
@@ -2616,7 +2617,16 @@ genhash(Sym *sym, Type *t)
        typecheck(&fn, Etop);
        typechecklist(fn->nbody, Etop);
        curfn = nil;
+
+       // Disable safemode while compiling this code: the code we
+       // generate internally can refer to unsafe.Pointer.
+       // In this case it can happen if we need to generate an ==
+       // for a struct containing a reflect.Value, which itself has
+       // an unexported field of type unsafe.Pointer.
+       old_safemode = safemode;
+       safemode = 0;
        funccompile(fn, 0);
+       safemode = old_safemode;
 }
 
 // Return node for
@@ -2694,6 +2704,7 @@ geneq(Sym *sym, Type *t)
 {
        Node *n, *fn, *np, *neq, *nq, *tfn, *nif, *ni, *nx, *ny, *nrange;
        Type *t1, *first;
+       int old_safemode;
        int64 size;
 
        if(debug['r'])
@@ -2814,7 +2825,16 @@ geneq(Sym *sym, Type *t)
        typecheck(&fn, Etop);
        typechecklist(fn->nbody, Etop);
        curfn = nil;
+       
+       // Disable safemode while compiling this code: the code we
+       // generate internally can refer to unsafe.Pointer.
+       // In this case it can happen if we need to generate an ==
+       // for a struct containing a reflect.Value, which itself has
+       // an unexported field of type unsafe.Pointer.
+       old_safemode = safemode;
+       safemode = 0;
        funccompile(fn, 0);
+       safemode = old_safemode;
 }
 
 static Type*