]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: fix iota undefined after ConstDecl inside function in ConstSpec
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Wed, 11 Sep 2019 08:51:28 +0000 (15:51 +0700)
committerRobert Griesemer <gri@golang.org>
Thu, 12 Sep 2019 04:15:00 +0000 (04:15 +0000)
When reaching const declaration, Checker override context iota to use
correct iota value, but does not restore the old value when exit, and
always set context iota to nil. It ends up with undefined iota after
const declaration.

To fix it, preserve the original iota value and restore it after const
declaration.

Fixes #34228

Change-Id: I42d5efb55a57e5ddc369bb72d31f1f039c92361c
Reviewed-on: https://go-review.googlesource.com/c/go/+/194737
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/go/types/decl.go
src/go/types/testdata/const0.src

index 1e2790a1711cf5d86da2d5dc66c6c060fcc54cce..a13442c95104b05f743d841ceb634d7e22068f7c 100644 (file)
@@ -333,8 +333,8 @@ func (check *Checker) constDecl(obj *Const, typ, init ast.Expr) {
        assert(obj.typ == nil)
 
        // use the correct value of iota
+       defer func(iota constant.Value) { check.iota = iota }(check.iota)
        check.iota = obj.val
-       defer func() { check.iota = nil }()
 
        // provide valid constant value under all circumstances
        obj.val = constant.MakeUnknown()
index 19fb1bdbbeea765796722dd8e51d08345840bc6c..adbbf2863b03e55e0dfaeea860551f88dd7f1b1e 100644 (file)
@@ -308,6 +308,8 @@ const (
                        _ = unsafe.Sizeof([iota-1]int{} == x) // assert types are equal
                        _ = unsafe.Sizeof([Two]int{} == x)    // assert types are equal
                )
+               var z [iota]int                           // [2]int
+               _ = unsafe.Sizeof([2]int{} == z)          // assert types are equal
        })
        three = iota // the sequence continues
 )
@@ -334,3 +336,15 @@ var _ = []int64{
        1 * 1e9,
        5 * 1e9,
 }
+
+const _ = unsafe.Sizeof(func() {
+       const _ = 0
+       _ = iota
+
+       const (
+          zero = iota
+          one
+       )
+       assert(one == 1)
+       assert(iota == 0)
+})