From: Robert Griesemer Date: Wed, 21 Jun 2023 18:14:34 +0000 (-0700) Subject: go/types, types2: avoid spurious "declared and not used" error X-Git-Tag: go1.21rc3~2^2~78 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=b1f1fb2950814ae2b69e64537ecd96f13ae32627;p=gostls13.git go/types, types2: avoid spurious "declared and not used" error Fixes #60906. Change-Id: Iba117b36041f72a54ce82cc914f8fa3b07a6fb2e Reviewed-on: https://go-review.googlesource.com/c/go/+/504877 TryBot-Result: Gopher Robot Reviewed-by: Robert Findley Run-TryBot: Robert Griesemer Reviewed-by: Robert Griesemer Auto-Submit: Robert Griesemer --- diff --git a/src/cmd/compile/internal/types2/index.go b/src/cmd/compile/internal/types2/index.go index 4fbe064da6..3ebe851355 100644 --- a/src/cmd/compile/internal/types2/index.go +++ b/src/cmd/compile/internal/types2/index.go @@ -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 } diff --git a/src/go/types/index.go b/src/go/types/index.go index 1bcfb38feb..c1c0f40e87 100644 --- a/src/go/types/index.go +++ b/src/go/types/index.go @@ -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 index 0000000000..2744e89455 --- /dev/null +++ b/src/internal/types/testdata/fixedbugs/issue60906.go @@ -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] +}