From: Keith Randall Date: Fri, 15 Oct 2021 15:06:58 +0000 (-0700) Subject: cmd/compile: make for loops with range statements not terminating X-Git-Tag: go1.18beta1~882 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=22951fbc89a1bc3c5cc38f4e71b4a682f5149361;p=gostls13.git cmd/compile: make for loops with range statements not terminating Fixes #49003 Change-Id: If09c6f028dce5440b1be238612653ffdd626113a Reviewed-on: https://go-review.googlesource.com/c/go/+/356189 Trust: Keith Randall Reviewed-by: roger peppe Reviewed-by: Matthew Dempsky Reviewed-by: Robert Griesemer Reviewed-by: Cuong Manh Le --- diff --git a/doc/go_spec.html b/doc/go_spec.html index c8051f58af..46eebb5713 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,6 +1,6 @@ @@ -4598,7 +4598,8 @@ a block. The following statements are terminating: A "for" statement in which:
  • there are no "break" statements referring to the "for" statement, and
  • -
  • the loop condition is absent.
  • +
  • the loop condition is absent, and
  • +
  • the "for" statement does not use a range clause.
diff --git a/src/cmd/compile/internal/types2/return.go b/src/cmd/compile/internal/types2/return.go index 204e456a91..6c3e1842ce 100644 --- a/src/cmd/compile/internal/types2/return.go +++ b/src/cmd/compile/internal/types2/return.go @@ -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 index 0000000000..da6c19b8cb --- /dev/null +++ b/test/fixedbugs/issue49003.go @@ -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"