Repository: goplus/gop
Pull request #2157 (2025-03-04 02:40:10)

cl: fix mapLit to any

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/goplus/gop/pull/2157?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus) Report
Attention: Patch coverage is `72.72727%` with `3 lines` in your changes missing coverage. Please review.
> Project coverage is 92.99%. Comparing base [(`92c4af3`)](https://app.codecov.io/gh/goplus/gop/commit/92c4af32b62aaa4a31e5c100ce245f4920151426?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus) to head [(`6086c07`)](https://app.codecov.io/gh/goplus/gop/commit/6086c0778f955f26ac956eeeb8756493636dba21?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus).
> Report is 2 commits behind head on main.

| [Files with missing lines](https://app.codecov.io/gh/goplus/gop/pull/2157?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus) | Patch % | Lines |
|---|---|---|
| [cl/expr.go](https://app.codecov.io/gh/goplus/gop/pull/2157?src=pr&el=tree&filepath=cl%2Fexpr.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus#diff-Y2wvZXhwci5nbw==) | 72.72% | [2 Missing and 1 partial :warning: ](https://app.codecov.io/gh/goplus/gop/pull/2157?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus) |

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


```diff
@@            Coverage Diff             @@
##             main    #2157      +/-   ##
==========================================
- Coverage   93.08%   92.99%   -0.10%     
==========================================
  Files          34       31       -3     
  Lines       10969    10944      -25     
==========================================
- Hits        10211    10177      -34     
- Misses        603      611       +8     
- Partials      155      156       +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/goplus/gop/pull/2157?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 68e86232..09d04435 100644
--- a/cl/expr.go
+++ b/cl/expr.go
@@ -1277,22 +1277,25 @@ func compileCompositeLitEx(ctx *blockCtx, v *ast.CompositeLit, expected types.Ty
 			return err
 		}
 		n := len(v.Elts)
-		if isMap(underlying) {
+		switch underlying.(type) {
+		case *types.Slice:
+			ctx.cb.SliceLitEx(typ, n<<kind, kind == compositeLitKeyVal, v)
+		case *types.Array:
+			ctx.cb.ArrayLitEx(typ, n<<kind, kind == compositeLitKeyVal, v)
+		case *types.Map:
 			if kind == compositeLitVal && n > 0 {
 				return ctx.newCodeError(v.Pos(), "missing key in map literal")
 			}
-			if err := ctx.cb.MapLitEx(typ, n<<1, v); err != nil {
+			if err := compileMapLitEx(ctx, typ, n, v); err != nil {
 				return err
 			}
-		} else {
-			switch underlying.(type) {
-			case *types.Slice:
-				ctx.cb.SliceLitEx(typ, n<<kind, kind == compositeLitKeyVal, v)
-			case *types.Array:
-				ctx.cb.ArrayLitEx(typ, n<<kind, kind == compositeLitKeyVal, v)
-			default:
+		default:
+			if kind == compositeLitVal && n > 0 {
 				return ctx.newCodeErrorf(v.Pos(), "invalid composite literal type %v", typ)
 			}
+			if err := compileMapLitEx(ctx, nil, n, v); err != nil {
+				return err
+			}
 		}
 	}
 	if hasPtr {
@@ -1305,12 +1308,14 @@ func compileCompositeLitEx(ctx *blockCtx, v *ast.CompositeLit, expected types.Ty
 	return nil
 }
 
-func isMap(tu types.Type) bool {
-	if tu == nil { // map can omit type
-		return true
-	}
-	_, ok := tu.(*types.Map)
-	return ok
+func compileMapLitEx(ctx *blockCtx, typ types.Type, n int, v *ast.CompositeLit) (err error) {
+	defer func() {
+		if e := recover(); e != nil {
+			err = ctx.newCodeError(v.Pos(), "invalid map literal")
+		}
+	}()
+	err = ctx.cb.MapLitEx(typ, n<<1, v)
+	return
 }
 
 func isMapOrStruct(tu types.Type) bool {


Test patch:
diff --git a/cl/compile_gop_test.go b/cl/compile_gop_test.go
index 8b7e9d8e..d6fa13e0 100644
--- a/cl/compile_gop_test.go
+++ b/cl/compile_gop_test.go
@@ -1706,3 +1706,22 @@ func main() {
 }
 `)
 }
+
+func TestMapLitType(t *testing.T) {
+	gopClTest(t, `
+var a any = {
+    "Monday": 1,
+    "Tuesday": 2,
+}
+echo a
+`, `package main
+
+import "fmt"
+
+var a interface{} = map[string]int{"Monday": 1, "Tuesday": 2}
+
+func main() {
+	fmt.Println(a)
+}
+`)
+}
diff --git a/cl/error_msg_test.go b/cl/error_msg_test.go
index a9553ee3..10cb6b76 100644
--- a/cl/error_msg_test.go
+++ b/cl/error_msg_test.go
@@ -603,6 +603,12 @@ bar.gop:3:27: cannot use "Go" + "+" (type untyped string) as type int in map val
 		`
 a := map[string]int{1+2: 2}
 b := map[string]int{"Hi": "Go" + "+"}
+`)
+	codeErrorTest(t, `bar.gop:2:13: invalid map literal`, `
+var v any = {1:2,1}
+`)
+	codeErrorTest(t, `bar.gop:2:21: invalid map literal`, `
+var v map[int]int = {1:2,1}
 `)
 }