Skip to content

An alternate approach to generics support #1272

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 30 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
59e4d76
Add typeparam test cases to the gorepo test suite.
nevkontakte Dec 2, 2023
82b84ff
Do not fail compilation when encountering generics.
nevkontakte Dec 2, 2023
9118679
Implement a new internal library for discovering generic instances.
nevkontakte Dec 17, 2023
af64433
Instantiate generic functions for each discovered set of type args.
nevkontakte Dec 24, 2023
f1a7c2a
Type parameter substitution when translating function instances.
nevkontakte Jan 4, 2024
ae5a9c2
Explicitly generate method instances for generic names types.
nevkontakte Jan 4, 2024
cdddbe2
Implement generic type instantiation.
nevkontakte Jan 5, 2024
b65288d
Re-triage failing test cases.
nevkontakte Jan 5, 2024
8f8d041
Correctly detect type conversions to generic types.
nevkontakte Jan 5, 2024
eff58dc
Correctly handle all integer types in funcContext.formatExpr().
nevkontakte Jan 5, 2024
2ca8aab
Discover generic type declarations inside generic functions.
nevkontakte Jan 5, 2024
aea4239
Perform type substitution for method selection expressions.
nevkontakte Jan 20, 2024
16af5b4
Fix variable name allocation for inline types.
nevkontakte Jan 20, 2024
b888728
Fix pointer-taking of an escaping variable.
nevkontakte Jan 21, 2024
77b5223
Re-triage remaining typeparam test failures.
nevkontakte Jan 21, 2024
ecc7d5f
Fix instance discovery for generic types embedded in structs.
nevkontakte Jan 21, 2024
7a384fd
Deduplicate collected typeNames within a package.
nevkontakte Jan 28, 2024
13fa377
Use types.Object as a key when grouping instances by object.
nevkontakte Feb 3, 2024
fe2849a
Assign unique numeric IDs to generic instances in the generated code.
nevkontakte Feb 18, 2024
49c773a
Include type arguments into generic type strings.
nevkontakte Feb 24, 2024
5a7c2d0
Fix variable name assignment for result variables.
nevkontakte Feb 24, 2024
61de589
Perform type substitution in the type switch expression.
nevkontakte Feb 24, 2024
0a51510
Implement builtins Sizeof(), Alignof() and Offsetof().
nevkontakte Feb 24, 2024
5aaeafb
Merge master with Go 1.19 support.
nevkontakte Feb 24, 2024
0343336
Borrow type substitution logic from x/tools/go/ssa package.
nevkontakte Feb 24, 2024
fb19fae
Update run.go to match the current set of passing tests.
nevkontakte Feb 24, 2024
deb26d0
Diagnose fixedbugs/issue50672.go issue.
nevkontakte Feb 25, 2024
8a80f06
Gofumpt the files to keep the linter happy.
nevkontakte Feb 25, 2024
b288e83
Address pull request comments.
nevkontakte Mar 2, 2024
2ce9bec
Merge remote-tracking branch 'upstream/generics-ng' into generics-ng
nevkontakte Mar 2, 2024
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
6 changes: 4 additions & 2 deletions compiler/analysis/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/gopherjs/gopherjs/compiler/astutil"
"github.com/gopherjs/gopherjs/compiler/typesutil"
"golang.org/x/exp/typeparams"
)

type continueStmt struct {
Expand Down Expand Up @@ -342,8 +343,8 @@ func (fi *FuncInfo) visitCallExpr(n *ast.CallExpr) ast.Visitor {
return nil // No need to walk under this CallExpr, we already did it manually.
default:
if astutil.IsTypeExpr(f, fi.pkgInfo.Info) {
// This is a type assertion, not a call. Type assertion itself is not
// blocking, but we will visit the expression itself.
// This is a type conversion, not a call. Type assertion itself is not
// blocking, but we will visit the input expression.
} else {
// The function is returned by a non-trivial expression. We have to be
// conservative and assume that function might be blocking.
Expand All @@ -357,6 +358,7 @@ func (fi *FuncInfo) visitCallExpr(n *ast.CallExpr) ast.Visitor {
func (fi *FuncInfo) callToNamedFunc(callee types.Object) {
switch o := callee.(type) {
case *types.Func:
o = typeparams.OriginMethod(o) // TODO(nevkontakte): Can be replaced with o.Origin() in Go 1.19.
if recv := o.Type().(*types.Signature).Recv(); recv != nil {
if _, ok := recv.Type().Underlying().(*types.Interface); ok {
// Conservatively assume that an interface implementation may be blocking.
Expand Down
22 changes: 22 additions & 0 deletions compiler/astutil/astutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ func NewIdent(name string, t types.Type, info *types.Info, pkg *types.Package) *
return ident
}

// IsTypeExpr returns true if expr denotes a type. This can be used to
// distinguish between calls and type conversions.
func IsTypeExpr(expr ast.Expr, info *types.Info) bool {
// Note that we could've used info.Types[expr].IsType() instead of doing our
// own analysis. However, that creates a problem because we synthesize some
// *ast.CallExpr nodes and, more importantly, *ast.Ident nodes that denote a
// type. Unfortunately, because the flag that controls
// types.TypeAndValue.IsType() return value is unexported we wouldn't be able
// to set it correctly. Thus, we can't rely on IsType().
switch e := expr.(type) {
case *ast.ArrayType, *ast.ChanType, *ast.FuncType, *ast.InterfaceType, *ast.MapType, *ast.StructType:
return true
Expand All @@ -47,6 +55,20 @@ func IsTypeExpr(expr ast.Expr, info *types.Info) bool {
case *ast.SelectorExpr:
_, ok := info.Uses[e.Sel].(*types.TypeName)
return ok
case *ast.IndexExpr:
ident, ok := e.X.(*ast.Ident)
if !ok {
return false
}
_, ok = info.Uses[ident].(*types.TypeName)
return ok
case *ast.IndexListExpr:
ident, ok := e.X.(*ast.Ident)
if !ok {
return false
}
_, ok = info.Uses[ident].(*types.TypeName)
return ok
case *ast.ParenExpr:
return IsTypeExpr(e.X, info)
default:
Expand Down
11 changes: 8 additions & 3 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"strings"
"time"

"github.com/gopherjs/gopherjs/compiler/internal/symbol"
"github.com/gopherjs/gopherjs/compiler/prelude"
"golang.org/x/tools/go/gcexportdata"
)
Expand Down Expand Up @@ -112,9 +113,13 @@ type Decl struct {
// Go compiler/linker toolchain. Used by GopherJS to support go:linkname
// directives. Must be set for decls that are supported by go:linkname
// implementation.
LinkingName SymName
LinkingName symbol.Name
// A list of package-level JavaScript variable names this symbol needs to declare.
Vars []string
// A JS expression by which the object represented by this decl may be
// referenced within the package context. Empty if the decl represents no such
// object.
RefExpr string
// NamedRecvType is method named recv declare.
NamedRecvType string
// JavaScript code that declares basic information about a symbol. For a type
Expand Down Expand Up @@ -326,7 +331,7 @@ func WritePkgCode(pkg *Archive, dceSelection map[*Decl]struct{}, gls goLinknameS
if recv, method, ok := d.LinkingName.IsMethod(); ok {
code = fmt.Sprintf("\t$linknames[%q] = $unsafeMethodToFunction(%v,%q,%t);\n", d.LinkingName.String(), d.NamedRecvType, method, strings.HasPrefix(recv, "*"))
} else {
code = fmt.Sprintf("\t$linknames[%q] = %s;\n", d.LinkingName.String(), d.Vars[0])
code = fmt.Sprintf("\t$linknames[%q] = %s;\n", d.LinkingName.String(), d.RefExpr)
}
if _, err := w.Write(removeWhitespace([]byte(code), minify)); err != nil {
return err
Expand Down Expand Up @@ -357,7 +362,7 @@ func WritePkgCode(pkg *Archive, dceSelection map[*Decl]struct{}, gls goLinknameS
if !found {
continue // The symbol is not affected by a go:linkname directive.
}
lines = append(lines, fmt.Sprintf("\t\t%s = $linknames[%q];\n", d.Vars[0], impl.String()))
lines = append(lines, fmt.Sprintf("\t\t%s = $linknames[%q];\n", d.RefExpr, impl.String()))
}
if len(lines) > 0 {
code := fmt.Sprintf("\t$pkg.$initLinknames = function() {\n%s};\n", strings.Join(lines, ""))
Expand Down
Loading