Skip to content

Commit 438b59c

Browse files
authored
Merge pull request #1579 from visualfc/typesutil_gop3
cl: compileLambdaExpr types record define names
2 parents 8d774f8 + d04b34b commit 438b59c

File tree

2 files changed

+159
-1
lines changed

2 files changed

+159
-1
lines changed

Diff for: cl/expr.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,9 @@ func compileLambdaExpr(ctx *blockCtx, v *ast.LambdaExpr, sig *types.Signature) {
688688
params := makeLambdaParams(ctx, v.Pos(), v.Lhs, sig.Params())
689689
results := makeLambdaResults(pkg, sig.Results())
690690
ctx.cb.NewClosure(params, results, false).BodyStart(pkg)
691+
if len(v.Lhs) > 0 {
692+
defNames(ctx, v.Lhs, ctx.cb.Scope())
693+
}
691694
for _, v := range v.Rhs {
692695
compileExpr(ctx, v)
693696
}
@@ -700,7 +703,12 @@ func compileLambdaExpr2(ctx *blockCtx, v *ast.LambdaExpr2, sig *types.Signature)
700703
results := makeLambdaResults(pkg, sig.Results())
701704
comments, once := ctx.cb.BackupComments()
702705
fn := ctx.cb.NewClosure(params, results, false)
703-
loadFuncBody(ctx, fn, v.Body, v)
706+
cb := fn.BodyStart(ctx.pkg, v.Body)
707+
if len(v.Lhs) > 0 {
708+
defNames(ctx, v.Lhs, cb.Scope())
709+
}
710+
compileStmts(ctx, v.Body.List)
711+
cb.End(v)
704712
ctx.cb.SetComments(comments, once)
705713
}
706714

Diff for: x/typesutil/info_test.go

+150
Original file line numberDiff line numberDiff line change
@@ -729,3 +729,153 @@ for line <- os.Stdin {
729729
002: 5: 2 | println | builtin println
730730
003: 5:10 | line | var line string`)
731731
}
732+
733+
func TestLambdaExpr(t *testing.T) {
734+
testGopInfo(t, `package main
735+
func Map(c []float64, t func(float64) float64) {
736+
// ...
737+
}
738+
739+
func Map2(c []float64, t func(float64) (float64, float64)) {
740+
// ...
741+
}
742+
743+
Map([1.2, 3.5, 6], x => x * x)
744+
Map2([1.2, 3.5, 6], x => (x * x, x + x))
745+
`, ``, `== types ==
746+
000: 2:12 | []float64 *ast.ArrayType | type : []float64 | type
747+
001: 2:14 | float64 *ast.Ident | type : float64 | type
748+
002: 2:25 | func(float64) float64 *ast.FuncType | type : func(float64) float64 | type
749+
003: 2:30 | float64 *ast.Ident | type : float64 | type
750+
004: 2:39 | float64 *ast.Ident | type : float64 | type
751+
005: 6:13 | []float64 *ast.ArrayType | type : []float64 | type
752+
006: 6:15 | float64 *ast.Ident | type : float64 | type
753+
007: 6:26 | func(float64) (float64, float64) *ast.FuncType | type : func(float64) (float64, float64) | type
754+
008: 6:31 | float64 *ast.Ident | type : float64 | type
755+
009: 6:41 | float64 *ast.Ident | type : float64 | type
756+
010: 6:50 | float64 *ast.Ident | type : float64 | type
757+
011: 10: 1 | Map *ast.Ident | value : func(c []float64, t func(float64) float64) | value
758+
012: 10: 1 | Map([1.2, 3.5, 6], x => x * x) *ast.CallExpr | void : () | no value
759+
013: 10: 6 | 1.2 *ast.BasicLit | value : untyped float = 1.2 | constant
760+
014: 10:11 | 3.5 *ast.BasicLit | value : untyped float = 3.5 | constant
761+
015: 10:16 | 6 *ast.BasicLit | value : untyped int = 6 | constant
762+
016: 10:25 | x *ast.Ident | var : float64 | variable
763+
017: 10:25 | x * x *ast.BinaryExpr | value : float64 | value
764+
018: 10:29 | x *ast.Ident | var : float64 | variable
765+
019: 11: 1 | Map2 *ast.Ident | value : func(c []float64, t func(float64) (float64, float64)) | value
766+
020: 11: 1 | Map2([1.2, 3.5, 6], x => (x * x, x + x)) *ast.CallExpr | void : () | no value
767+
021: 11: 7 | 1.2 *ast.BasicLit | value : untyped float = 1.2 | constant
768+
022: 11:12 | 3.5 *ast.BasicLit | value : untyped float = 3.5 | constant
769+
023: 11:17 | 6 *ast.BasicLit | value : untyped int = 6 | constant
770+
024: 11:27 | x *ast.Ident | var : float64 | variable
771+
025: 11:27 | x * x *ast.BinaryExpr | value : float64 | value
772+
026: 11:31 | x *ast.Ident | var : float64 | variable
773+
027: 11:34 | x *ast.Ident | var : float64 | variable
774+
028: 11:34 | x + x *ast.BinaryExpr | value : float64 | value
775+
029: 11:38 | x *ast.Ident | var : float64 | variable
776+
== defs ==
777+
000: 2: 6 | Map | func main.Map(c []float64, t func(float64) float64)
778+
001: 2:10 | c | var c []float64
779+
002: 2:23 | t | var t func(float64) float64
780+
003: 6: 6 | Map2 | func main.Map2(c []float64, t func(float64) (float64, float64))
781+
004: 6:11 | c | var c []float64
782+
005: 6:24 | t | var t func(float64) (float64, float64)
783+
006: 10: 1 | main | func main.main()
784+
007: 10:20 | x | var x float64
785+
008: 11:21 | x | var x float64
786+
== uses ==
787+
000: 2:14 | float64 | type float64
788+
001: 2:30 | float64 | type float64
789+
002: 2:39 | float64 | type float64
790+
003: 6:15 | float64 | type float64
791+
004: 6:31 | float64 | type float64
792+
005: 6:41 | float64 | type float64
793+
006: 6:50 | float64 | type float64
794+
007: 10: 1 | Map | func main.Map(c []float64, t func(float64) float64)
795+
008: 10:25 | x | var x float64
796+
009: 10:29 | x | var x float64
797+
010: 11: 1 | Map2 | func main.Map2(c []float64, t func(float64) (float64, float64))
798+
011: 11:27 | x | var x float64
799+
012: 11:31 | x | var x float64
800+
013: 11:34 | x | var x float64
801+
014: 11:38 | x | var x float64`)
802+
}
803+
804+
func TestLambdaExpr2(t *testing.T) {
805+
testGopInfo(t, `package main
806+
func Map(c []float64, t func(float64) float64) {
807+
// ...
808+
}
809+
810+
func Map2(c []float64, t func(float64) (float64, float64)) {
811+
// ...
812+
}
813+
814+
Map([1.2, 3.5, 6], x => {
815+
return x * x
816+
})
817+
Map2([1.2, 3.5, 6], x => {
818+
return x * x, x + x
819+
})
820+
`, ``, `== types ==
821+
000: 2:12 | []float64 *ast.ArrayType | type : []float64 | type
822+
001: 2:14 | float64 *ast.Ident | type : float64 | type
823+
002: 2:25 | func(float64) float64 *ast.FuncType | type : func(float64) float64 | type
824+
003: 2:30 | float64 *ast.Ident | type : float64 | type
825+
004: 2:39 | float64 *ast.Ident | type : float64 | type
826+
005: 6:13 | []float64 *ast.ArrayType | type : []float64 | type
827+
006: 6:15 | float64 *ast.Ident | type : float64 | type
828+
007: 6:26 | func(float64) (float64, float64) *ast.FuncType | type : func(float64) (float64, float64) | type
829+
008: 6:31 | float64 *ast.Ident | type : float64 | type
830+
009: 6:41 | float64 *ast.Ident | type : float64 | type
831+
010: 6:50 | float64 *ast.Ident | type : float64 | type
832+
011: 10: 1 | Map *ast.Ident | value : func(c []float64, t func(float64) float64) | value
833+
012: 10: 1 | Map([1.2, 3.5, 6], x => {
834+
return x * x
835+
}) *ast.CallExpr | void : () | no value
836+
013: 10: 6 | 1.2 *ast.BasicLit | value : untyped float = 1.2 | constant
837+
014: 10:11 | 3.5 *ast.BasicLit | value : untyped float = 3.5 | constant
838+
015: 10:16 | 6 *ast.BasicLit | value : untyped int = 6 | constant
839+
016: 11: 9 | x *ast.Ident | var : float64 | variable
840+
017: 11: 9 | x * x *ast.BinaryExpr | value : float64 | value
841+
018: 11:13 | x *ast.Ident | var : float64 | variable
842+
019: 13: 1 | Map2 *ast.Ident | value : func(c []float64, t func(float64) (float64, float64)) | value
843+
020: 13: 1 | Map2([1.2, 3.5, 6], x => {
844+
return x * x, x + x
845+
}) *ast.CallExpr | void : () | no value
846+
021: 13: 7 | 1.2 *ast.BasicLit | value : untyped float = 1.2 | constant
847+
022: 13:12 | 3.5 *ast.BasicLit | value : untyped float = 3.5 | constant
848+
023: 13:17 | 6 *ast.BasicLit | value : untyped int = 6 | constant
849+
024: 14: 9 | x *ast.Ident | var : float64 | variable
850+
025: 14: 9 | x * x *ast.BinaryExpr | value : float64 | value
851+
026: 14:13 | x *ast.Ident | var : float64 | variable
852+
027: 14:16 | x *ast.Ident | var : float64 | variable
853+
028: 14:16 | x + x *ast.BinaryExpr | value : float64 | value
854+
029: 14:20 | x *ast.Ident | var : float64 | variable
855+
== defs ==
856+
000: 2: 6 | Map | func main.Map(c []float64, t func(float64) float64)
857+
001: 2:10 | c | var c []float64
858+
002: 2:23 | t | var t func(float64) float64
859+
003: 6: 6 | Map2 | func main.Map2(c []float64, t func(float64) (float64, float64))
860+
004: 6:11 | c | var c []float64
861+
005: 6:24 | t | var t func(float64) (float64, float64)
862+
006: 10: 1 | main | func main.main()
863+
007: 10:20 | x | var x float64
864+
008: 13:21 | x | var x float64
865+
== uses ==
866+
000: 2:14 | float64 | type float64
867+
001: 2:30 | float64 | type float64
868+
002: 2:39 | float64 | type float64
869+
003: 6:15 | float64 | type float64
870+
004: 6:31 | float64 | type float64
871+
005: 6:41 | float64 | type float64
872+
006: 6:50 | float64 | type float64
873+
007: 10: 1 | Map | func main.Map(c []float64, t func(float64) float64)
874+
008: 11: 9 | x | var x float64
875+
009: 11:13 | x | var x float64
876+
010: 13: 1 | Map2 | func main.Map2(c []float64, t func(float64) (float64, float64))
877+
011: 14: 9 | x | var x float64
878+
012: 14:13 | x | var x float64
879+
013: 14:16 | x | var x float64
880+
014: 14:20 | x | var x float64`)
881+
}

0 commit comments

Comments
 (0)