From: Robert Griesemer Date: Mon, 20 Mar 2023 20:32:04 +0000 (-0700) Subject: go/types: remove Checker.useLHS - not needed X-Git-Tag: go1.21rc1~1198 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=cbcef91a1054dedaa97ce07874281ac842838ac8;p=gostls13.git go/types: remove Checker.useLHS - not needed 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 TryBot-Result: Gopher Robot Reviewed-by: Robert Findley Run-TryBot: Robert Griesemer Auto-Submit: Robert Griesemer --- diff --git a/src/go/types/assignments.go b/src/go/types/assignments.go index 2d71c8f100..8d12df81a0 100644 --- a/src/go/types/assignments.go +++ b/src/go/types/assignments.go @@ -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 diff --git a/src/go/types/call.go b/src/go/types/call.go index bb9bba32c8..dce05eb4d4 100644 --- a/src/go/types/call.go +++ b/src/go/types/call.go @@ -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 - } } }