Skip to content

Commit

Permalink
Yield explicit errors when encountering typeparams.
Browse files Browse the repository at this point in the history
Even though the compiler was usually able to compile them into
_something_, the code was likely incorrect in all cases. To prevent
users from tripping up on that the compiler will return an error if it
encounters type params until
gopherjs#1013 is resolved.
  • Loading branch information
nevkontakte committed Jul 31, 2022
1 parent 44a730e commit 2452ec0
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ func parseAndAugment(xctx XContext, pkg *PackageData, isTest bool, fileSet *toke
s := spec.(*ast.TypeSpec)
if replacedDeclNames[s.Name.Name] {
s.Name = ast.NewIdent("_")
s.Type = &ast.StructType{Struct: s.Pos(), Fields: &ast.FieldList{}}
s.TypeParams = nil
}
}
case token.VAR, token.CONST:
Expand Down
37 changes: 37 additions & 0 deletions compiler/natives/src/go/doc/doc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//go:build js

package doc

import (
"fmt"
"testing"
)

func compareSlices(t *testing.T, name string, got, want interface{}, compareElem interface{}) {
// TODO(nevkontakte): Remove this override after generics are supported.
// https://github.com/gopherjs/gopherjs/issues/1013.
switch got.(type) {
case []*Func:
got := got.([]*Func)
want := want.([]*Func)
compareElem := compareElem.(func(t *testing.T, msg string, got, want *Func))
if len(got) != len(want) {
t.Errorf("%s: got %d, want %d", name, len(got), len(want))
}
for i := 0; i < len(got) && i < len(want); i++ {
compareElem(t, fmt.Sprintf("%s[%d]", name, i), got[i], want[i])
}
case []*Type:
got := got.([]*Type)
want := want.([]*Type)
compareElem := compareElem.(func(t *testing.T, msg string, got, want *Type))
if len(got) != len(want) {
t.Errorf("%s: got %d, want %d", name, len(got), len(want))
}
for i := 0; i < len(got) && i < len(want); i++ {
compareElem(t, fmt.Sprintf("%s[%d]", name, i), got[i], want[i])
}
default:
t.Errorf("unexpected argument type %T", got)
}
}
3 changes: 3 additions & 0 deletions compiler/natives/src/reflect/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ func TestMethodCallValueCodePtr(t *testing.T) {
t.Skip("methodValueCallCodePtr() is not applicable in GopherJS")
}

type B struct{}

//gopherjs:prune-original
func TestIssue50208(t *testing.T) {
t.Skip("This test required generics, which are not yet supported: https://github.com/gopherjs/gopherjs/issues/1013")
}
16 changes: 16 additions & 0 deletions compiler/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"go/ast"
"go/constant"
"go/scanner"
"go/token"
"go/types"
"sort"
Expand Down Expand Up @@ -398,6 +399,13 @@ func Compile(importPath string, files []*ast.File, fileSet *token.FileSet, impor
var mainFunc *types.Func
for _, fun := range functions {
o := funcCtx.pkgCtx.Defs[fun.Name].(*types.Func)

if fun.Type.TypeParams.NumFields() > 0 {
return nil, scanner.Error{
Pos: fileSet.Position(fun.Type.TypeParams.Pos()),
Msg: fmt.Sprintf("function %s: type parameters are not supported by GopherJS: https://github.com/gopherjs/gopherjs/issues/1013", o.Name()),
}
}
funcInfo := funcCtx.pkgCtx.FuncDeclInfos[o]
d := Decl{
FullName: o.FullName(),
Expand Down Expand Up @@ -480,6 +488,14 @@ func Compile(importPath string, files []*ast.File, fileSet *token.FileSet, impor
continue
}
typeName := funcCtx.objectName(o)

if named, ok := o.Type().(*types.Named); ok && named.TypeParams().Len() > 0 {
return nil, scanner.Error{
Pos: fileSet.Position(o.Pos()),
Msg: fmt.Sprintf("type %s: type parameters are not supported by GopherJS: https://github.com/gopherjs/gopherjs/issues/1013", o.Name()),
}
}

d := Decl{
Vars: []string{typeName},
DceObjectFilter: o.Name(),
Expand Down

0 comments on commit 2452ec0

Please sign in to comment.