]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: use ":" as start of implicit type switch case scopes
authorRobert Griesemer <gri@golang.org>
Thu, 13 Jun 2024 23:24:26 +0000 (16:24 -0700)
committerGopher Robot <gobot@golang.org>
Fri, 26 Jul 2024 20:11:09 +0000 (20:11 +0000)
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>

src/cmd/compile/internal/types2/api_test.go
src/cmd/compile/internal/types2/stmt.go
src/go/types/api_test.go
src/go/types/stmt.go

index 5126ac51116cd92daf357c8a5c1acce8ac04e1d0..f23c50ba4612388705061f2b234c4ac9255a173f 100644 (file)
@@ -1884,6 +1884,9 @@ func TestScopeLookupParent(t *testing.T) {
        // Each /*name=kind:line*/ comment makes the test look up the
        // name at that point and checks that it resolves to a decl of
        // 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 := `
 /*lib=pkgname:5*/ /*X=var:1*/ /*Pi=const:8*/ /*T=typename:9*/ /*Y=var:10*/ /*F=func:12*/
 package main
@@ -1907,17 +1910,17 @@ func F[T *U, U any](param1, param2 int) /*param1=undef*/ (res1 /*res1=undef*/, r
 
        var i interface{}
        switch y := i.(type) { /*y=undef*/
-       case /*y=undef*/ int /*y=var:23*/ :
-       case float32, /*y=undef*/ float64 /*y=var:23*/ :
-       default /*y=var:23*/:
+       case /*y=undef*/ int /*y=undef*/ : /*y=var:23*/ ;
+       case float32, /*y=undef*/ float64 /*y=undef*/ : /*y=var:23*/ ;
+       default /*y=undef*/ : /*y=var:23*/
                println(y)
        }
        /*y=undef*/
 
         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)
-        default /*int=var:31*/ :
+        default /*int=typename:0*/ : /*int=var:31*/ ;
         }
 
        _ = param1
index b471fb1f348cd694adcda2d7be47a19c32fc3b1a..e0e4ee6a0277e9e4abf4f5cf25c0775f048e12d3 100644 (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 != nil {
                        obj := NewVar(lhs.Pos(), check.pkg, lhs.Value, T)
-                       // TODO(mdempsky): Just use clause.Colon? Why did I even suggest
-                       // "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.declare(check.scope, nil, obj, clause.Colon)
                        check.recordImplicit(clause, obj)
                        // 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'.
index beed94f35579964cc12c4f5ff3c04f1ca38f4af5..0854a119c2462ed95bad750157bbdb4c72acdc4b 100644 (file)
@@ -1885,6 +1885,9 @@ func TestScopeLookupParent(t *testing.T) {
        // Each /*name=kind:line*/ comment makes the test look up the
        // name at that point and checks that it resolves to a decl of
        // 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 := `
 /*lib=pkgname:5*/ /*X=var:1*/ /*Pi=const:8*/ /*T=typename:9*/ /*Y=var:10*/ /*F=func:12*/
 package main
@@ -1908,17 +1911,17 @@ func F[T *U, U any](param1, param2 int) /*param1=undef*/ (res1 /*res1=undef*/, r
 
        var i interface{}
        switch y := i.(type) { /*y=undef*/
-       case /*y=undef*/ int /*y=var:23*/ :
-       case float32, /*y=undef*/ float64 /*y=var:23*/ :
-       default /*y=var:23*/:
+       case /*y=undef*/ int /*y=undef*/ : /*y=var:23*/ ;
+       case float32, /*y=undef*/ float64 /*y=undef*/ : /*y=var:23*/ ;
+       default /*y=undef*/ : /*y=var:23*/
                println(y)
        }
        /*y=undef*/
 
         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)
-        default /*int=var:31*/ :
+        default /*int=typename:0*/ : /*int=var:31*/ ;
         }
 
        _ = param1
index 74a64f40aae434c5b6279eb2f577aa193d35df63..f8514fdbb79ee43e8ca752465982b0e1cdca6661 100644 (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 != nil {
                                obj := NewVar(lhs.Pos(), check.pkg, lhs.Name, T)
-                               // TODO(mdempsky): Just use clause.Colon? Why did I even suggest
-                               // "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.declare(check.scope, nil, obj, clause.Colon)
                                check.recordImplicit(clause, obj)
                                // 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'.