]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: factor type checking of func literals and generate go/types code
authorRobert Griesemer <gri@golang.org>
Thu, 5 Sep 2024 00:15:57 +0000 (17:15 -0700)
committerGopher Robot <gobot@golang.org>
Thu, 5 Sep 2024 22:09:30 +0000 (22:09 +0000)
Move the code for type checking of function literals into
literals.go.

In go/types, the respective code is now generated from the types2 source.

Change-Id: Ic81ab3c0d3c66d99bc0f2e21d66bf9a896ef9375
Reviewed-on: https://go-review.googlesource.com/c/go/+/610996
Reviewed-by: Tim King <taking@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Griesemer <gri@google.com>
src/cmd/compile/internal/types2/expr.go
src/cmd/compile/internal/types2/literals.go
src/go/types/expr.go
src/go/types/literals.go

index 30fa05673cdda9a15efedca74b368db52c00e6c0..a1e3012bcb5f9674b150c1d318b44aba726b3d82 100644 (file)
@@ -1123,29 +1123,8 @@ func (check *Checker) exprInternal(T *target, x *operand, e syntax.Expr, hint Ty
                check.overflow(x, opPos(x.expr))
 
        case *syntax.FuncLit:
-               if sig, ok := check.typ(e.Type).(*Signature); ok {
-                       // Set the Scope's extent to the complete "func (...) {...}"
-                       // so that Scope.Innermost works correctly.
-                       sig.scope.pos = e.Pos()
-                       sig.scope.end = endPos(e)
-                       if !check.conf.IgnoreFuncBodies && e.Body != nil {
-                               // Anonymous functions are considered part of the
-                               // init expression/func declaration which contains
-                               // them: use existing package-level declaration info.
-                               decl := check.decl // capture for use in closure below
-                               iota := check.iota // capture for use in closure below (go.dev/issue/22345)
-                               // Don't type-check right away because the function may
-                               // be part of a type definition to which the function
-                               // body refers. Instead, type-check as soon as possible,
-                               // but before the enclosing scope contents changes (go.dev/issue/22992).
-                               check.later(func() {
-                                       check.funcBody(decl, "<function literal>", sig, e.Body, iota)
-                               }).describef(e, "func literal")
-                       }
-                       x.mode = value
-                       x.typ = sig
-               } else {
-                       check.errorf(e, InvalidSyntaxTree, "invalid function literal %v", e)
+               check.funcLit(x, e)
+               if x.mode == invalid {
                        goto Error
                }
 
index fbb139be8f04c7abc237433c9e722ff7621ca4ed..188c92077690370ca4ad872ee3bc8ba5be5b723d 100644 (file)
@@ -11,6 +11,34 @@ import (
        . "internal/types/errors"
 )
 
+func (check *Checker) funcLit(x *operand, e *syntax.FuncLit) {
+       if sig, ok := check.typ(e.Type).(*Signature); ok {
+               // Set the Scope's extent to the complete "func (...) {...}"
+               // so that Scope.Innermost works correctly.
+               sig.scope.pos = e.Pos()
+               sig.scope.end = endPos(e)
+               if !check.conf.IgnoreFuncBodies && e.Body != nil {
+                       // Anonymous functions are considered part of the
+                       // init expression/func declaration which contains
+                       // them: use existing package-level declaration info.
+                       decl := check.decl // capture for use in closure below
+                       iota := check.iota // capture for use in closure below (go.dev/issue/22345)
+                       // Don't type-check right away because the function may
+                       // be part of a type definition to which the function
+                       // body refers. Instead, type-check as soon as possible,
+                       // but before the enclosing scope contents changes (go.dev/issue/22992).
+                       check.later(func() {
+                               check.funcBody(decl, "<function literal>", sig, e.Body, iota)
+                       }).describef(e, "func literal")
+               }
+               x.mode = value
+               x.typ = sig
+       } else {
+               check.errorf(e, InvalidSyntaxTree, "invalid function literal %v", e)
+               x.mode = invalid
+       }
+}
+
 func (check *Checker) compositeLit(T *target, x *operand, e *syntax.CompositeLit, hint Type) {
        var typ, base Type
        var isElem bool // true if composite literal is an element of an enclosing composite literal
index ebc662e9663c39cd4bec56407fda8e1058e60353..d17464e27ea12ee3b73cbd6765a5b548e1882d00 100644 (file)
@@ -1101,29 +1101,8 @@ func (check *Checker) exprInternal(T *target, x *operand, e ast.Expr, hint Type)
                check.overflow(x, e.Pos())
 
        case *ast.FuncLit:
-               if sig, ok := check.typ(e.Type).(*Signature); ok {
-                       // Set the Scope's extent to the complete "func (...) {...}"
-                       // so that Scope.Innermost works correctly.
-                       sig.scope.pos = e.Pos()
-                       sig.scope.end = endPos(e)
-                       if !check.conf.IgnoreFuncBodies && e.Body != nil {
-                               // Anonymous functions are considered part of the
-                               // init expression/func declaration which contains
-                               // them: use existing package-level declaration info.
-                               decl := check.decl // capture for use in closure below
-                               iota := check.iota // capture for use in closure below (go.dev/issue/22345)
-                               // Don't type-check right away because the function may
-                               // be part of a type definition to which the function
-                               // body refers. Instead, type-check as soon as possible,
-                               // but before the enclosing scope contents changes (go.dev/issue/22992).
-                               check.later(func() {
-                                       check.funcBody(decl, "<function literal>", sig, e.Body, iota)
-                               }).describef(e, "func literal")
-                       }
-                       x.mode = value
-                       x.typ = sig
-               } else {
-                       check.errorf(e, InvalidSyntaxTree, "invalid function literal %v", e)
+               check.funcLit(x, e)
+               if x.mode == invalid {
                        goto Error
                }
 
index a32f8ca37cb7e802ce3e5263270a7c6bfdff7c06..5efb0360adbf09ced1d892bd61d79890ff9b26d0 100644 (file)
@@ -14,6 +14,34 @@ import (
        . "internal/types/errors"
 )
 
+func (check *Checker) funcLit(x *operand, e *ast.FuncLit) {
+       if sig, ok := check.typ(e.Type).(*Signature); ok {
+               // Set the Scope's extent to the complete "func (...) {...}"
+               // so that Scope.Innermost works correctly.
+               sig.scope.pos = e.Pos()
+               sig.scope.end = endPos(e)
+               if !check.conf.IgnoreFuncBodies && e.Body != nil {
+                       // Anonymous functions are considered part of the
+                       // init expression/func declaration which contains
+                       // them: use existing package-level declaration info.
+                       decl := check.decl // capture for use in closure below
+                       iota := check.iota // capture for use in closure below (go.dev/issue/22345)
+                       // Don't type-check right away because the function may
+                       // be part of a type definition to which the function
+                       // body refers. Instead, type-check as soon as possible,
+                       // but before the enclosing scope contents changes (go.dev/issue/22992).
+                       check.later(func() {
+                               check.funcBody(decl, "<function literal>", sig, e.Body, iota)
+                       }).describef(e, "func literal")
+               }
+               x.mode = value
+               x.typ = sig
+       } else {
+               check.errorf(e, InvalidSyntaxTree, "invalid function literal %v", e)
+               x.mode = invalid
+       }
+}
+
 func (check *Checker) compositeLit(T *target, x *operand, e *ast.CompositeLit, hint Type) {
        var typ, base Type
        var isElem bool // true if composite literal is an element of an enclosing composite literal