Repository: goplus/gop
Pull request #2155 (2025-03-03 05:17:53)

cl: fix slice check type

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/goplus/gop/pull/2155?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 93.08%. Comparing base [(`38eb8ad`)](https://app.codecov.io/gh/goplus/gop/commit/38eb8adc3c21f188a37161ed395468533a441f6f?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus) to head [(`4e44e62`)](https://app.codecov.io/gh/goplus/gop/commit/4e44e6206da582bc74059e7536a99b9cab4aedc5?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus).
> Report is 27 commits behind head on main.

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##             main    #2155   +/-   ##
=======================================
  Coverage   93.08%   93.08%           
=======================================
  Files          34       33    -1     
  Lines       10967    10967           
=======================================
  Hits        10209    10209           
  Misses        603      603           
  Partials      155      155           
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/goplus/gop/pull/2155?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus).


Patch:
diff --git a/cl/expr.go b/cl/expr.go
index da4db1b8..68e86232 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 95a3df90..ede7ff34 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 {


Test patch:
diff --git a/cl/compile_gop_test.go b/cl/compile_gop_test.go
index d95d39f6..8b7e9d8e 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)
+}
+`)
+}