Skip to content

Commit d28af7a

Browse files
committed
cl: compileErrWrapExpr remove autoCall (#2638)
1 parent 6155935 commit d28af7a

File tree

6 files changed

+50
-112
lines changed

6 files changed

+50
-112
lines changed

cl/_testgop/dql2/in.xgo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import "github.com/goplus/xgo/cl/internal/dql"
22

33
doc := dql.new
4-
name := doc.users@($age?:100 < 18).$name
4+
name := doc.users@($age?:100 < 18).$name!
55
echo name

cl/_testgop/dql2/out.go

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,37 @@ package main
33
import (
44
"fmt"
55
"github.com/goplus/xgo/cl/internal/dql"
6+
"github.com/qiniu/x/errors"
67
)
78

89
func main() {
910
doc := dql.New()
10-
name := dql.NodeSet_Cast(func(_xgo_yield func(*dql.Node) bool) {
11-
doc.XGo_Elem("users").XGo_Enum()(func(self dql.NodeSet) bool {
12-
if func() (_xgo_ret int) {
13-
var _xgo_err error
14-
_xgo_ret, _xgo_err = self.XGo_Attr__1("age")
15-
if _xgo_err != nil {
16-
return 100
17-
}
18-
return
19-
}() < 18 {
20-
if _xgo_val, _xgo_err := self.XGo_first(); _xgo_err == nil {
21-
if !_xgo_yield(_xgo_val) {
22-
return false
11+
name := func() (_xgo_ret int) {
12+
var _xgo_err error
13+
_xgo_ret, _xgo_err = dql.NodeSet_Cast(func(_xgo_yield func(*dql.Node) bool) {
14+
doc.XGo_Elem("users").XGo_Enum()(func(self dql.NodeSet) bool {
15+
if func() (_xgo_ret int) {
16+
var _xgo_err error
17+
_xgo_ret, _xgo_err = self.XGo_Attr__1("age")
18+
if _xgo_err != nil {
19+
return 100
20+
}
21+
return
22+
}() < 18 {
23+
if _xgo_val, _xgo_err := self.XGo_first(); _xgo_err == nil {
24+
if !_xgo_yield(_xgo_val) {
25+
return false
26+
}
2327
}
2428
}
25-
}
26-
return true
27-
})
28-
}).XGo_Attr__0("name")
29+
return true
30+
})
31+
}).XGo_Attr__1("name")
32+
if _xgo_err != nil {
33+
_xgo_err = errors.NewFrame(_xgo_err, "doc.users@($age?:100 < 18).$name", "cl/_testgop/dql2/in.xgo", 4, "main.main")
34+
panic(_xgo_err)
35+
}
36+
return
37+
}()
2938
fmt.Println(name)
3039
}

cl/builtin_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func TestCompileFuncAlias(t *testing.T) {
193193
}
194194
scope := types.NewScope(nil, 0, 0, "")
195195
x := ast.NewIdent("foo")
196-
if compileFuncAlias(ctx, scope, x, 0) {
196+
if compileFuncAlias(ctx, 0, scope, x, 0) {
197197
t.Fatal("compileFuncAlias: ok?")
198198
}
199199
}

cl/compile_spx_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ Greem.t4spx:1:1: cannot use (type *Greem) as type github.com/goplus/xgo/cl/inte
119119
"/foo/Greem.t4spx": ``,
120120
})
121121

122-
gopSpxErrorTestMap(t, `Game.t4gmx:1:9: assignment mismatch: 2 variables but this.BackdropName returns 1 values`, map[string][]string{
122+
gopSpxErrorTestMap(t, `Game.t4gmx:1:9: cannot use backdropName (type string) as type error in assignment`, map[string][]string{
123123
"/foo": {"Game.t4gmx"},
124124
}, map[string]string{
125125
"/foo/Game.t4gmx": `println backdropName!`,

cl/compile_test.go

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4099,72 +4099,6 @@ func main() {
40994099
`)
41004100
}
41014101

4102-
func TestErrWrapNoArgs(t *testing.T) {
4103-
gopClTest(t, `
4104-
func foo(v ...int) (func(), error) {
4105-
return nil, nil
4106-
}
4107-
func Bar() (int, error) {
4108-
return 100, nil
4109-
}
4110-
foo!()
4111-
foo(1)!()
4112-
echo foo!
4113-
echo bar!
4114-
`, `package main
4115-
4116-
import (
4117-
"fmt"
4118-
"github.com/qiniu/x/errors"
4119-
)
4120-
4121-
func foo(v ...int) (func(), error) {
4122-
return nil, nil
4123-
}
4124-
func Bar() (int, error) {
4125-
return 100, nil
4126-
}
4127-
func main() {
4128-
func() (_xgo_ret func()) {
4129-
var _xgo_err error
4130-
_xgo_ret, _xgo_err = foo()
4131-
if _xgo_err != nil {
4132-
_xgo_err = errors.NewFrame(_xgo_err, "foo", "/foo/bar.xgo", 8, "main.main")
4133-
panic(_xgo_err)
4134-
}
4135-
return
4136-
}()()
4137-
func() (_xgo_ret func()) {
4138-
var _xgo_err error
4139-
_xgo_ret, _xgo_err = foo(1)
4140-
if _xgo_err != nil {
4141-
_xgo_err = errors.NewFrame(_xgo_err, "foo(1)", "/foo/bar.xgo", 9, "main.main")
4142-
panic(_xgo_err)
4143-
}
4144-
return
4145-
}()()
4146-
fmt.Println(func() (_xgo_ret func()) {
4147-
var _xgo_err error
4148-
_xgo_ret, _xgo_err = foo()
4149-
if _xgo_err != nil {
4150-
_xgo_err = errors.NewFrame(_xgo_err, "foo", "/foo/bar.xgo", 10, "main.main")
4151-
panic(_xgo_err)
4152-
}
4153-
return
4154-
}())
4155-
fmt.Println(func() (_xgo_ret int) {
4156-
var _xgo_err error
4157-
_xgo_ret, _xgo_err = Bar()
4158-
if _xgo_err != nil {
4159-
_xgo_err = errors.NewFrame(_xgo_err, "bar", "/foo/bar.xgo", 11, "main.main")
4160-
panic(_xgo_err)
4161-
}
4162-
return
4163-
}())
4164-
}
4165-
`)
4166-
}
4167-
41684102
func TestCommentFunc(t *testing.T) {
41694103
gopClTestEx(t, gblConfLine, "main", `
41704104
import (

cl/expr.go

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ const (
7979
objXGoExec = objXGoExecOrEnv
8080
)
8181

82-
func compileIdent(ctx *blockCtx, ident *ast.Ident, flags int) (pkg gogen.PkgRef, kind int) {
82+
func compileIdent(ctx *blockCtx, lhs int, ident *ast.Ident, flags int) (pkg gogen.PkgRef, kind int) {
8383
fvalue := (flags&clIdentSelectorExpr) != 0 || (flags&clIdentLHS) == 0
8484
cb := ctx.cb
8585
name := ident.Name
@@ -108,7 +108,7 @@ func compileIdent(ctx *blockCtx, ident *ast.Ident, flags int) (pkg gogen.PkgRef,
108108
if chkFlag&clIdentSelectorExpr != 0 { // TODO(xsw): remove this condition
109109
chkFlag = clIdentCanAutoCall
110110
}
111-
if compileMember(ctx, 0, ident, name, chkFlag) == nil { // class member object
111+
if compileMember(ctx, lhs, ident, name, chkFlag) == nil { // class member object
112112
return
113113
}
114114
cb.InternalStack().PopN(1)
@@ -134,12 +134,12 @@ func compileIdent(ctx *blockCtx, ident *ast.Ident, flags int) (pkg gogen.PkgRef,
134134
}
135135

136136
// function alias
137-
if compileFuncAlias(ctx, scope, ident, flags) {
137+
if compileFuncAlias(ctx, lhs, scope, ident, flags) {
138138
return
139139
}
140140

141141
// object from import . "xxx"
142-
if compilePkgRef(ctx, gogen.PkgRef{}, ident, flags, objPkgRef) {
142+
if compilePkgRef(ctx, lhs, gogen.PkgRef{}, ident, flags, objPkgRef) {
143143
return
144144
}
145145

@@ -278,7 +278,7 @@ func compileMember(ctx *blockCtx, lhs int, v ast.Node, name string, flags int) e
278278
func compileExprLHS(ctx *blockCtx, expr ast.Expr) {
279279
switch v := expr.(type) {
280280
case *ast.Ident:
281-
compileIdent(ctx, v, clIdentLHS)
281+
compileIdent(ctx, 1, v, clIdentLHS)
282282
case *ast.IndexExpr:
283283
compileIndexExprLHS(ctx, v)
284284
case *ast.SelectorExpr:
@@ -329,7 +329,7 @@ func compileExpr(ctx *blockCtx, lhs int, expr ast.Expr, inFlags ...int) {
329329
if cmdNoArgs {
330330
flags |= clCommandIdent // for support XGo_Exec, see TestSpxXGoExec
331331
}
332-
_, kind := compileIdent(ctx, v, flags)
332+
_, kind := compileIdent(ctx, lhs, v, flags)
333333
if cmdNoArgs || kind == objXGoExecOrEnv {
334334
cb := ctx.cb
335335
if kind == objXGoExecOrEnv {
@@ -492,7 +492,7 @@ func compileSliceExpr(ctx *blockCtx, v *ast.SliceExpr) { // x[i:j:k]
492492
func compileSelectorExprLHS(ctx *blockCtx, v *ast.SelectorExpr) {
493493
switch x := v.X.(type) {
494494
case *ast.Ident:
495-
if at, kind := compileIdent(ctx, x, clIdentLHS|clIdentSelectorExpr); kind != objNormal {
495+
if at, kind := compileIdent(ctx, 1, x, clIdentLHS|clIdentSelectorExpr); kind != objNormal {
496496
ctx.cb.VarRef(at.Ref(v.Sel.Name))
497497
return
498498
}
@@ -602,8 +602,8 @@ func convMapToNodeSet(cb *gogen.CodeBuilder) {
602602
func compileSelectorExpr(ctx *blockCtx, lhs int, v *ast.SelectorExpr, flags int) {
603603
switch x := v.X.(type) {
604604
case *ast.Ident:
605-
if at, kind := compileIdent(ctx, x, flags|clIdentCanAutoCall|clIdentSelectorExpr); kind != objNormal {
606-
if compilePkgRef(ctx, at, v.Sel, flags, kind) {
605+
if at, kind := compileIdent(ctx, 1, x, flags|clIdentCanAutoCall|clIdentSelectorExpr); kind != objNormal {
606+
if compilePkgRef(ctx, 1, at, v.Sel, flags, kind) {
607607
return
608608
}
609609
if token.IsExported(v.Sel.Name) {
@@ -663,7 +663,7 @@ func unquote(name string) string {
663663
return s
664664
}
665665

666-
func compileFuncAlias(ctx *blockCtx, scope *types.Scope, x *ast.Ident, flags int) bool {
666+
func compileFuncAlias(ctx *blockCtx, lhs int, scope *types.Scope, x *ast.Ident, flags int) bool {
667667
name := x.Name
668668
if c := name[0]; c >= 'a' && c <= 'z' {
669669
name = string(rune(c)+('A'-'a')) + name[1:]
@@ -672,7 +672,7 @@ func compileFuncAlias(ctx *blockCtx, scope *types.Scope, x *ast.Ident, flags int
672672
o = scope.Lookup(name)
673673
}
674674
if o != nil {
675-
return identVal(ctx, x, flags, o, true)
675+
return identVal(ctx, lhs, x, flags, o, true)
676676
}
677677
}
678678
return false
@@ -710,7 +710,7 @@ func lookupPkgRef(ctx *blockCtx, pkg gogen.PkgRef, x *ast.Ident, pkgKind int) (o
710710
}
711711

712712
// allow at.Types to be nil
713-
func compilePkgRef(ctx *blockCtx, at gogen.PkgRef, x *ast.Ident, flags, pkgKind int) bool {
713+
func compilePkgRef(ctx *blockCtx, lhs int, at gogen.PkgRef, x *ast.Ident, flags, pkgKind int) bool {
714714
if v, alias := lookupPkgRef(ctx, at, x, pkgKind); v != nil {
715715
if (flags & clIdentLHS) != 0 {
716716
if rec := ctx.recorder(); rec != nil {
@@ -719,12 +719,12 @@ func compilePkgRef(ctx *blockCtx, at gogen.PkgRef, x *ast.Ident, flags, pkgKind
719719
ctx.cb.VarRef(v, x)
720720
return true
721721
}
722-
return identVal(ctx, x, flags, v, alias)
722+
return identVal(ctx, lhs, x, flags, v, alias)
723723
}
724724
return false
725725
}
726726

727-
func identVal(ctx *blockCtx, x *ast.Ident, flags int, v types.Object, alias bool) bool {
727+
func identVal(ctx *blockCtx, lhs int, x *ast.Ident, flags int, v types.Object, alias bool) bool {
728728
autocall := false
729729
if alias {
730730
if autocall = (flags & clIdentCanAutoCall) != 0; autocall {
@@ -738,7 +738,7 @@ func identVal(ctx *blockCtx, x *ast.Ident, flags int, v types.Object, alias bool
738738
}
739739
cb := ctx.cb.Val(v, x)
740740
if autocall {
741-
cb.CallWith(0, 0, 0, x)
741+
cb.CallWith(0, lhs, 0, x)
742742
}
743743
return true
744744
}
@@ -837,7 +837,7 @@ func compileCallExpr(ctx *blockCtx, lhs int, v *ast.CallExpr, inFlags int) {
837837
if v.IsCommand() { // for support XGo_Exec, see TestSpxXGoExec
838838
inFlags |= clCommandIdent
839839
}
840-
if _, kind := compileIdent(ctx, fn, clIdentAllowBuiltin|inFlags); kind == objXGoExec {
840+
if _, kind := compileIdent(ctx, 1, fn, clIdentAllowBuiltin|inFlags); kind == objXGoExec {
841841
args := make([]ast.Expr, 1, len(v.Args)+1)
842842
args[0] = toBasicLit(fn)
843843
args = append(args, v.Args...)
@@ -846,7 +846,7 @@ func compileCallExpr(ctx *blockCtx, lhs int, v *ast.CallExpr, inFlags int) {
846846
ifn = fn
847847
}
848848
case *ast.SelectorExpr:
849-
compileSelectorExpr(ctx, lhs, fn, 0)
849+
compileSelectorExpr(ctx, 1, fn, 0)
850850
case *ast.ErrWrapExpr:
851851
if v.IsCommand() {
852852
callExpr := *v
@@ -858,7 +858,7 @@ func compileCallExpr(ctx *blockCtx, lhs int, v *ast.CallExpr, inFlags int) {
858858
}
859859
compileErrWrapExpr(ctx, 1, fn, 0)
860860
default:
861-
compileExpr(ctx, 0, fn, clInCallExpr)
861+
compileExpr(ctx, 1, fn, clInCallExpr)
862862
}
863863
var err error
864864
var stk = ctx.cb.InternalStack()
@@ -1083,7 +1083,7 @@ func compileCallArgs(ctx *blockCtx, lhs int, pfn *gogen.Element, fn *fnType, v *
10831083
case *ast.LambdaExpr:
10841084
if fn.typeparam {
10851085
needInferFunc = true
1086-
compileIdent(ctx, ast.NewIdent("nil"), 0)
1086+
compileIdent(ctx, 0, ast.NewIdent("nil"), 0) // TODO(xsw): check lhs
10871087
continue
10881088
}
10891089
sig, e := checkLambdaFuncType(ctx, expr, t, clLambaArgument, v.Fun)
@@ -1096,7 +1096,7 @@ func compileCallArgs(ctx *blockCtx, lhs int, pfn *gogen.Element, fn *fnType, v *
10961096
case *ast.LambdaExpr2:
10971097
if fn.typeparam {
10981098
needInferFunc = true
1099-
compileIdent(ctx, ast.NewIdent("nil"), 0)
1099+
compileIdent(ctx, 0, ast.NewIdent("nil"), 0) // TODO(xsw): check lhs
11001100
continue
11011101
}
11021102
sig, e := checkLambdaFuncType(ctx, expr, t, clLambaArgument, v.Fun)
@@ -1956,17 +1956,12 @@ func compileErrWrapExpr(ctx *blockCtx, lhs int, v *ast.ErrWrapExpr, inFlags int)
19561956
if !useClosure && (cb.Scope().Parent() == types.Universe) {
19571957
panic("TODO: can't use expr? in global")
19581958
}
1959-
expr := v.X
1960-
switch expr.(type) {
1961-
case *ast.Ident, *ast.SelectorExpr:
1962-
expr = &ast.CallExpr{Fun: expr, NoParenEnd: expr.End()}
1963-
}
19641959
if lhs != 0 {
19651960
// lhs == 0 means the result is discarded
19661961
// +1 accounts for the error value that will be stripped from the result tuple
19671962
lhs++
19681963
}
1969-
compileExpr(ctx, lhs, expr, inFlags)
1964+
compileExpr(ctx, lhs, v.X, inFlags)
19701965
x := cb.InternalStack().Pop()
19711966
n := 0
19721967
results, ok := x.Type.(*types.Tuple)

0 commit comments

Comments
 (0)