Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Update FuncKey to handle generics #1255

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

Merged
merged 1 commit into from
Dec 19, 2023
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
2 changes: 1 addition & 1 deletion build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ func (s *Session) BuildPackage(pkg *PackageData) (*compiler.Archive, error) {
archive := s.buildCache.LoadArchive(pkg.ImportPath)
if archive != nil && !pkg.SrcModTime.After(archive.BuildTime) {
if err := archive.RegisterTypes(s.Types); err != nil {
panic(fmt.Errorf("Failed to load type information from %v: %w", archive, err))
panic(fmt.Errorf("failed to load type information from %v: %w", archive, err))
}
s.UpToDateArchives[pkg.ImportPath] = archive
// Existing archive is up to date, no need to build it from scratch.
Expand Down
33 changes: 28 additions & 5 deletions compiler/astutil/astutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,37 @@ func ImportsUnsafe(file *ast.File) bool {
// FuncKey returns a string, which uniquely identifies a top-level function or
// method in a package.
func FuncKey(d *ast.FuncDecl) string {
if d.Recv == nil || len(d.Recv.List) == 0 {
return d.Name.Name
if recvKey := FuncReceiverKey(d); len(recvKey) > 0 {
return recvKey + "." + d.Name.Name
}
return d.Name.Name
}

// FuncReceiverKey returns a string that uniquely identifies the receiver
// struct of the function or an empty string if there is no receiver.
// This name will match the name of the struct in the struct's type spec.
func FuncReceiverKey(d *ast.FuncDecl) string {
if d == nil || d.Recv == nil || len(d.Recv.List) == 0 {
return ``
}
recv := d.Recv.List[0].Type
if star, ok := recv.(*ast.StarExpr); ok {
recv = star.X
for {
switch r := recv.(type) {
case *ast.IndexListExpr:
recv = r.X
continue
case *ast.IndexExpr:
recv = r.X
continue
case *ast.StarExpr:
recv = r.X
continue
case *ast.Ident:
return r.Name
default:
panic(fmt.Errorf(`unexpected type %T in receiver of function: %v`, recv, d))
}
}
return recv.(*ast.Ident).Name + "." + d.Name.Name
}

// PruneOriginal returns true if gopherjs:prune-original directive is present
Expand Down
39 changes: 28 additions & 11 deletions compiler/astutil/astutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,41 @@ func TestFuncKey(t *testing.T) {
want string
}{
{
desc: "top-level function",
src: `package testpackage; func foo() {}`,
want: "foo",
desc: `top-level function`,
src: `func foo() {}`,
want: `foo`,
}, {
desc: `top-level exported function`,
src: `func Foo() {}`,
want: `Foo`,
}, {
desc: `method on reference`,
src: `func (_ myType) bar() {}`,
want: `myType.bar`,
}, {
desc: "top-level exported function",
src: `package testpackage; func Foo() {}`,
want: "Foo",
desc: `method on pointer`,
src: ` func (_ *myType) bar() {}`,
want: `myType.bar`,
}, {
desc: "method",
src: `package testpackage; func (_ myType) bar() {}`,
want: "myType.bar",
desc: `method on generic reference`,
src: ` func (_ myType[T]) bar() {}`,
want: `myType.bar`,
}, {
desc: `method on generic pointer`,
src: ` func (_ *myType[T]) bar() {}`,
want: `myType.bar`,
}, {
desc: `method on struct with multiple generics`,
src: ` func (_ *myType[T1, T2, T3, T4]) bar() {}`,
want: `myType.bar`,
},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
fdecl := srctesting.ParseFuncDecl(t, test.src)
src := `package testpackage; ` + test.src
fdecl := srctesting.ParseFuncDecl(t, src)
if got := FuncKey(fdecl); got != test.want {
t.Errorf("Got %q, want %q", got, test.want)
t.Errorf(`Got %q, want %q`, got, test.want)
}
})
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/natives/src/net/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"bufio"
"bytes"
"errors"
"io/ioutil"
"io"
"net/textproto"
"strconv"

Expand Down Expand Up @@ -68,7 +68,7 @@ func (t *XHRTransport) RoundTrip(req *Request) (*Response, error) {
StatusCode: xhr.Get("status").Int(),
Header: Header(header),
ContentLength: contentLength,
Body: ioutil.NopCloser(bytes.NewReader(body)),
Body: io.NopCloser(bytes.NewReader(body)),
Request: req,
}
})
Expand All @@ -91,7 +91,7 @@ func (t *XHRTransport) RoundTrip(req *Request) (*Response, error) {
if req.Body == nil {
xhr.Call("send")
} else {
body, err := ioutil.ReadAll(req.Body)
body, err := io.ReadAll(req.Body)
if err != nil {
req.Body.Close() // RoundTrip must always close the body, including on errors.
return nil, err
Expand Down