1
0
mirror of https://github.com/golang/go synced 2024-11-25 15:37:56 -07:00

cmd/compile/internal/types2: use syntax.EndPos instead of local computation

Previously, the end position for a select statement clause body was
computed explicitly as the start of the next clause or the closing "}"
of the select statement, respectively.

Since syntax.EndPos computes the end position of a node, there's no
need to compute these positions "manually", we can simply use the
syntax.ExdPos for each clause. The positions are not exactly the
same as before but for the purpose of identifier visibility in
scopes there is no semantic change.

Simplifies the code and brings it more in line with go/types.

Change-Id: I24bca85a131a0ea31a2adaafc08ab713450258fb
Reviewed-on: https://go-review.googlesource.com/c/go/+/593016
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
This commit is contained in:
Robert Griesemer 2024-06-17 15:02:10 -07:00 committed by Gopher Robot
parent e7b7f44ff8
commit 88bd985239

View File

@ -150,11 +150,7 @@ func (check *Checker) multipleSelectDefaults(list []*syntax.CommClause) {
} }
func (check *Checker) openScope(node syntax.Node, comment string) { func (check *Checker) openScope(node syntax.Node, comment string) {
check.openScopeUntil(node, syntax.EndPos(node), comment) scope := NewScope(check.scope, node.Pos(), syntax.EndPos(node), comment)
}
func (check *Checker) openScopeUntil(node syntax.Node, end syntax.Pos, comment string) {
scope := NewScope(check.scope, node.Pos(), end, comment)
check.recordScope(node, scope) check.recordScope(node, scope)
check.scope = scope check.scope = scope
} }
@ -637,7 +633,7 @@ func (check *Checker) stmt(ctxt stmtContext, s syntax.Stmt) {
check.multipleSelectDefaults(s.Body) check.multipleSelectDefaults(s.Body)
for i, clause := range s.Body { for _, clause := range s.Body {
if clause == nil { if clause == nil {
continue // error reported before continue // error reported before
} }
@ -667,11 +663,7 @@ func (check *Checker) stmt(ctxt stmtContext, s syntax.Stmt) {
check.error(clause.Comm, InvalidSelectCase, "select case must be send or receive (possibly with assignment)") check.error(clause.Comm, InvalidSelectCase, "select case must be send or receive (possibly with assignment)")
continue continue
} }
end := s.Rbrace check.openScope(clause, "case")
if i+1 < len(s.Body) {
end = s.Body[i+1].Pos()
}
check.openScopeUntil(clause, end, "case")
if clause.Comm != nil { if clause.Comm != nil {
check.stmt(inner, clause.Comm) check.stmt(inner, clause.Comm)
} }
@ -747,16 +739,14 @@ func (check *Checker) switchStmt(inner stmtContext, s *syntax.SwitchStmt) {
check.error(clause, InvalidSyntaxTree, "incorrect expression switch case") check.error(clause, InvalidSyntaxTree, "incorrect expression switch case")
continue continue
} }
end := s.Rbrace
inner := inner inner := inner
if i+1 < len(s.Body) { if i+1 < len(s.Body) {
end = s.Body[i+1].Pos()
inner |= fallthroughOk inner |= fallthroughOk
} else { } else {
inner |= finalSwitchCase inner |= finalSwitchCase
} }
check.caseValues(&x, syntax.UnpackListExpr(clause.Cases), seen) check.caseValues(&x, syntax.UnpackListExpr(clause.Cases), seen)
check.openScopeUntil(clause, end, "case") check.openScope(clause, "case")
check.stmtList(inner, clause.Body) check.stmtList(inner, clause.Body)
check.closeScope() check.closeScope()
} }
@ -802,19 +792,15 @@ func (check *Checker) typeSwitchStmt(inner stmtContext, s *syntax.SwitchStmt, gu
var lhsVars []*Var // list of implicitly declared lhs variables var lhsVars []*Var // list of implicitly declared lhs variables
seen := make(map[Type]syntax.Expr) // map of seen types to positions seen := make(map[Type]syntax.Expr) // map of seen types to positions
for i, clause := range s.Body { for _, clause := range s.Body {
if clause == nil { if clause == nil {
check.error(s, InvalidSyntaxTree, "incorrect type switch case") check.error(s, InvalidSyntaxTree, "incorrect type switch case")
continue continue
} }
end := s.Rbrace
if i+1 < len(s.Body) {
end = s.Body[i+1].Pos()
}
// Check each type in this type switch case. // Check each type in this type switch case.
cases := syntax.UnpackListExpr(clause.Cases) cases := syntax.UnpackListExpr(clause.Cases)
T := check.caseTypes(sx, cases, seen) T := check.caseTypes(sx, cases, seen)
check.openScopeUntil(clause, end, "case") check.openScope(clause, "case")
// If lhs exists, declare a corresponding variable in the case-local scope. // If lhs exists, declare a corresponding variable in the case-local scope.
if lhs != nil { if lhs != nil {
obj := NewVar(lhs.Pos(), check.pkg, lhs.Value, T) obj := NewVar(lhs.Pos(), check.pkg, lhs.Value, T)