Skip to content

Commit 94274d0

Browse files
griesemerdmitshur
authored andcommitted
[release-branch.go1.18] cmd/compile/internal/types2: use correct value of iota
Fixes #52441. Change-Id: I5cbf8c448dba037e9e0c5fe8f209401d6bf7d43f Reviewed-on: https://go-review.googlesource.com/c/go/+/401134 Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/go/+/401174 TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Robert Griesemer <[email protected]>
1 parent 24fcbb9 commit 94274d0

File tree

5 files changed

+79
-2
lines changed

5 files changed

+79
-2
lines changed

src/cmd/compile/internal/types2/decl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ func (check *Checker) declStmt(list []syntax.Decl) {
735735
top := len(check.delayed)
736736

737737
// iota is the index of the current constDecl within the group
738-
if first < 0 || list[index-1].(*syntax.ConstDecl).Group != s.Group {
738+
if first < 0 || s.Group == nil || list[index-1].(*syntax.ConstDecl).Group != s.Group {
739739
first = index
740740
last = nil
741741
}

src/cmd/compile/internal/types2/resolver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ func (check *Checker) collectObjects() {
339339

340340
case *syntax.ConstDecl:
341341
// iota is the index of the current constDecl within the group
342-
if first < 0 || file.DeclList[index-1].(*syntax.ConstDecl).Group != s.Group {
342+
if first < 0 || s.Group == nil || file.DeclList[index-1].(*syntax.ConstDecl).Group != s.Group {
343343
first = index
344344
last = nil
345345
}

src/cmd/compile/internal/types2/testdata/check/const0.src

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,25 @@ const _ = unsafe.Sizeof(func() {
349349
assert(iota == 0)
350350
})
351351

352+
// issue #52438
353+
const i1 = iota
354+
const i2 = iota
355+
const i3 = iota
356+
357+
func _() {
358+
assert(i1 == 0)
359+
assert(i2 == 0)
360+
assert(i3 == 0)
361+
362+
const i4 = iota
363+
const i5 = iota
364+
const i6 = iota
365+
366+
assert(i4 == 0)
367+
assert(i5 == 0)
368+
assert(i6 == 0)
369+
}
370+
352371
// untyped constants must not get arbitrarily large
353372
const prec = 512 // internal maximum precision for integers
354373
const maxInt = (1<<(prec/2) - 1) * (1<<(prec/2) + 1) // == 1<<prec - 1

src/go/types/testdata/check/const0.src

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,25 @@ const _ = unsafe.Sizeof(func() {
349349
assert(iota == 0)
350350
})
351351

352+
// issue #52438
353+
const i1 = iota
354+
const i2 = iota
355+
const i3 = iota
356+
357+
func _() {
358+
assert(i1 == 0)
359+
assert(i2 == 0)
360+
assert(i3 == 0)
361+
362+
const i4 = iota
363+
const i5 = iota
364+
const i6 = iota
365+
366+
assert(i4 == 0)
367+
assert(i5 == 0)
368+
assert(i6 == 0)
369+
}
370+
352371
// untyped constants must not get arbitrarily large
353372
const prec = 512 // internal maximum precision for integers
354373
const maxInt = (1<<(prec/2) - 1) * (1<<(prec/2) + 1) // == 1<<prec - 1

test/fixedbugs/issue52438.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// run
2+
3+
// Copyright 2022 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package main
8+
9+
const c1 = iota
10+
const c2 = iota
11+
12+
const c3 = 0 + iota<<8
13+
const c4 = 1 + iota<<8
14+
15+
func main() {
16+
if c1 != 0 {
17+
panic(c1)
18+
}
19+
if c2 != 0 {
20+
panic(c2)
21+
}
22+
23+
if c3 != 0 {
24+
panic(c3)
25+
}
26+
if c4 != 1 {
27+
panic(c4)
28+
}
29+
30+
const c5 = iota
31+
const c6 = iota
32+
33+
if c5 != 0 {
34+
panic(c5)
35+
}
36+
if c6 != 0 {
37+
panic(c6)
38+
}
39+
}

0 commit comments

Comments
 (0)