]> Cypherpunks repositories - gostls13.git/commitdiff
fix unsafe.Sizeof("abc")
authorKen Thompson <ken@golang.org>
Tue, 17 Feb 2009 21:10:57 +0000 (13:10 -0800)
committerKen Thompson <ken@golang.org>
Tue, 17 Feb 2009 21:10:57 +0000 (13:10 -0800)
R=rsc
OCL=25105
CL=25105

src/cmd/gc/dcl.c

index 1f053b6114e01880837374ef4dbbabc282e6688c..fc977eba20b40c99de1d47f50a2b82e4ae296c95 100644 (file)
@@ -1525,7 +1525,7 @@ unsafenmagic(Node *l, Node *r)
 {
        Node *n;
        Sym *s;
-       Type *t;
+       Type *t, *tr;
        long v;
        Val val;
 
@@ -1541,9 +1541,12 @@ unsafenmagic(Node *l, Node *r)
 
        if(strcmp(s->name, "Sizeof") == 0) {
                walktype(r, Erv);
-               if(r->type == T)
+               tr = r->type;
+               if(r->op == OLITERAL && r->val.ctype == CTSTR)
+                       tr = types[TSTRING];
+               if(tr == T)
                        goto no;
-               v = r->type->width;
+               v = tr->width;
                goto yes;
        }
        if(strcmp(s->name, "Offsetof") == 0) {
@@ -1555,16 +1558,21 @@ unsafenmagic(Node *l, Node *r)
        }
        if(strcmp(s->name, "Alignof") == 0) {
                walktype(r, Erv);
-               if (r->type == T)
+               tr = r->type;
+               if(r->op == OLITERAL && r->val.ctype == CTSTR)
+                       tr = types[TSTRING];
+               if(tr == T)
                        goto no;
+
                // make struct { byte; T; }
                t = typ(TSTRUCT);
                t->type = typ(TFIELD);
                t->type->type = types[TUINT8];
                t->type->down = typ(TFIELD);
-               t->type->down->type = r->type;
+               t->type->down->type = tr;
                // compute struct widths
                dowidth(t);
+
                // the offset of T is its required alignment
                v = t->type->down->width;
                goto yes;