Skip to content

Commit 9b9311f

Browse files
ssa: fix zero-value map indexing
Represent zero-value maps as null *runtime.Map pointers and avoid constant-extracting array elements from nil-backed values. This keeps zero-value map indexing on the normal load path and emits MapAccess1 with a nil map pointer instead of a zero-initialized hmap value. Add a focused cl/_testlibgo/mapzero case covering the runtime output and the FileCheck expectation. Signed-off-by: ZhouGuangyuan <[email protected]>
1 parent b4d9167 commit 9b9311f

4 files changed

Lines changed: 18 additions & 4 deletions

File tree

cl/_testlibgo/mapzero/expect.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
runtime error: index out of range

cl/_testlibgo/mapzero/in.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// LITTEST
2+
package main
3+
4+
import "fmt"
5+
6+
var a = 0
7+
8+
func main() {
9+
defer func() {
10+
err := recover()
11+
fmt.Println(err)
12+
}()
13+
// CHECK: call ptr @"github.com/goplus/llgo/runtime/internal/runtime.MapAccess1"(ptr @"map[_llgo_int]_llgo_int", ptr null, ptr %21)
14+
m := [0]map[int]int{}[a][0]
15+
print(m)
16+
}

ssa/datastruct.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,6 @@ func (b Builder) Index(x, idx Expr, takeAddr func() (addr Expr, zero bool)) Expr
284284
return prog.Zero(telem)
285285
}
286286
if ptr.IsNil() {
287-
if x.impl.IsConstant() {
288-
return Expr{llvm.ConstExtractElement(x.impl, idx.impl), telem}
289-
}
290287
ptr = b.Alloc(x.Type, false)
291288
b.impl.CreateStore(x.impl, ptr.impl)
292289
}

ssa/expr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func (p Program) Zero(t Type) Expr {
163163
}
164164
ret = p.Zero(p.rtType(name)).impl
165165
case *types.Map:
166-
ret = p.Zero(p.rtType("Map")).impl
166+
ret = p.Zero(p.Pointer(p.rtType("Map"))).impl
167167
case *types.Tuple:
168168
n := u.Len()
169169
flds := make([]llvm.Value, n)

0 commit comments

Comments
 (0)