Skip to content

Commit aa2dc78

Browse files
committed
Yield explicit errors when encountering typeparams.
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.
1 parent 2db03cc commit aa2dc78

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

build/build.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ func parseAndAugment(xctx XContext, pkg *PackageData, isTest bool, fileSet *toke
279279
s := spec.(*ast.TypeSpec)
280280
if replacedDeclNames[s.Name.Name] {
281281
s.Name = ast.NewIdent("_")
282+
s.Type = &ast.StructType{Struct: s.Pos(), Fields: &ast.FieldList{}}
283+
s.TypeParams = nil
282284
}
283285
}
284286
case token.VAR, token.CONST:
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//go:build js
2+
3+
package doc
4+
5+
import (
6+
"fmt"
7+
"testing"
8+
)
9+
10+
func compareSlices(t *testing.T, name string, got, want interface{}, compareElem interface{}) {
11+
// TODO(nevkontakte): Remove this override after generics are supported.
12+
// https://github.com/gopherjs/gopherjs/issues/1013.
13+
switch got.(type) {
14+
case []*Func:
15+
got := got.([]*Func)
16+
want := want.([]*Func)
17+
compareElem := compareElem.(func(t *testing.T, msg string, got, want *Func))
18+
if len(got) != len(want) {
19+
t.Errorf("%s: got %d, want %d", name, len(got), len(want))
20+
}
21+
for i := 0; i < len(got) && i < len(want); i++ {
22+
compareElem(t, fmt.Sprintf("%s[%d]", name, i), got[i], want[i])
23+
}
24+
case []*Type:
25+
got := got.([]*Type)
26+
want := want.([]*Type)
27+
compareElem := compareElem.(func(t *testing.T, msg string, got, want *Type))
28+
if len(got) != len(want) {
29+
t.Errorf("%s: got %d, want %d", name, len(got), len(want))
30+
}
31+
for i := 0; i < len(got) && i < len(want); i++ {
32+
compareElem(t, fmt.Sprintf("%s[%d]", name, i), got[i], want[i])
33+
}
34+
default:
35+
t.Errorf("unexpected argument type %T", got)
36+
}
37+
}

compiler/natives/src/reflect/reflect_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,9 @@ func TestMethodCallValueCodePtr(t *testing.T) {
285285
t.Skip("methodValueCallCodePtr() is not applicable in GopherJS")
286286
}
287287

288+
type B struct{}
289+
290+
//gopherjs:prune-original
288291
func TestIssue50208(t *testing.T) {
289292
t.Skip("This test required generics, which are not yet supported: https://github.com/gopherjs/gopherjs/issues/1013")
290293
}

compiler/package.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"go/ast"
88
"go/constant"
9+
"go/scanner"
910
"go/token"
1011
"go/types"
1112
"sort"
@@ -398,6 +399,13 @@ func Compile(importPath string, files []*ast.File, fileSet *token.FileSet, impor
398399
var mainFunc *types.Func
399400
for _, fun := range functions {
400401
o := funcCtx.pkgCtx.Defs[fun.Name].(*types.Func)
402+
403+
if fun.Type.TypeParams.NumFields() > 0 {
404+
return nil, scanner.Error{
405+
Pos: fileSet.Position(fun.Type.TypeParams.Pos()),
406+
Msg: fmt.Sprintf("function %s: type parameters are not supported by GopherJS: https://github.com/gopherjs/gopherjs/issues/1013", o.Name()),
407+
}
408+
}
401409
funcInfo := funcCtx.pkgCtx.FuncDeclInfos[o]
402410
d := Decl{
403411
FullName: o.FullName(),
@@ -480,6 +488,14 @@ func Compile(importPath string, files []*ast.File, fileSet *token.FileSet, impor
480488
continue
481489
}
482490
typeName := funcCtx.objectName(o)
491+
492+
if named, ok := o.Type().(*types.Named); ok && named.TypeParams().Len() > 0 {
493+
return nil, scanner.Error{
494+
Pos: fileSet.Position(o.Pos()),
495+
Msg: fmt.Sprintf("type %s: type parameters are not supported by GopherJS: https://github.com/gopherjs/gopherjs/issues/1013", o.Name()),
496+
}
497+
}
498+
483499
d := Decl{
484500
Vars: []string{typeName},
485501
DceObjectFilter: o.Name(),

0 commit comments

Comments
 (0)