]> Cypherpunks.ru repositories - gostls13.git/commitdiff
go/types, types2: avoid spurious "declared and not used" error
authorRobert Griesemer <gri@golang.org>
Wed, 21 Jun 2023 18:14:34 +0000 (11:14 -0700)
committerGopher Robot <gobot@golang.org>
Wed, 21 Jun 2023 18:32:13 +0000 (18:32 +0000)
Fixes #60906.

Change-Id: Iba117b36041f72a54ce82cc914f8fa3b07a6fb2e
Reviewed-on: https://go-review.googlesource.com/c/go/+/504877
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>

src/cmd/compile/internal/types2/index.go
src/go/types/index.go
src/internal/types/testdata/fixedbugs/issue60906.go [new file with mode: 0644]

index 4fbe064da60d4a1fe96e4d90a10672b50e4ba3e6..3ebe85135507a8378b8e00125779e45482a9b42c 100644 (file)
@@ -184,6 +184,7 @@ func (check *Checker) indexExpr(x *operand, e *syntax.IndexExpr) (isFuncInst boo
 
        if !valid {
                check.errorf(e.Pos(), NonSliceableOperand, invalidOp+"cannot index %s", x)
+               check.use(e.Index)
                x.mode = invalid
                return false
        }
index 1bcfb38feb257ac24402d2e3a00e174232fb8013..c1c0f40e878d49d18cc86085b32e7541cacab76a 100644 (file)
@@ -186,6 +186,7 @@ func (check *Checker) indexExpr(x *operand, e *typeparams.IndexExpr) (isFuncInst
        if !valid {
                // types2 uses the position of '[' for the error
                check.errorf(x, NonIndexableOperand, invalidOp+"cannot index %s", x)
+               check.use(e.Indices...)
                x.mode = invalid
                return false
        }
diff --git a/src/internal/types/testdata/fixedbugs/issue60906.go b/src/internal/types/testdata/fixedbugs/issue60906.go
new file mode 100644 (file)
index 0000000..2744e89
--- /dev/null
@@ -0,0 +1,11 @@
+// Copyright 2023 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 _() {
+       var x int
+       var f func() []int
+       _ = f /* ERROR "cannot index f" */ [x]
+}