]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: refactor multiExpr and exprList
authorRobert Griesemer <gri@golang.org>
Tue, 21 Mar 2023 04:04:14 +0000 (21:04 -0700)
committerGopher Robot <gobot@golang.org>
Tue, 21 Mar 2023 21:31:13 +0000 (21:31 +0000)
Preparation for simpler exprList use.

Change-Id: I2d62bbaba006aa3a378ec743564d46c5edcb8b47
Reviewed-on: https://go-review.googlesource.com/c/go/+/478016
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/cmd/compile/internal/types2/call.go
src/cmd/compile/internal/types2/expr.go
src/go/types/call.go
src/go/types/expr.go

index db83d58fcce61a885d0c18882c60f5ed06b5a029..4a4c77decf90855f6570c58fec053e63e090f237 100644 (file)
@@ -265,32 +265,8 @@ func (check *Checker) exprList(elist []syntax.Expr, allowCommaOk bool) (xlist []
        switch len(elist) {
        case 0:
                // nothing to do
-
        case 1:
-               // single (possibly comma-ok) value, or function returning multiple values
-               e := elist[0]
-               var x operand
-               check.multiExpr(&x, e)
-               if t, ok := x.typ.(*Tuple); ok && x.mode != invalid {
-                       // multiple values
-                       xlist = make([]*operand, t.Len())
-                       for i, v := range t.vars {
-                               xlist[i] = &operand{mode: value, expr: e, typ: v.typ}
-                       }
-                       break
-               }
-
-               // exactly one (possibly invalid or comma-ok) value
-               xlist = []*operand{&x}
-               if allowCommaOk && (x.mode == mapindex || x.mode == commaok || x.mode == commaerr) {
-                       x2 := &operand{mode: value, expr: e, typ: Typ[UntypedBool]}
-                       if x.mode == commaerr {
-                               x2.typ = universeError
-                       }
-                       xlist = append(xlist, x2)
-                       commaOk = true
-               }
-
+               return check.multiExpr(elist[0], allowCommaOk)
        default:
                // multiple (possibly invalid) values
                xlist = make([]*operand, len(elist))
@@ -300,7 +276,6 @@ func (check *Checker) exprList(elist []syntax.Expr, allowCommaOk bool) (xlist []
                        xlist[i] = &x
                }
        }
-
        return
 }
 
index 2baa80c4faab3cb4c331c9e2feb9a6a06edb3aae..72c01863394a6e4f373cdb94be46bda725667bf8 100644 (file)
@@ -1816,10 +1816,36 @@ func (check *Checker) expr(x *operand, e syntax.Expr) {
        check.singleValue(x)
 }
 
-// multiExpr is like expr but the result may also be a multi-value.
-func (check *Checker) multiExpr(x *operand, e syntax.Expr) {
-       check.rawExpr(x, e, nil, false)
-       check.exclude(x, 1<<novalue|1<<builtin|1<<typexpr)
+// multiExpr typechecks e and returns its value (or values) in list.
+// If allowCommaOk is set and e is a map index, comma-ok, or comma-err
+// expression, the result is a two-element list containing the value
+// of e, and an untyped bool value or an error value, respectively.
+func (check *Checker) multiExpr(e syntax.Expr, allowCommaOk bool) (list []*operand, commaOk bool) {
+       var x operand
+       check.rawExpr(&x, e, nil, false)
+       check.exclude(&x, 1<<novalue|1<<builtin|1<<typexpr)
+
+       if t, ok := x.typ.(*Tuple); ok && x.mode != invalid {
+               // multiple values
+               list = make([]*operand, t.Len())
+               for i, v := range t.vars {
+                       list[i] = &operand{mode: value, expr: e, typ: v.typ}
+               }
+               return
+       }
+
+       // exactly one (possibly invalid or comma-ok) value
+       list = []*operand{&x}
+       if allowCommaOk && (x.mode == mapindex || x.mode == commaok || x.mode == commaerr) {
+               x2 := &operand{mode: value, expr: e, typ: Typ[UntypedBool]}
+               if x.mode == commaerr {
+                       x2.typ = universeError
+               }
+               list = append(list, x2)
+               commaOk = true
+       }
+
+       return
 }
 
 // exprWithHint typechecks expression e and initializes x with the expression value;
index dbb84111855cbbed08cfdff625c5c361aca65cec..bb9bba32c83c24682f9aeae1eb5a6eda123e9732 100644 (file)
@@ -267,32 +267,8 @@ func (check *Checker) exprList(elist []ast.Expr, allowCommaOk bool) (xlist []*op
        switch len(elist) {
        case 0:
                // nothing to do
-
        case 1:
-               // single (possibly comma-ok) value, or function returning multiple values
-               e := elist[0]
-               var x operand
-               check.multiExpr(&x, e)
-               if t, ok := x.typ.(*Tuple); ok && x.mode != invalid {
-                       // multiple values
-                       xlist = make([]*operand, t.Len())
-                       for i, v := range t.vars {
-                               xlist[i] = &operand{mode: value, expr: e, typ: v.typ}
-                       }
-                       break
-               }
-
-               // exactly one (possibly invalid or comma-ok) value
-               xlist = []*operand{&x}
-               if allowCommaOk && (x.mode == mapindex || x.mode == commaok || x.mode == commaerr) {
-                       x2 := &operand{mode: value, expr: e, typ: Typ[UntypedBool]}
-                       if x.mode == commaerr {
-                               x2.typ = universeError
-                       }
-                       xlist = append(xlist, x2)
-                       commaOk = true
-               }
-
+               return check.multiExpr(elist[0], allowCommaOk)
        default:
                // multiple (possibly invalid) values
                xlist = make([]*operand, len(elist))
@@ -302,7 +278,6 @@ func (check *Checker) exprList(elist []ast.Expr, allowCommaOk bool) (xlist []*op
                        xlist[i] = &x
                }
        }
-
        return
 }
 
index 7c87702bd8675f3bc00d0e2686bac15683fda730..4dff5332eaf5eb3925c6fccce0cb36266cc278c0 100644 (file)
@@ -1763,10 +1763,36 @@ func (check *Checker) expr(x *operand, e ast.Expr) {
        check.singleValue(x)
 }
 
-// multiExpr is like expr but the result may also be a multi-value.
-func (check *Checker) multiExpr(x *operand, e ast.Expr) {
-       check.rawExpr(x, e, nil, false)
-       check.exclude(x, 1<<novalue|1<<builtin|1<<typexpr)
+// multiExpr typechecks e and returns its value (or values) in list.
+// If allowCommaOk is set and e is a map index, comma-ok, or comma-err
+// expression, the result is a two-element list containing the value
+// of e, and an untyped bool value or an error value, respectively.
+func (check *Checker) multiExpr(e ast.Expr, allowCommaOk bool) (list []*operand, commaOk bool) {
+       var x operand
+       check.rawExpr(&x, e, nil, false)
+       check.exclude(&x, 1<<novalue|1<<builtin|1<<typexpr)
+
+       if t, ok := x.typ.(*Tuple); ok && x.mode != invalid {
+               // multiple values
+               list = make([]*operand, t.Len())
+               for i, v := range t.vars {
+                       list[i] = &operand{mode: value, expr: e, typ: v.typ}
+               }
+               return
+       }
+
+       // exactly one (possibly invalid or comma-ok) value
+       list = []*operand{&x}
+       if allowCommaOk && (x.mode == mapindex || x.mode == commaok || x.mode == commaerr) {
+               x2 := &operand{mode: value, expr: e, typ: Typ[UntypedBool]}
+               if x.mode == commaerr {
+                       x2.typ = universeError
+               }
+               list = append(list, x2)
+               commaOk = true
+       }
+
+       return
 }
 
 // exprWithHint typechecks expression e and initializes x with the expression value;