From 104742fddae061b52c38e221697cf20ebd09bf10 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Tue, 19 Apr 2022 13:46:15 -0700 Subject: [PATCH] cmd/compile/internal/types2: use correct value of iota Fixes #52438. Change-Id: I5cbf8c448dba037e9e0c5fe8f209401d6bf7d43f Reviewed-on: https://go-review.googlesource.com/c/go/+/401134 Reviewed-by: Ian Lance Taylor Reviewed-by: Robert Griesemer --- src/cmd/compile/internal/types2/decl.go | 2 +- src/cmd/compile/internal/types2/resolver.go | 2 +- .../internal/types2/testdata/check/const0.go | 19 +++++++++ src/go/types/testdata/check/const0.go | 19 +++++++++ test/fixedbugs/issue52438.go | 39 +++++++++++++++++++ 5 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 test/fixedbugs/issue52438.go diff --git a/src/cmd/compile/internal/types2/decl.go b/src/cmd/compile/internal/types2/decl.go index 95143cbed5..4f28c362c7 100644 --- a/src/cmd/compile/internal/types2/decl.go +++ b/src/cmd/compile/internal/types2/decl.go @@ -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 } diff --git a/src/cmd/compile/internal/types2/resolver.go b/src/cmd/compile/internal/types2/resolver.go index 5c64ecdfc8..5d498b6b2b 100644 --- a/src/cmd/compile/internal/types2/resolver.go +++ b/src/cmd/compile/internal/types2/resolver.go @@ -340,7 +340,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 } diff --git a/src/cmd/compile/internal/types2/testdata/check/const0.go b/src/cmd/compile/internal/types2/testdata/check/const0.go index 3cffdf904c..229c248643 100644 --- a/src/cmd/compile/internal/types2/testdata/check/const0.go +++ b/src/cmd/compile/internal/types2/testdata/check/const0.go @@ -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<