]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.18] cmd/compile/internal/types2: use correct value of iota
authorRobert Griesemer <gri@golang.org>
Tue, 19 Apr 2022 20:46:15 +0000 (13:46 -0700)
committerDmitri Shuralyov <dmitshur@golang.org>
Mon, 2 May 2022 20:59:21 +0000 (20:59 +0000)
Fixes #52441.

Change-Id: I5cbf8c448dba037e9e0c5fe8f209401d6bf7d43f
Reviewed-on: https://go-review.googlesource.com/c/go/+/401134
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-on: https://go-review.googlesource.com/c/go/+/401174
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>

src/cmd/compile/internal/types2/decl.go
src/cmd/compile/internal/types2/resolver.go
src/cmd/compile/internal/types2/testdata/check/const0.src
src/go/types/testdata/check/const0.src
test/fixedbugs/issue52438.go [new file with mode: 0644]

index 579fa55e5911888b06090503f0ef95f5e1f37664..17453f31fa9910857a0d1c8632191d3f1efedcf5 100644 (file)
@@ -735,7 +735,7 @@ func (check *Checker) declStmt(list []syntax.Decl) {
                        top := len(check.delayed)
 
                        // iota is the index of the current constDecl within the group
-                       if first < 0 || list[index-1].(*syntax.ConstDecl).Group != s.Group {
+                       if first < 0 || s.Group == nil || list[index-1].(*syntax.ConstDecl).Group != s.Group {
                                first = index
                                last = nil
                        }
index 61963cb0437eeda5685a5a3595801ae09a348fe3..10dd86fea2bbaafcb3d8ad22f40153767a7696d5 100644 (file)
@@ -339,7 +339,7 @@ func (check *Checker) collectObjects() {
 
                        case *syntax.ConstDecl:
                                // iota is the index of the current constDecl within the group
-                               if first < 0 || file.DeclList[index-1].(*syntax.ConstDecl).Group != s.Group {
+                               if first < 0 || s.Group == nil || file.DeclList[index-1].(*syntax.ConstDecl).Group != s.Group {
                                        first = index
                                        last = nil
                                }
index 3cffdf904c87feb265ee5b1293d07be185e17372..229c248643dd748fe12e774d8d8df8919d368036 100644 (file)
@@ -349,6 +349,25 @@ const _ = unsafe.Sizeof(func() {
        assert(iota == 0)
 })
 
+// issue #52438
+const i1 = iota
+const i2 = iota
+const i3 = iota
+
+func _() {
+       assert(i1 == 0)
+       assert(i2 == 0)
+       assert(i3 == 0)
+
+       const i4 = iota
+       const i5 = iota
+       const i6 = iota
+
+       assert(i4 == 0)
+       assert(i5 == 0)
+       assert(i6 == 0)
+}
+
 // untyped constants must not get arbitrarily large
 const prec = 512 // internal maximum precision for integers
 const maxInt = (1<<(prec/2) - 1) * (1<<(prec/2) + 1) // == 1<<prec - 1
index 3cffdf904c87feb265ee5b1293d07be185e17372..229c248643dd748fe12e774d8d8df8919d368036 100644 (file)
@@ -349,6 +349,25 @@ const _ = unsafe.Sizeof(func() {
        assert(iota == 0)
 })
 
+// issue #52438
+const i1 = iota
+const i2 = iota
+const i3 = iota
+
+func _() {
+       assert(i1 == 0)
+       assert(i2 == 0)
+       assert(i3 == 0)
+
+       const i4 = iota
+       const i5 = iota
+       const i6 = iota
+
+       assert(i4 == 0)
+       assert(i5 == 0)
+       assert(i6 == 0)
+}
+
 // untyped constants must not get arbitrarily large
 const prec = 512 // internal maximum precision for integers
 const maxInt = (1<<(prec/2) - 1) * (1<<(prec/2) + 1) // == 1<<prec - 1
diff --git a/test/fixedbugs/issue52438.go b/test/fixedbugs/issue52438.go
new file mode 100644 (file)
index 0000000..375e727
--- /dev/null
@@ -0,0 +1,39 @@
+// run
+
+// Copyright 2022 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 main
+
+const c1 = iota
+const c2 = iota
+
+const c3 = 0 + iota<<8
+const c4 = 1 + iota<<8
+
+func main() {
+       if c1 != 0 {
+               panic(c1)
+       }
+       if c2 != 0 {
+               panic(c2)
+       }
+
+       if c3 != 0 {
+               panic(c3)
+       }
+       if c4 != 1 {
+               panic(c4)
+       }
+
+       const c5 = iota
+       const c6 = iota
+
+       if c5 != 0 {
+               panic(c5)
+       }
+       if c6 != 0 {
+               panic(c6)
+       }
+}