Skip to content

Commit

Permalink
Don't include package qualifier when type is in same package
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Sep 23, 2023
1 parent c986eda commit b9c58ed
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 50 deletions.
10 changes: 5 additions & 5 deletions compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ func (scope *scope) compileFuncLit(p *packages.Package, fn *ast.FuncLit, color *
}

func (scope *scope) compileFuncBody(p *packages.Package, typ *ast.FuncType, body *ast.BlockStmt, color *types.Signature) *ast.BlockStmt {
body = desugar(body, p.TypesInfo).(*ast.BlockStmt)
body = desugar(p.Types, body, p.TypesInfo).(*ast.BlockStmt)
body = astutil.Apply(body,
func(cursor *astutil.Cursor) bool {
switch n := cursor.Node().(type) {
Expand All @@ -416,8 +416,8 @@ func (scope *scope) compileFuncBody(p *packages.Package, typ *ast.FuncType, body
fp := ast.NewIdent("_fp")

yieldTypeExpr := make([]ast.Expr, 2)
yieldTypeExpr[0] = typeExpr(color.Params().At(0).Type())
yieldTypeExpr[1] = typeExpr(color.Results().At(0).Type())
yieldTypeExpr[0] = typeExpr(p.Types, color.Params().At(0).Type())
yieldTypeExpr[1] = typeExpr(p.Types, color.Results().At(0).Type())

// _c := coroutine.LoadContext[R, S]()
gen.List = append(gen.List, &ast.AssignStmt{
Expand Down Expand Up @@ -460,7 +460,7 @@ func (scope *scope) compileFuncBody(p *packages.Package, typ *ast.FuncType, body
// declarations to the function prologue. We downgrade inline var decls and
// assignments that use := to assignments that use =. Constant decls are
// hoisted and also have their value assigned in the function prologue.
decls := extractDecls(body, p.TypesInfo)
decls := extractDecls(p.Types, body, p.TypesInfo)
renameObjects(body, p.TypesInfo, decls, scope)
for _, decl := range decls {
gen.List = append(gen.List, &ast.DeclStmt{Decl: decl})
Expand Down Expand Up @@ -513,7 +513,7 @@ func (scope *scope) compileFuncBody(p *packages.Package, typ *ast.FuncType, body
Lhs: []ast.Expr{name},
Tok: token.ASSIGN,
Rhs: []ast.Expr{
&ast.TypeAssertExpr{X: value, Type: typeExpr(saveAndRestoreTypes[i])},
&ast.TypeAssertExpr{X: value, Type: typeExpr(p.Types, saveAndRestoreTypes[i])},
},
},
},
Expand Down
6 changes: 3 additions & 3 deletions compiler/decls.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
// Note that declarations are extracted from all nested scopes within the
// function body, so there may be duplicate identifiers. Identifiers can be
// disambiguated using (*types.Info).ObjectOf(ident).
func extractDecls(tree ast.Node, info *types.Info) (decls []*ast.GenDecl) {
func extractDecls(p *types.Package, tree ast.Node, info *types.Info) (decls []*ast.GenDecl) {
ast.Inspect(tree, func(node ast.Node) bool {
switch n := node.(type) {
case *ast.FuncLit:
Expand All @@ -46,7 +46,7 @@ func extractDecls(tree ast.Node, info *types.Info) (decls []*ast.GenDecl) {
Specs: []ast.Spec{
&ast.ValueSpec{
Names: []*ast.Ident{name},
Type: typeExpr(info.TypeOf(name)),
Type: typeExpr(p, info.TypeOf(name)),
},
},
})
Expand Down Expand Up @@ -80,7 +80,7 @@ func extractDecls(tree ast.Node, info *types.Info) (decls []*ast.GenDecl) {
Specs: []ast.Spec{
&ast.ValueSpec{
Names: []*ast.Ident{name},
Type: typeExpr(t),
Type: typeExpr(p, t),
},
},
})
Expand Down
9 changes: 5 additions & 4 deletions compiler/desugar.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ import (
// types.Info. If this gets unruly in the future, desugaring should be
// performed after parsing AST's but before type checking so that this is
// done automatically by the type checker.
func desugar(stmt ast.Stmt, info *types.Info) ast.Stmt {
d := desugarer{info: info}
func desugar(p *types.Package, stmt ast.Stmt, info *types.Info) ast.Stmt {
d := desugarer{pkg: p, info: info}
stmt = d.desugar(stmt, nil, nil, nil)

// Unused labels cause a compile error (label X defined and not used)
Expand All @@ -56,6 +56,7 @@ func desugar(stmt ast.Stmt, info *types.Info) ast.Stmt {
}

type desugarer struct {
pkg *types.Package
info *types.Info
vars int
labels int
Expand Down Expand Up @@ -253,7 +254,7 @@ func (d *desugarer) desugar(stmt ast.Stmt, breakTo, continueTo, userLabel *ast.I
&ast.CallExpr{
Fun: d.builtin("make"),
Args: []ast.Expr{
typeExpr(keySliceType),
typeExpr(d.pkg, keySliceType),
&ast.BasicLit{Kind: token.INT, Value: "0"},
&ast.CallExpr{Fun: d.builtin("len"), Args: []ast.Expr{x}},
},
Expand Down Expand Up @@ -385,7 +386,7 @@ func (d *desugarer) desugar(stmt ast.Stmt, breakTo, continueTo, userLabel *ast.I
Specs: []ast.Spec{
&ast.ValueSpec{
Names: []*ast.Ident{tmpLhs},
Type: typeExpr(lhsType),
Type: typeExpr(d.pkg, lhsType),
},
},
}})
Expand Down
4 changes: 2 additions & 2 deletions compiler/desugar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,7 @@ _l0:
}
return true
})
desugared := desugar(body, info)
desugared := desugar(nil, body, info)
desugared = unnestBlocks(desugared)

expect := strings.TrimSpace(test.expect)
Expand All @@ -1220,7 +1220,7 @@ _l0:

func formatNode(node ast.Node) string {
fset := token.NewFileSet()
//ast.Print(fset, node)
// ast.Print(fset, node)
var b bytes.Buffer
if err := format.Node(&b, fset, node); err != nil {
panic(err)
Expand Down
7 changes: 3 additions & 4 deletions compiler/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ func generateFunctypesInit(pkg *ssa.Package, fn *ssa.Function, init *ast.BlockSt
if fn.TypeParams() != nil {
return // ignore non-instantiated generic functions
}

var register ast.Expr
if len(fn.FreeVars) == 0 {
register = &ast.IndexListExpr{
Expand All @@ -63,7 +62,7 @@ func generateFunctypesInit(pkg *ssa.Package, fn *ssa.Function, init *ast.BlockSt
Sel: ast.NewIdent("RegisterFunc"),
},
Indices: []ast.Expr{
newFuncType(fn.Signature),
newFuncType(pkg.Pkg, fn.Signature),
},
}
} else {
Expand All @@ -77,7 +76,7 @@ func generateFunctypesInit(pkg *ssa.Package, fn *ssa.Function, init *ast.BlockSt
for i, freeVar := range fn.FreeVars {
fields[i+1] = &ast.Field{
Names: []*ast.Ident{ast.NewIdent(freeVar.Name())},
Type: typeExpr(freeVar.Type()),
Type: typeExpr(pkg.Pkg, freeVar.Type()),
}
}

Expand All @@ -87,7 +86,7 @@ func generateFunctypesInit(pkg *ssa.Package, fn *ssa.Function, init *ast.BlockSt
Sel: ast.NewIdent("RegisterClosure"),
},
Indices: []ast.Expr{
newFuncType(fn.Signature),
newFuncType(pkg.Pkg, fn.Signature),
&ast.StructType{
Fields: &ast.FieldList{
List: fields,
Expand Down
28 changes: 14 additions & 14 deletions compiler/testdata/http/coroc_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 17 additions & 18 deletions compiler/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strconv"
)

func typeExpr(typ types.Type) ast.Expr {
func typeExpr(p *types.Package, typ types.Type) ast.Expr {
switch t := typ.(type) {
case *types.Basic:
switch t {
Expand All @@ -18,22 +18,22 @@ func typeExpr(typ types.Type) ast.Expr {
}
return ast.NewIdent(t.String())
case *types.Slice:
return &ast.ArrayType{Elt: typeExpr(t.Elem())}
return &ast.ArrayType{Elt: typeExpr(p, t.Elem())}
case *types.Array:
return &ast.ArrayType{
Len: &ast.BasicLit{Kind: token.INT, Value: strconv.FormatInt(t.Len(), 10)},
Elt: typeExpr(t.Elem()),
Elt: typeExpr(p, t.Elem()),
}
case *types.Map:
return &ast.MapType{
Key: typeExpr(t.Key()),
Value: typeExpr(t.Elem()),
Key: typeExpr(p, t.Key()),
Value: typeExpr(p, t.Elem()),
}
case *types.Struct:
fields := make([]*ast.Field, t.NumFields())
for i := range fields {
f := t.Field(i)
fields[i] = &ast.Field{Type: typeExpr(f.Type())}
fields[i] = &ast.Field{Type: typeExpr(p, f.Type())}
if !f.Anonymous() {
fields[i].Names = []*ast.Ident{ast.NewIdent(f.Name())}
}
Expand All @@ -43,28 +43,27 @@ func typeExpr(typ types.Type) ast.Expr {
}
return &ast.StructType{Fields: &ast.FieldList{List: fields}}
case *types.Pointer:
return &ast.StarExpr{X: typeExpr(t.Elem())}
return &ast.StarExpr{X: typeExpr(p, t.Elem())}
case *types.Interface:
if t.Empty() {
return ast.NewIdent("any")
}
case *types.Signature:
return newFuncType(t)
return newFuncType(p, t)
case *types.Named:
if t.TypeParams() != nil || t.TypeArgs() != nil {
panic("not implemented: generic types")
}
obj := t.Obj()
name := ast.NewIdent(obj.Name())
pkg := obj.Pkg()
if pkg == nil {
if pkg == nil || p == pkg {
return name
}
// TODO: this needs to be incorporated in the pass to find imports
return &ast.SelectorExpr{X: ast.NewIdent(pkg.Name()), Sel: name}
case *types.Chan:
c := &ast.ChanType{
Value: typeExpr(t.Elem()),
Value: typeExpr(p, t.Elem()),
}
switch t.Dir() {
case types.SendRecv:
Expand All @@ -79,24 +78,24 @@ func typeExpr(typ types.Type) ast.Expr {
panic(fmt.Sprintf("not implemented: %T", typ))
}

func newFuncType(signature *types.Signature) *ast.FuncType {
func newFuncType(p *types.Package, signature *types.Signature) *ast.FuncType {
return &ast.FuncType{
Params: newFieldList(signature.Params()),
Results: newFieldList(signature.Results()),
Params: newFieldList(p, signature.Params()),
Results: newFieldList(p, signature.Results()),
}
}

func newFieldList(tuple *types.Tuple) *ast.FieldList {
func newFieldList(p *types.Package, tuple *types.Tuple) *ast.FieldList {
return &ast.FieldList{
List: newFields(tuple),
List: newFields(p, tuple),
}
}

func newFields(tuple *types.Tuple) []*ast.Field {
func newFields(p *types.Package, tuple *types.Tuple) []*ast.Field {
fields := make([]*ast.Field, tuple.Len())
for i := range fields {
fields[i] = &ast.Field{
Type: typeExpr(tuple.At(i).Type()),
Type: typeExpr(p, tuple.At(i).Type()),
}
}
return fields
Expand Down

0 comments on commit b9c58ed

Please sign in to comment.