Skip to content
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

types record support command call for overload #1594

Merged
merged 3 commits into from
Dec 31, 2023
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
30 changes: 30 additions & 0 deletions cl/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5090,3 +5090,33 @@ func main() {
}
`)
}

func TestMixedOverloadCommand(t *testing.T) {
gopMixedClTest(t, "main", `package main

func Test__0() {
}
func Test__1(n int) {
}
type N struct {
}
func (p *N) Test__0() {
}
func (p *N) Test__1(n int) {
}`, `
Test
Test 100
var n N
n.test
n.test 100
`, `package main

func main() {
Test__0()
Test__1(100)
var n N
n.Test__0()
n.Test__1(100)
}
`)
}
11 changes: 7 additions & 4 deletions cl/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,20 +435,23 @@ func compilePkgRef(ctx *blockCtx, at *gox.PkgRef, x *ast.Ident, flags, pkgKind i
if v, alias := lookupPkgRef(ctx, at, x, pkgKind); v != nil {
cb := ctx.cb
if (flags & clIdentLHS) != 0 {
if rec := ctx.recorder(); rec != nil {
rec.Use(x, v)
}
cb.VarRef(v, x)
} else {
autoprop := alias && (flags&clIdentCanAutoCall) != 0
if autoprop && !gox.HasAutoProperty(v.Type()) {
return false
}
if rec := ctx.recorder(); rec != nil {
rec.Use(x, v)
}
cb.Val(v, x)
if autoprop {
cb.Call(0)
cb.CallWith(0, 0, x)
}
}
if rec := ctx.recorder(); rec != nil {
rec.Use(x, v)
}
return true
}
return false
Expand Down
6 changes: 6 additions & 0 deletions cl/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ func (p *goxRecorder) Member(id ast.Node, obj types.Object) {

func (p *goxRecorder) Call(id ast.Node, obj types.Object) {
switch v := id.(type) {
case *ast.Ident:
p.rec.Use(v, obj)
p.rec.Type(v, typesutil.NewTypeAndValueForObject(obj))
case *ast.SelectorExpr:
p.rec.Use(v.Sel, obj)
p.rec.Type(v, typesutil.NewTypeAndValueForObject(obj))
case *ast.CallExpr:
switch id := v.Fun.(type) {
case *ast.Ident:
Expand Down
2 changes: 1 addition & 1 deletion cl/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func compileStmt(ctx *blockCtx, stmt ast.Stmt) {
}
compileExpr(ctx, v.X, inFlags)
if inFlags != 0 && gox.IsFunc(ctx.cb.InternalStack().Get(-1).Type) {
ctx.cb.Call(0)
ctx.cb.CallWith(0, 0, v.X)
}
case *ast.AssignStmt:
compileAssignStmt(ctx, v)
Expand Down
45 changes: 45 additions & 0 deletions x/typesutil/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1340,3 +1340,48 @@ func OnKey__a(a, b string, v ...int) {
030: 33: 1 | n | var n *main.N
031: 33: 3 | onKey | func (*main.N).OnKey__8(x int, y int)`)
}

func TestMixedOverload3(t *testing.T) {
testGopInfo(t, `
Test
Test 100
var n N
n.test
n.test 100
`, `
package main

func Test__0() {
}
func Test__1(n int) {
}
type N struct {
}
func (p *N) Test__0() {
}
func (p *N) Test__1(n int) {
}
`, `== types ==
000: 2: 1 | Test *ast.Ident | value : func() | value
001: 3: 1 | Test *ast.Ident | value : func(n int) | value
002: 3: 1 | Test 100 *ast.CallExpr | void : () | no value
003: 3: 6 | 100 *ast.BasicLit | value : untyped int = 100 | constant
004: 4: 7 | N *ast.Ident | type : main.N | type
005: 5: 1 | n *ast.Ident | var : main.N | variable
006: 5: 1 | n.test *ast.SelectorExpr | value : func() | value
007: 6: 1 | n *ast.Ident | var : main.N | variable
008: 6: 1 | n.test *ast.SelectorExpr | value : func(n int) | value
009: 6: 1 | n.test 100 *ast.CallExpr | void : () | no value
010: 6: 8 | 100 *ast.BasicLit | value : untyped int = 100 | constant
== defs ==
000: 2: 1 | main | func main.main()
001: 4: 5 | n | var n main.N
== uses ==
000: 2: 1 | Test | func main.Test__0()
001: 3: 1 | Test | func main.Test__1(n int)
002: 4: 7 | N | type main.N struct{}
003: 5: 1 | n | var n main.N
004: 5: 3 | test | func (*main.N).Test__0()
005: 6: 1 | n | var n main.N
006: 6: 3 | test | func (*main.N).Test__1(n int)`)
}