Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cl: fix mapLit to any #2157

Merged
merged 1 commit into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions cl/compile_gop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
`)
}
6 changes: 6 additions & 0 deletions cl/error_msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
`)
}

Expand Down
35 changes: 20 additions & 15 deletions cl/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down