From c1e28d51885a45624107ac36d8c3ad9561a4b447 Mon Sep 17 00:00:00 2001 From: visualfc Date: Thu, 1 Aug 2024 21:56:02 +0800 Subject: [PATCH] cl: fix compositeLit for assignStmt --- cl/compile_test.go | 30 ++++++++++++++++++++++++++++++ cl/stmt.go | 6 ++++++ 2 files changed, 36 insertions(+) diff --git a/cl/compile_test.go b/cl/compile_test.go index c70734e8f..4d7930ccd 100644 --- a/cl/compile_test.go +++ b/cl/compile_test.go @@ -4163,3 +4163,33 @@ func main() { } `) } + +func TestCompositeLitAssign(t *testing.T) { + gopClTest(t, ` +var a map[any]any = {10: "A", 3.14: "B", 200: "C"} +var b map[any]string = {10: "A", 3.14: "B", 200: "C"} +echo a +echo b +var n int +n, a = 1, {10: "A", 3.14: "B", 200: "C"} +echo a, n +n, b = 1, {10: "A", 3.14: "B", 200: "C"} +echo b, n +`, `package main + +import "fmt" + +var a map[interface{}]interface{} = map[interface{}]interface{}{10: "A", 3.14: "B", 200: "C"} +var b map[interface{}]string = map[interface{}]string{10: "A", 3.14: "B", 200: "C"} + +func main() { + fmt.Println(a) + fmt.Println(b) + var n int + n, a = 1, map[interface{}]interface{}{10: "A", 3.14: "B", 200: "C"} + fmt.Println(a, n) + n, b = 1, map[interface{}]string{10: "A", 3.14: "B", 200: "C"} + fmt.Println(b, n) +} +`) +} diff --git a/cl/stmt.go b/cl/stmt.go index 771fa33a2..fc3d05f74 100644 --- a/cl/stmt.go +++ b/cl/stmt.go @@ -313,6 +313,12 @@ func compileAssignStmt(ctx *blockCtx, expr *ast.AssignStmt) { typ, _ = gogen.DerefType(ctx.cb.Get(-1 - i).Type) } compileSliceLit(ctx, e, typ) + case *ast.CompositeLit: + var typ types.Type + if len(expr.Lhs) == len(expr.Rhs) { + typ, _ = gogen.DerefType(ctx.cb.Get(-1 - i).Type) + } + compileCompositeLit(ctx, e, typ, false) default: compileExpr(ctx, rhs, inFlags) }