]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: make for loops with range statements not terminating
authorKeith Randall <khr@golang.org>
Fri, 15 Oct 2021 15:06:58 +0000 (08:06 -0700)
committerKeith Randall <khr@golang.org>
Fri, 15 Oct 2021 19:54:44 +0000 (19:54 +0000)
Fixes #49003

Change-Id: If09c6f028dce5440b1be238612653ffdd626113a
Reviewed-on: https://go-review.googlesource.com/c/go/+/356189
Trust: Keith Randall <khr@golang.org>
Reviewed-by: roger peppe <rogpeppe@gmail.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
doc/go_spec.html
src/cmd/compile/internal/types2/return.go
test/fixedbugs/issue49003.go [new file with mode: 0644]

index c8051f58af4fb4bad294b50c043f0477b5814c97..46eebb57136d0bcb56566c6a46cf3f72c54965ec 100644 (file)
@@ -1,6 +1,6 @@
 <!--{
        "Title": "The Go Programming Language Specification",
-       "Subtitle": "Version of Sep 16, 2021",
+       "Subtitle": "Version of Oct 15, 2021",
        "Path": "/ref/spec"
 }-->
 
@@ -4598,7 +4598,8 @@ a <a href="#Blocks">block</a>. The following statements are terminating:
        A <a href="#For_statements">"for" statement</a> in which:
        <ul>
        <li>there are no "break" statements referring to the "for" statement, and</li>
-       <li>the loop condition is absent.</li>
+       <li>the loop condition is absent, and</li>
+       <li>the "for" statement does not use a range clause.</li>
        </ul>
 </li>
 
index 204e456a916fb127e02b3fa9aee0f6c41cd5971b..6c3e1842ce7d715b93d350ff58cc1e3738e4cf0d 100644 (file)
@@ -62,6 +62,11 @@ func (check *Checker) isTerminating(s syntax.Stmt, label string) bool {
                return true
 
        case *syntax.ForStmt:
+               if _, ok := s.Init.(*syntax.RangeClause); ok {
+                       // Range clauses guarantee that the loop terminates,
+                       // so the loop is not a terminating statement. See issue 49003.
+                       break
+               }
                if s.Cond == nil && !hasBreak(s.Body, label, true) {
                        return true
                }
diff --git a/test/fixedbugs/issue49003.go b/test/fixedbugs/issue49003.go
new file mode 100644 (file)
index 0000000..da6c19b
--- /dev/null
@@ -0,0 +1,12 @@
+// errorcheck
+
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package p
+
+func f(s string) int {
+       for range s {
+       }
+} // ERROR "missing return"