1
0
mirror of https://github.com/golang/go synced 2024-11-22 15:24:42 -07:00

go/types, types2: use ":" as start of implicit type switch case scopes

Adjust the respective API test accordingly.

Change-Id: I7ecc8899b40ae3b5aeb2c1e032935c672b41e0b6
Reviewed-on: https://go-review.googlesource.com/c/go/+/592675
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Robert Griesemer 2024-06-13 16:24:26 -07:00 committed by Gopher Robot
parent c761174d96
commit e7b7f44ff8
4 changed files with 18 additions and 24 deletions

View File

@ -1884,6 +1884,9 @@ func TestScopeLookupParent(t *testing.T) {
// Each /*name=kind:line*/ comment makes the test look up the // Each /*name=kind:line*/ comment makes the test look up the
// name at that point and checks that it resolves to a decl of // name at that point and checks that it resolves to a decl of
// the specified kind and line number. "undef" means undefined. // the specified kind and line number. "undef" means undefined.
// Note that type switch case clauses with an empty body (but for
// comments) need the ";" to ensure that the recorded scope extends
// past the comments.
mainSrc := ` mainSrc := `
/*lib=pkgname:5*/ /*X=var:1*/ /*Pi=const:8*/ /*T=typename:9*/ /*Y=var:10*/ /*F=func:12*/ /*lib=pkgname:5*/ /*X=var:1*/ /*Pi=const:8*/ /*T=typename:9*/ /*Y=var:10*/ /*F=func:12*/
package main package main
@ -1907,17 +1910,17 @@ func F[T *U, U any](param1, param2 int) /*param1=undef*/ (res1 /*res1=undef*/, r
var i interface{} var i interface{}
switch y := i.(type) { /*y=undef*/ switch y := i.(type) { /*y=undef*/
case /*y=undef*/ int /*y=var:23*/ : case /*y=undef*/ int /*y=undef*/ : /*y=var:23*/ ;
case float32, /*y=undef*/ float64 /*y=var:23*/ : case float32, /*y=undef*/ float64 /*y=undef*/ : /*y=var:23*/ ;
default /*y=var:23*/: default /*y=undef*/ : /*y=var:23*/
println(y) println(y)
} }
/*y=undef*/ /*y=undef*/
switch int := i.(type) { switch int := i.(type) {
case /*int=typename:0*/ int /*int=var:31*/ : case /*int=typename:0*/ int /*int=typename:0*/ : /*int=var:31*/
println(int) println(int)
default /*int=var:31*/ : default /*int=typename:0*/ : /*int=var:31*/ ;
} }
_ = param1 _ = param1

View File

@ -818,13 +818,7 @@ func (check *Checker) typeSwitchStmt(inner stmtContext, s *syntax.SwitchStmt, gu
// 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)
// TODO(mdempsky): Just use clause.Colon? Why did I even suggest check.declare(check.scope, nil, obj, clause.Colon)
// "at the end of the TypeSwitchCase" in go.dev/issue/16794 instead?
scopePos := clause.Pos() // for default clause (len(List) == 0)
if n := len(cases); n > 0 {
scopePos = syntax.EndPos(cases[n-1])
}
check.declare(check.scope, nil, obj, scopePos)
check.recordImplicit(clause, obj) check.recordImplicit(clause, obj)
// For the "declared and not used" error, all lhs variables act as // For the "declared and not used" error, all lhs variables act as
// one; i.e., if any one of them is 'used', all of them are 'used'. // one; i.e., if any one of them is 'used', all of them are 'used'.

View File

@ -1885,6 +1885,9 @@ func TestScopeLookupParent(t *testing.T) {
// Each /*name=kind:line*/ comment makes the test look up the // Each /*name=kind:line*/ comment makes the test look up the
// name at that point and checks that it resolves to a decl of // name at that point and checks that it resolves to a decl of
// the specified kind and line number. "undef" means undefined. // the specified kind and line number. "undef" means undefined.
// Note that type switch case clauses with an empty body (but for
// comments) need the ";" to ensure that the recorded scope extends
// past the comments.
mainSrc := ` mainSrc := `
/*lib=pkgname:5*/ /*X=var:1*/ /*Pi=const:8*/ /*T=typename:9*/ /*Y=var:10*/ /*F=func:12*/ /*lib=pkgname:5*/ /*X=var:1*/ /*Pi=const:8*/ /*T=typename:9*/ /*Y=var:10*/ /*F=func:12*/
package main package main
@ -1908,17 +1911,17 @@ func F[T *U, U any](param1, param2 int) /*param1=undef*/ (res1 /*res1=undef*/, r
var i interface{} var i interface{}
switch y := i.(type) { /*y=undef*/ switch y := i.(type) { /*y=undef*/
case /*y=undef*/ int /*y=var:23*/ : case /*y=undef*/ int /*y=undef*/ : /*y=var:23*/ ;
case float32, /*y=undef*/ float64 /*y=var:23*/ : case float32, /*y=undef*/ float64 /*y=undef*/ : /*y=var:23*/ ;
default /*y=var:23*/: default /*y=undef*/ : /*y=var:23*/
println(y) println(y)
} }
/*y=undef*/ /*y=undef*/
switch int := i.(type) { switch int := i.(type) {
case /*int=typename:0*/ int /*int=var:31*/ : case /*int=typename:0*/ int /*int=typename:0*/ : /*int=var:31*/
println(int) println(int)
default /*int=var:31*/ : default /*int=typename:0*/ : /*int=var:31*/ ;
} }
_ = param1 _ = param1

View File

@ -763,13 +763,7 @@ func (check *Checker) stmt(ctxt stmtContext, s ast.Stmt) {
// 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.Name, T) obj := NewVar(lhs.Pos(), check.pkg, lhs.Name, T)
// TODO(mdempsky): Just use clause.Colon? Why did I even suggest check.declare(check.scope, nil, obj, clause.Colon)
// "at the end of the TypeSwitchCase" in go.dev/issue/16794 instead?
scopePos := clause.Pos() + token.Pos(len("default")) // for default clause (len(List) == 0)
if n := len(clause.List); n > 0 {
scopePos = clause.List[n-1].End()
}
check.declare(check.scope, nil, obj, scopePos)
check.recordImplicit(clause, obj) check.recordImplicit(clause, obj)
// For the "declared and not used" error, all lhs variables act as // For the "declared and not used" error, all lhs variables act as
// one; i.e., if any one of them is 'used', all of them are 'used'. // one; i.e., if any one of them is 'used', all of them are 'used'.