diff options
author | Ken Thompson <ken@golang.org> | 2009-02-17 13:10:57 -0800 |
---|---|---|
committer | Ken Thompson <ken@golang.org> | 2009-02-17 13:10:57 -0800 |
commit | b2f877ddfa0d19942741d03e7af3e3e6efedd7fe (patch) | |
tree | c0793cd9761bc88a1a9d2e3b1e649f002d59db99 /src | |
parent | 80975a24e255e78a906ff795b95a3d314aaa5656 (diff) | |
download | golang-b2f877ddfa0d19942741d03e7af3e3e6efedd7fe.tar.gz |
fix unsafe.Sizeof("abc")
R=rsc
OCL=25105
CL=25105
Diffstat (limited to 'src')
-rw-r--r-- | src/cmd/gc/dcl.c | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/src/cmd/gc/dcl.c b/src/cmd/gc/dcl.c index 1f053b611..fc977eba2 100644 --- a/src/cmd/gc/dcl.c +++ b/src/cmd/gc/dcl.c @@ -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; |