Skip to content

Commit 73cb9a1

Browse files
committed
all: refer to map elements as elements instead of values
The spec carefully and consistently uses "key" and "element" as map terminology. The implementation, not so much. This change attempts to make the implementation consistently hew to the spec's terminology. Beyond consistency, this has the advantage of avoid some confusion and naming collisions, since v and value are very generic and commonly used terms. I believe that I found all everything, but there are a lot of non-obvious places for these to hide, and grepping for them is hard. Hopefully this change changes enough of them that we will start using elem going forward. Any remaining hidden cases can be removed ad hoc as they are discovered. The only externally-facing part of this change is in package reflect, where there is a minor doc change and a function parameter name change. Updates #27167 Change-Id: I2f2d78f16c360dc39007b9966d5c2046a29d3701 Reviewed-on: https://go-review.googlesource.com/c/go/+/174523 Run-TryBot: Josh Bleecher Snyder <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent a8d0047 commit 73cb9a1

File tree

10 files changed

+222
-223
lines changed

10 files changed

+222
-223
lines changed

src/cmd/compile/internal/gc/range.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func typecheckrange(n *Node) {
1515
// Typechecking order is important here:
1616
// 0. first typecheck range expression (slice/map/chan),
1717
// it is evaluated only once and so logically it is not part of the loop.
18-
// 1. typcheck produced values,
18+
// 1. typecheck produced values,
1919
// this part can declare new vars and so it must be typechecked before body,
2020
// because body can contain a closure that captures the vars.
2121
// 2. decldepth++ to denote loop body.
@@ -298,8 +298,8 @@ func walkrange(n *Node) *Node {
298298
hit := prealloc[n]
299299
th := hit.Type
300300
n.Left = nil
301-
keysym := th.Field(0).Sym // depends on layout of iterator struct. See reflect.go:hiter
302-
valsym := th.Field(1).Sym // ditto
301+
keysym := th.Field(0).Sym // depends on layout of iterator struct. See reflect.go:hiter
302+
elemsym := th.Field(1).Sym // ditto
303303

304304
fn := syslook("mapiterinit")
305305

@@ -318,11 +318,11 @@ func walkrange(n *Node) *Node {
318318
} else if v2 == nil {
319319
body = []*Node{nod(OAS, v1, key)}
320320
} else {
321-
val := nodSym(ODOT, hit, valsym)
322-
val = nod(ODEREF, val, nil)
321+
elem := nodSym(ODOT, hit, elemsym)
322+
elem = nod(ODEREF, elem, nil)
323323
a := nod(OAS2, nil, nil)
324324
a.List.Set2(v1, v2)
325-
a.Rlist.Set2(key, val)
325+
a.Rlist.Set2(key, elem)
326326
body = []*Node{a}
327327
}
328328

src/cmd/compile/internal/gc/reflect.go

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ type Sig struct {
5656
// program for it.
5757
// Make sure this stays in sync with runtime/map.go.
5858
const (
59-
BUCKETSIZE = 8
60-
MAXKEYSIZE = 128
61-
MAXVALSIZE = 128
59+
BUCKETSIZE = 8
60+
MAXKEYSIZE = 128
61+
MAXELEMSIZE = 128
6262
)
6363

6464
func structfieldSize() int { return 3 * Widthptr } // Sizeof(runtime.structfield{})
@@ -86,14 +86,14 @@ func bmap(t *types.Type) *types.Type {
8686

8787
bucket := types.New(TSTRUCT)
8888
keytype := t.Key()
89-
valtype := t.Elem()
89+
elemtype := t.Elem()
9090
dowidth(keytype)
91-
dowidth(valtype)
91+
dowidth(elemtype)
9292
if keytype.Width > MAXKEYSIZE {
9393
keytype = types.NewPtr(keytype)
9494
}
95-
if valtype.Width > MAXVALSIZE {
96-
valtype = types.NewPtr(valtype)
95+
if elemtype.Width > MAXELEMSIZE {
96+
elemtype = types.NewPtr(elemtype)
9797
}
9898

9999
field := make([]*types.Field, 0, 5)
@@ -107,10 +107,10 @@ func bmap(t *types.Type) *types.Type {
107107
keys := makefield("keys", arr)
108108
field = append(field, keys)
109109

110-
arr = types.NewArray(valtype, BUCKETSIZE)
110+
arr = types.NewArray(elemtype, BUCKETSIZE)
111111
arr.SetNoalg(true)
112-
values := makefield("values", arr)
113-
field = append(field, values)
112+
elems := makefield("elems", arr)
113+
field = append(field, elems)
114114

115115
// Make sure the overflow pointer is the last memory in the struct,
116116
// because the runtime assumes it can use size-ptrSize as the
@@ -126,21 +126,21 @@ func bmap(t *types.Type) *types.Type {
126126
// will end with no padding.
127127
// On nacl/amd64p32, however, the max alignment is 64-bit,
128128
// but the overflow pointer will add only a 32-bit field,
129-
// so if the struct needs 64-bit padding (because a key or value does)
129+
// so if the struct needs 64-bit padding (because a key or elem does)
130130
// then it would end with an extra 32-bit padding field.
131131
// Preempt that by emitting the padding here.
132-
if int(valtype.Align) > Widthptr || int(keytype.Align) > Widthptr {
132+
if int(elemtype.Align) > Widthptr || int(keytype.Align) > Widthptr {
133133
field = append(field, makefield("pad", types.Types[TUINTPTR]))
134134
}
135135

136-
// If keys and values have no pointers, the map implementation
136+
// If keys and elems have no pointers, the map implementation
137137
// can keep a list of overflow pointers on the side so that
138138
// buckets can be marked as having no pointers.
139139
// Arrange for the bucket to have no pointers by changing
140140
// the type of the overflow field to uintptr in this case.
141141
// See comment on hmap.overflow in runtime/map.go.
142142
otyp := types.NewPtr(bucket)
143-
if !types.Haspointers(valtype) && !types.Haspointers(keytype) {
143+
if !types.Haspointers(elemtype) && !types.Haspointers(keytype) {
144144
otyp = types.Types[TUINTPTR]
145145
}
146146
overflow := makefield("overflow", otyp)
@@ -161,38 +161,38 @@ func bmap(t *types.Type) *types.Type {
161161
if keytype.Align > BUCKETSIZE {
162162
Fatalf("key align too big for %v", t)
163163
}
164-
if valtype.Align > BUCKETSIZE {
165-
Fatalf("value align too big for %v", t)
164+
if elemtype.Align > BUCKETSIZE {
165+
Fatalf("elem align too big for %v", t)
166166
}
167167
if keytype.Width > MAXKEYSIZE {
168168
Fatalf("key size to large for %v", t)
169169
}
170-
if valtype.Width > MAXVALSIZE {
171-
Fatalf("value size to large for %v", t)
170+
if elemtype.Width > MAXELEMSIZE {
171+
Fatalf("elem size to large for %v", t)
172172
}
173173
if t.Key().Width > MAXKEYSIZE && !keytype.IsPtr() {
174174
Fatalf("key indirect incorrect for %v", t)
175175
}
176-
if t.Elem().Width > MAXVALSIZE && !valtype.IsPtr() {
177-
Fatalf("value indirect incorrect for %v", t)
176+
if t.Elem().Width > MAXELEMSIZE && !elemtype.IsPtr() {
177+
Fatalf("elem indirect incorrect for %v", t)
178178
}
179179
if keytype.Width%int64(keytype.Align) != 0 {
180180
Fatalf("key size not a multiple of key align for %v", t)
181181
}
182-
if valtype.Width%int64(valtype.Align) != 0 {
183-
Fatalf("value size not a multiple of value align for %v", t)
182+
if elemtype.Width%int64(elemtype.Align) != 0 {
183+
Fatalf("elem size not a multiple of elem align for %v", t)
184184
}
185185
if bucket.Align%keytype.Align != 0 {
186186
Fatalf("bucket align not multiple of key align %v", t)
187187
}
188-
if bucket.Align%valtype.Align != 0 {
189-
Fatalf("bucket align not multiple of value align %v", t)
188+
if bucket.Align%elemtype.Align != 0 {
189+
Fatalf("bucket align not multiple of elem align %v", t)
190190
}
191191
if keys.Offset%int64(keytype.Align) != 0 {
192192
Fatalf("bad alignment of keys in bmap for %v", t)
193193
}
194-
if values.Offset%int64(valtype.Align) != 0 {
195-
Fatalf("bad alignment of values in bmap for %v", t)
194+
if elems.Offset%int64(elemtype.Align) != 0 {
195+
Fatalf("bad alignment of elems in bmap for %v", t)
196196
}
197197

198198
// Double-check that overflow field is final memory in struct,
@@ -270,7 +270,7 @@ func hiter(t *types.Type) *types.Type {
270270
// build a struct:
271271
// type hiter struct {
272272
// key *Key
273-
// val *Value
273+
// elem *Elem
274274
// t unsafe.Pointer // *MapType
275275
// h *hmap
276276
// buckets *bmap
@@ -287,8 +287,8 @@ func hiter(t *types.Type) *types.Type {
287287
// }
288288
// must match runtime/map.go:hiter.
289289
fields := []*types.Field{
290-
makefield("key", types.NewPtr(t.Key())), // Used in range.go for TMAP.
291-
makefield("val", types.NewPtr(t.Elem())), // Used in range.go for TMAP.
290+
makefield("key", types.NewPtr(t.Key())), // Used in range.go for TMAP.
291+
makefield("elem", types.NewPtr(t.Elem())), // Used in range.go for TMAP.
292292
makefield("t", types.Types[TUNSAFEPTR]),
293293
makefield("h", types.NewPtr(hmap)),
294294
makefield("buckets", types.NewPtr(bmap)),
@@ -1284,7 +1284,7 @@ func dtypesym(t *types.Type) *obj.LSym {
12841284
ot = duint8(lsym, ot, uint8(t.Key().Width))
12851285
}
12861286

1287-
if t.Elem().Width > MAXVALSIZE {
1287+
if t.Elem().Width > MAXELEMSIZE {
12881288
ot = duint8(lsym, ot, uint8(Widthptr))
12891289
flags |= 2 // indirect value
12901290
} else {
@@ -1894,7 +1894,7 @@ func (p *GCProg) emit(t *types.Type, offset int64) {
18941894
// size bytes of zeros.
18951895
func zeroaddr(size int64) *Node {
18961896
if size >= 1<<31 {
1897-
Fatalf("map value too big %d", size)
1897+
Fatalf("map elem too big %d", size)
18981898
}
18991899
if zerosize < size {
19001900
zerosize = size

src/cmd/compile/internal/gc/sinit.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -973,33 +973,33 @@ func maplit(n *Node, m *Node, init *Nodes) {
973973

974974
// build types [count]Tindex and [count]Tvalue
975975
tk := types.NewArray(n.Type.Key(), int64(len(stat)))
976-
tv := types.NewArray(n.Type.Elem(), int64(len(stat)))
976+
te := types.NewArray(n.Type.Elem(), int64(len(stat)))
977977

978978
// TODO(josharian): suppress alg generation for these types?
979979
dowidth(tk)
980-
dowidth(tv)
980+
dowidth(te)
981981

982982
// make and initialize static arrays
983983
vstatk := staticname(tk)
984984
vstatk.Name.SetReadonly(true)
985-
vstatv := staticname(tv)
986-
vstatv.Name.SetReadonly(true)
985+
vstate := staticname(te)
986+
vstate.Name.SetReadonly(true)
987987

988988
datak := nod(OARRAYLIT, nil, nil)
989-
datav := nod(OARRAYLIT, nil, nil)
989+
datae := nod(OARRAYLIT, nil, nil)
990990
for _, r := range stat {
991991
datak.List.Append(r.Left)
992-
datav.List.Append(r.Right)
992+
datae.List.Append(r.Right)
993993
}
994994
fixedlit(inInitFunction, initKindStatic, datak, vstatk, init)
995-
fixedlit(inInitFunction, initKindStatic, datav, vstatv, init)
995+
fixedlit(inInitFunction, initKindStatic, datae, vstate, init)
996996

997997
// loop adding structure elements to map
998998
// for i = 0; i < len(vstatk); i++ {
999-
// map[vstatk[i]] = vstatv[i]
999+
// map[vstatk[i]] = vstate[i]
10001000
// }
10011001
i := temp(types.Types[TINT])
1002-
rhs := nod(OINDEX, vstatv, i)
1002+
rhs := nod(OINDEX, vstate, i)
10031003
rhs.SetBounded(true)
10041004

10051005
kidx := nod(OINDEX, vstatk, i)
@@ -1035,28 +1035,28 @@ func addMapEntries(m *Node, dyn []*Node, init *Nodes) {
10351035
nerr := nerrors
10361036

10371037
// Build list of var[c] = expr.
1038-
// Use temporaries so that mapassign1 can have addressable key, val.
1038+
// Use temporaries so that mapassign1 can have addressable key, elem.
10391039
// TODO(josharian): avoid map key temporaries for mapfast_* assignments with literal keys.
1040-
key := temp(m.Type.Key())
1041-
val := temp(m.Type.Elem())
1040+
tmpkey := temp(m.Type.Key())
1041+
tmpelem := temp(m.Type.Elem())
10421042

10431043
for _, r := range dyn {
1044-
index, value := r.Left, r.Right
1044+
index, elem := r.Left, r.Right
10451045

10461046
setlineno(index)
1047-
a := nod(OAS, key, index)
1047+
a := nod(OAS, tmpkey, index)
10481048
a = typecheck(a, ctxStmt)
10491049
a = walkstmt(a)
10501050
init.Append(a)
10511051

1052-
setlineno(value)
1053-
a = nod(OAS, val, value)
1052+
setlineno(elem)
1053+
a = nod(OAS, tmpelem, elem)
10541054
a = typecheck(a, ctxStmt)
10551055
a = walkstmt(a)
10561056
init.Append(a)
10571057

1058-
setlineno(val)
1059-
a = nod(OAS, nod(OINDEX, m, key), val)
1058+
setlineno(tmpelem)
1059+
a = nod(OAS, nod(OINDEX, m, tmpkey), tmpelem)
10601060
a = typecheck(a, ctxStmt)
10611061
a = walkstmt(a)
10621062
init.Append(a)
@@ -1066,10 +1066,10 @@ func addMapEntries(m *Node, dyn []*Node, init *Nodes) {
10661066
}
10671067
}
10681068

1069-
a := nod(OVARKILL, key, nil)
1069+
a := nod(OVARKILL, tmpkey, nil)
10701070
a = typecheck(a, ctxStmt)
10711071
init.Append(a)
1072-
a = nod(OVARKILL, val, nil)
1072+
a = nod(OVARKILL, tmpelem, nil)
10731073
a = typecheck(a, ctxStmt)
10741074
init.Append(a)
10751075
}

src/cmd/compile/internal/gc/walk.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,7 @@ opswitch:
12101210
// Allocate one bucket pointed to by hmap.buckets on stack if hint
12111211
// is not larger than BUCKETSIZE. In case hint is larger than
12121212
// BUCKETSIZE runtime.makemap will allocate the buckets on the heap.
1213-
// Maximum key and value size is 128 bytes, larger objects
1213+
// Maximum key and elem size is 128 bytes, larger objects
12141214
// are stored with an indirection. So max bucket size is 2048+eps.
12151215
if !Isconst(hint, CTINT) ||
12161216
hint.Val().U.(*Mpint).CmpInt64(BUCKETSIZE) <= 0 {
@@ -2462,7 +2462,7 @@ var mapassign = mkmapnames("mapassign", "ptr")
24622462
var mapdelete = mkmapnames("mapdelete", "")
24632463

24642464
func mapfast(t *types.Type) int {
2465-
// Check runtime/map.go:maxValueSize before changing.
2465+
// Check runtime/map.go:maxElemSize before changing.
24662466
if t.Elem().Width > 128 {
24672467
return mapslow
24682468
}

src/reflect/value.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,7 @@ func (it *MapIter) Value() Value {
12371237

12381238
t := (*mapType)(unsafe.Pointer(it.m.typ))
12391239
vtype := t.elem
1240-
return copyVal(vtype, it.m.flag.ro()|flag(vtype.Kind()), mapitervalue(it.it))
1240+
return copyVal(vtype, it.m.flag.ro()|flag(vtype.Kind()), mapiterelem(it.it))
12411241
}
12421242

12431243
// Next advances the map iterator and reports whether there is another
@@ -1635,13 +1635,13 @@ func (v Value) SetCap(n int) {
16351635
s.Cap = n
16361636
}
16371637

1638-
// SetMapIndex sets the value associated with key in the map v to val.
1638+
// SetMapIndex sets the element associated with key in the map v to elem.
16391639
// It panics if v's Kind is not Map.
1640-
// If val is the zero Value, SetMapIndex deletes the key from the map.
1640+
// If elem is the zero Value, SetMapIndex deletes the key from the map.
16411641
// Otherwise if v holds a nil map, SetMapIndex will panic.
1642-
// As in Go, key's value must be assignable to the map's key type,
1643-
// and val's value must be assignable to the map's value type.
1644-
func (v Value) SetMapIndex(key, val Value) {
1642+
// As in Go, key's elem must be assignable to the map's key type,
1643+
// and elem's value must be assignable to the map's elem type.
1644+
func (v Value) SetMapIndex(key, elem Value) {
16451645
v.mustBe(Map)
16461646
v.mustBeExported()
16471647
key.mustBeExported()
@@ -1653,17 +1653,17 @@ func (v Value) SetMapIndex(key, val Value) {
16531653
} else {
16541654
k = unsafe.Pointer(&key.ptr)
16551655
}
1656-
if val.typ == nil {
1656+
if elem.typ == nil {
16571657
mapdelete(v.typ, v.pointer(), k)
16581658
return
16591659
}
1660-
val.mustBeExported()
1661-
val = val.assignTo("reflect.Value.SetMapIndex", tt.elem, nil)
1660+
elem.mustBeExported()
1661+
elem = elem.assignTo("reflect.Value.SetMapIndex", tt.elem, nil)
16621662
var e unsafe.Pointer
1663-
if val.flag&flagIndir != 0 {
1664-
e = val.ptr
1663+
if elem.flag&flagIndir != 0 {
1664+
e = elem.ptr
16651665
} else {
1666-
e = unsafe.Pointer(&val.ptr)
1666+
e = unsafe.Pointer(&elem.ptr)
16671667
}
16681668
mapassign(v.typ, v.pointer(), k, e)
16691669
}
@@ -2708,7 +2708,7 @@ func mapiterinit(t *rtype, m unsafe.Pointer) unsafe.Pointer
27082708
func mapiterkey(it unsafe.Pointer) (key unsafe.Pointer)
27092709

27102710
//go:noescape
2711-
func mapitervalue(it unsafe.Pointer) (value unsafe.Pointer)
2711+
func mapiterelem(it unsafe.Pointer) (elem unsafe.Pointer)
27122712

27132713
//go:noescape
27142714
func mapiternext(it unsafe.Pointer)

0 commit comments

Comments
 (0)