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

cmd/go2go: anonymous struct type as type parameter causes panic #39737

Closed
DeedleFake opened this issue Jun 21, 2020 · 3 comments
Closed

cmd/go2go: anonymous struct type as type parameter causes panic #39737

DeedleFake opened this issue Jun 21, 2020 · 3 comments

Comments

@DeedleFake
Copy link

Playground

package main

import (
	"encoding/json"
	"fmt"
	"io"
	"strings"
)

func Decode(type T)(r io.Reader) (T, error) {
	var t T
	err := json.NewDecoder(r).Decode(&t)
	return t, err
}

func main() {
	t, err := Decode(struct {
		This string `json:"this"`
		A    string `json:"a"`
	})(strings.NewReader(`{"this": "is", "a": "test"}`))
	if err != nil {
		panic(err)
	}
	fmt.Printf("%#v", t)
}

Output:

panic: prog.go2:17:12: unexpected type string character '"' in "struct{This string \"json:\\\"this\\\"\"; A string \"json:\\\"a\\\"\"}"

goroutine 1 [running]:
go/go2go.(*translator).instantiatedName(0xc0003f19b0, 0x0, 0xc00009a6a0, 0xc00001c4b0, 0x1, 0x1, 0xc0003f1158, 0x403518, 0x652e80, 0xc0003f1158)
	/usr/local/go-faketime/src/go/go2go/names.go:61 +0x72b
go/go2go.(*translator).instantiateFunction(0xc0003f19b0, 0x0, 0xc00009a6a0, 0xc0000826a0, 0x1, 0x1, 0xc00001c4b0, 0x1, 0x1, 0xc0003f1200, ...)
	/usr/local/go-faketime/src/go/go2go/instantiate.go:94 +0x85
go/go2go.(*translator).translateFunctionInstantiation(0xc0003f19b0, 0xc0000ae680)
	/usr/local/go-faketime/src/go/go2go/rewrite.go:734 +0x3ea
go/go2go.(*translator).translateExpr(0xc0003f19b0, 0xc0000ae680)
	/usr/local/go-faketime/src/go/go2go/rewrite.go:614 +0x3e5
go/go2go.(*translator).translateExpr(0xc0003f19b0, 0xc0000826e0)
	/usr/local/go-faketime/src/go/go2go/rewrite.go:619 +0x398
go/go2go.(*translator).translateExprList(0xc0003f19b0, 0xc0000826e0, 0x1, 0x1)
	/usr/local/go-faketime/src/go/go2go/rewrite.go:693 +0x46
go/go2go.(*translator).translateStmt(0xc0003f19b0, 0xc0000ae7c0)
	/usr/local/go-faketime/src/go/go2go/rewrite.go:494 +0x29d
go/go2go.(*translator).translateBlockStmt(0xc0003f19b0, 0xc000084e40)
	/usr/local/go-faketime/src/go/go2go/rewrite.go:455 +0x52
go/go2go.(*translator).translateFuncDecl(0xc0003f19b0, 0xc0000ae820)
	/usr/local/go-faketime/src/go/go2go/rewrite.go:448 +0xc5
go/go2go.(*translator).translate(0xc0003f19b0, 0xc0000c2200)
	/usr/local/go-faketime/src/go/go2go/rewrite.go:376 +0x391
go/go2go.rewriteAST(0xc0000ae240, 0xc0000b1360, 0x0, 0x0, 0xc0000b1720, 0xc0000c2200, 0x1, 0xc0000b1310, 0xc0000b1720)
	/usr/local/go-faketime/src/go/go2go/rewrite.go:188 +0x109
go/go2go.RewriteBuffer(0xc0000b1360, 0x7fff700f3dd8, 0x1e, 0xc0000f8000, 0x17f, 0x37f, 0x0, 0xc000084a20, 0xc0000849f0, 0xc0000849c0, ...)
	/usr/local/go-faketime/src/go/go2go/go2go.go:132 +0x2c5
main.translateFile(0xc0000b1360, 0x7fff700f3dd8, 0x1e)
	/usr/local/go-faketime/src/cmd/go2go/translate.go:26 +0xa9
main.main()
	/usr/local/go-faketime/src/cmd/go2go/main.go:64 +0x2ea
@DeedleFake
Copy link
Author

DeedleFake commented Jun 21, 2020

This seems to be related to #39643, but it's still happening for this case.

@gopherbot
Copy link

Change https://golang.org/cl/239379 mentions this issue: [dev.go2go] go/go2go: ignore struct tags

@ianlancetaylor
Copy link
Contributor

Thanks. This is now fixed in the dev.go2go branch.

gopherbot pushed a commit that referenced this issue Jun 22, 2020
Ignore struct tags when generating an instantiated names, and when
comparing types for identity.

Fixes #39737

Change-Id: I53e67fa1d09214dafc4088a59eb00e1a6f18ef3d
Reviewed-on: https://go-review.googlesource.com/c/go/+/239379
Reviewed-by: Ian Lance Taylor <iant@golang.org>
@golang golang locked and limited conversation to collaborators Jun 22, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants