Skip to content

Several small fixes to generics handling #1170

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 3 commits into from
Oct 31, 2022
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
3 changes: 3 additions & 0 deletions compiler/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,9 @@ func (fc *funcContext) translateExpr(expr ast.Expr) *expression {
if typesutil.IsJsObject(exprType) {
return fc.formatExpr("null")
}
if typesutil.IsGeneric(exprType) {
return fc.formatExpr("%s.zero()", fc.typeName(exprType))
}
switch t := exprType.Underlying().(type) {
case *types.Basic:
if t.Kind() != types.UnsafePointer {
Expand Down
2 changes: 1 addition & 1 deletion compiler/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ func Compile(importPath string, files []*ast.File, fileSet *token.FileSet, impor
d.DeclCode = funcCtx.CatchOutput(0, func() {
typeName := funcCtx.objectName(o)
lhs := typeName
if typeVarLevel(o) == varPackage {
if getVarLevel(o) == varPackage {
lhs += " = $pkg." + encodeIdent(o.Name())
}
size := int64(0)
Expand Down
21 changes: 15 additions & 6 deletions compiler/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,17 @@ func isVarOrConst(o types.Object) bool {
return false
}

func typeVarLevel(o types.Object) varLevel {
if _, ok := o.Type().(*types.TypeParam); ok {
func isTypeParameterName(o types.Object) bool {
_, isTypeName := o.(*types.TypeName)
_, isTypeParam := o.Type().(*types.TypeParam)
return isTypeName && isTypeParam
}

// getVarLevel returns at which level a JavaScript variable for the given object
// should be defined. The object can represent any named Go object: variable,
// type, function, etc.
func getVarLevel(o types.Object) varLevel {
if isTypeParameterName(o) {
return varGenericFactory
}
if o.Parent() != nil && o.Parent().Parent() == types.Universe {
Expand All @@ -381,7 +390,7 @@ func typeVarLevel(o types.Object) varLevel {
// objectName returns a JS identifier corresponding to the given types.Object.
// Repeated calls for the same object will return the same name.
func (fc *funcContext) objectName(o types.Object) string {
if typeVarLevel(o) == varPackage {
if getVarLevel(o) == varPackage {
fc.pkgCtx.dependencies[o] = true

if o.Pkg() != fc.pkgCtx.Pkg || (isVarOrConst(o) && o.Exported()) {
Expand All @@ -391,7 +400,7 @@ func (fc *funcContext) objectName(o types.Object) string {

name, ok := fc.pkgCtx.objectNames[o]
if !ok {
name = fc.newVariable(o.Name(), typeVarLevel(o))
name = fc.newVariable(o.Name(), getVarLevel(o))
fc.pkgCtx.objectNames[o] = name
}

Expand All @@ -402,13 +411,13 @@ func (fc *funcContext) objectName(o types.Object) string {
}

func (fc *funcContext) varPtrName(o *types.Var) string {
if typeVarLevel(o) == varPackage && o.Exported() {
if getVarLevel(o) == varPackage && o.Exported() {
return fc.pkgVar(o.Pkg()) + "." + o.Name() + "$ptr"
}

name, ok := fc.pkgCtx.varPtrNames[o]
if !ok {
name = fc.newVariable(o.Name()+"$ptr", typeVarLevel(o))
name = fc.newVariable(o.Name()+"$ptr", getVarLevel(o))
fc.pkgCtx.varPtrNames[o] = name
}
return name
Expand Down
2 changes: 1 addition & 1 deletion tests/gorepo/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ func (t *test) run() {
supportedArgs := []string{}
for _, a := range args {
switch a {
case "-gcflags=-G=3":
case "-gcflags=-G=3", `-gcflags="-G=3"`:
continue
default:
supportedArgs = append(supportedArgs, a)
Expand Down