From 92c4af32b62aaa4a31e5c100ce245f4920151426 Mon Sep 17 00:00:00 2001 From: visualfc Date: Mon, 3 Mar 2025 13:17:20 +0800 Subject: [PATCH] cl: fix slice to any --- cl/compile_gop_test.go | 17 +++++++++++++++++ cl/expr.go | 6 +++--- cl/typeparams.go | 6 ++++-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/cl/compile_gop_test.go b/cl/compile_gop_test.go index d95d39f69..8b7e9d8ef 100644 --- a/cl/compile_gop_test.go +++ b/cl/compile_gop_test.go @@ -1689,3 +1689,20 @@ func main() { } `) } + +func TestSliceType(t *testing.T) { + gopClTest(t, ` +a := [1, "a"] +a[0] = [1, 2, 3] +echo a +`, `package main + +import "fmt" + +func main() { + a := []interface{}{1, "a"} + a[0] = []int{1, 2, 3} + fmt.Println(a) +} +`) +} diff --git a/cl/expr.go b/cl/expr.go index da4db1b88..68e862322 100644 --- a/cl/expr.go +++ b/cl/expr.go @@ -1335,10 +1335,10 @@ func compileSliceLit(ctx *blockCtx, v *ast.SliceLit, typ types.Type, noPanic ... for _, elt := range v.Elts { compileExpr(ctx, elt) } - if sliceHasTypeParam(ctx, typ) { - ctx.cb.SliceLitEx(nil, n, false, v) - } else { + if isSpecificSliceType(ctx, typ) { ctx.cb.SliceLitEx(typ, n, false, v) + } else { + ctx.cb.SliceLitEx(nil, n, false, v) } return } diff --git a/cl/typeparams.go b/cl/typeparams.go index 95a3df90c..ede7ff34d 100644 --- a/cl/typeparams.go +++ b/cl/typeparams.go @@ -255,7 +255,7 @@ func isTypeParam(t types.Type) bool { return ok } -func sliceHasTypeParam(ctx *blockCtx, typ types.Type) bool { +func isSpecificSliceType(ctx *blockCtx, typ types.Type) bool { if typ == nil { return false } @@ -265,9 +265,11 @@ func sliceHasTypeParam(ctx *blockCtx, typ types.Type) bool { t = getUnderlying(ctx, tt).(*types.Slice) case *types.Slice: t = tt + default: + return false } _, ok := t.Elem().(*types.TypeParam) - return ok + return !ok } func boundTypeParam(ctx *blockCtx, x ast.Expr) types.Type {