]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: remove Checker.useLHS - not needed
authorRobert Griesemer <gri@golang.org>
Mon, 20 Mar 2023 20:32:04 +0000 (13:32 -0700)
committerGopher Robot <gobot@golang.org>
Tue, 21 Mar 2023 21:47:10 +0000 (21:47 +0000)
We can just use Checker.use, as long as we take care of blank (_)
identifiers that may appear of the LHS of assignments. It's ok to
"use" non-blank variables in case of an error, even on the LHS.

This makes this code match the types2 implementation.

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

src/go/types/assignments.go
src/go/types/call.go

index 2d71c8f10092380174c2a27f34fc0b9cb3d08e2c..8d12df81a057e739d1f36a00fa377f5fc7e9684e 100644 (file)
@@ -227,7 +227,7 @@ func (check *Checker) lhsVar(lhs ast.Expr) Type {
 // If the assignment is invalid, the result is nil.
 func (check *Checker) assignVar(lhs ast.Expr, x *operand) Type {
        if x.mode == invalid || x.typ == Typ[Invalid] {
-               check.useLHS(lhs)
+               check.use(lhs)
                return nil
        }
 
@@ -382,7 +382,7 @@ func (check *Checker) assignVars(lhs, origRHS []ast.Expr) {
        rhs, commaOk := check.exprList(origRHS, len(lhs) == 2)
 
        if len(lhs) != len(rhs) {
-               check.useLHS(lhs...)
+               check.use(lhs...)
                // don't report an error if we already reported one
                for _, x := range rhs {
                        if x.mode == invalid {
@@ -419,7 +419,7 @@ func (check *Checker) shortVarDecl(pos positioner, lhs, rhs []ast.Expr) {
        for i, lhs := range lhs {
                ident, _ := lhs.(*ast.Ident)
                if ident == nil {
-                       check.useLHS(lhs)
+                       check.use(lhs)
                        // TODO(rFindley) this is redundant with a parser error. Consider omitting?
                        check.errorf(lhs, BadDecl, "non-name %s on left side of :=", lhs)
                        hasErr = true
index bb9bba32c83c24682f9aeae1eb5a6eda123e9732..dce05eb4d4db926b571cd7e9fd2ea449a2078d67 100644 (file)
@@ -749,44 +749,17 @@ Error:
 func (check *Checker) use(arg ...ast.Expr) {
        var x operand
        for _, e := range arg {
-               // The nil check below is necessary since certain AST fields
-               // may legally be nil (e.g., the ast.SliceExpr.High field).
-               if e != nil {
-                       check.rawExpr(&x, e, nil, false)
-               }
-       }
-}
-
-// useLHS is like use, but doesn't "use" top-level identifiers.
-// It should be called instead of use if the arguments are
-// expressions on the lhs of an assignment.
-// The arguments must not be nil.
-func (check *Checker) useLHS(arg ...ast.Expr) {
-       var x operand
-       for _, e := range arg {
-               // If the lhs is an identifier denoting a variable v, this assignment
-               // is not a 'use' of v. Remember current value of v.used and restore
-               // after evaluating the lhs via check.rawExpr.
-               var v *Var
-               var v_used bool
-               if ident, _ := unparen(e).(*ast.Ident); ident != nil {
-                       // never type-check the blank name on the lhs
-                       if ident.Name == "_" {
+               switch n := e.(type) {
+               case nil:
+                       // some AST fields may be nil (e.g., the ast.SliceExpr.High field)
+                       // TODO(gri) can those fields really make it here?
+                       continue
+               case *ast.Ident:
+                       // don't report an error evaluating blank
+                       if n.Name == "_" {
                                continue
                        }
-                       if _, obj := check.scope.LookupParent(ident.Name, nopos); obj != nil {
-                               // It's ok to mark non-local variables, but ignore variables
-                               // from other packages to avoid potential race conditions with
-                               // dot-imported variables.
-                               if w, _ := obj.(*Var); w != nil && w.pkg == check.pkg {
-                                       v = w
-                                       v_used = v.used
-                               }
-                       }
                }
                check.rawExpr(&x, e, nil, false)
-               if v != nil {
-                       v.used = v_used // restore v.used
-               }
        }
 }