[go] reflect: document that Seq and Seq2 panic on unexported values

3 views
Skip to first unread message

Cyber Hamza (Gerrit)

unread,
Feb 19, 2026, 2:24:54 AM (6 days ago) Feb 19
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Cyber Hamza has uploaded the change for review

Commit message

reflect: document that Seq and Seq2 panic on unexported values

Fixes #74377
Change-Id: Iefa372c43581504b1d9f8ae7629d73a5cd94c4b7

Change diff

diff --git a/src/reflect/iter.go b/src/reflect/iter.go
index 03df87b..9188cbe 100644
--- a/src/reflect/iter.go
+++ b/src/reflect/iter.go
@@ -35,6 +35,7 @@
// Otherwise v's kind must be Int, Int8, Int16, Int32, Int64,
// Uint, Uint8, Uint16, Uint32, Uint64, Uintptr,
// Array, Chan, Map, Slice, or String.
+// It panics if the Value was obtained by accessing unexported struct fields.
func (v Value) Seq() iter.Seq[Value] {
if canRangeFunc(v.abiType()) {
return func(yield func(Value) bool) {
@@ -121,6 +122,7 @@
// that takes a single argument of type func(K, V) bool for some type K, V.
// If v's kind is Pointer, the pointer element type must have kind Array.
// Otherwise v's kind must be Array, Map, Slice, or String.
+// It panics if the Value was obtained by accessing unexported struct fields.
func (v Value) Seq2() iter.Seq2[Value, Value] {
if canRangeFunc2(v.abiType()) {
return func(yield func(Value, Value) bool) {
diff --git a/src/reflect/iter_test.go b/src/reflect/iter_test.go
index 668d665..3a02b0d 100644
--- a/src/reflect/iter_test.go
+++ b/src/reflect/iter_test.go
@@ -410,3 +410,33 @@

// For Type.CanSeq2 test.
func (methodIter2) NonSeq2(yield func(int, int)) {}
+
+func TestSeqPanic(t *testing.T) {
+ // Seq and Seq2 panic if the value is unexported.
+ type s struct {
+ f1 func(int) bool
+ f2 func(int, int) bool
+ }
+ v := reflect.ValueOf(s{
+ f1: func(int) bool { return true },
+ f2: func(int, int) bool { return true },
+ })
+
+ t.Run("Seq", func(t *testing.T) {
+ defer func() {
+ if recover() == nil {
+ t.Error("Seq did not panic on unexported value")
+ }
+ }()
+ v.FieldByName("f1").Seq()
+ })
+
+ t.Run("Seq2", func(t *testing.T) {
+ defer func() {
+ if recover() == nil {
+ t.Error("Seq2 did not panic on unexported value")
+ }
+ }()
+ v.FieldByName("f2").Seq2()
+ })
+}

Change information

Files:
  • M src/reflect/iter.go
  • M src/reflect/iter_test.go
Change size: S
Delta: 2 files changed, 32 insertions(+), 0 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Iefa372c43581504b1d9f8ae7629d73a5cd94c4b7
Gerrit-Change-Number: 746920
Gerrit-PatchSet: 1
Gerrit-Owner: Cyber Hamza <cyberha...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Gopher Robot (Gerrit)

unread,
Feb 19, 2026, 2:29:22 AM (6 days ago) Feb 19
to Cyber Hamza, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Message from Gopher Robot

Congratulations on opening your first change. Thank you for your contribution!

Next steps:
A maintainer will review your change and provide feedback. See
https://go.dev/doc/contribute#review for more info and tips to get your
patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.

During May-July and Nov-Jan the Go project is in a code freeze, during which
little code gets reviewed or merged. If a reviewer responds with a comment like
R=go1.11 or adds a tag like "wait-release", it means that this CL will be
reviewed as part of the next development cycle. See https://go.dev/s/release
for more details.

Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Iefa372c43581504b1d9f8ae7629d73a5cd94c4b7
Gerrit-Change-Number: 746920
Gerrit-PatchSet: 1
Gerrit-Owner: Cyber Hamza <cyberha...@gmail.com>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Comment-Date: Thu, 19 Feb 2026 07:29:17 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: No
unsatisfied_requirement
satisfied_requirement
open
diffy

Cherry Mui (Gerrit)

unread,
Feb 19, 2026, 9:18:39 AM (6 days ago) Feb 19
to Cyber Hamza, goph...@pubsubhelper.golang.org, Russ Cox, Ian Lance Taylor, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Cyber Hamza, Ian Lance Taylor and Russ Cox

Cherry Mui voted and added 1 comment

Votes added by Cherry Mui

Hold+1

1 comment

File src/reflect/iter.go
Line 38, Patchset 1 (Latest):// It panics if the Value was obtained by accessing unexported struct fields.
Cherry Mui . unresolved

This is not correct. See the discussion in CL 690616. Thanks.

Open in Gerrit

Related details

Attention is currently required from:
  • Cyber Hamza
  • Ian Lance Taylor
  • Russ Cox
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Holds
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Iefa372c43581504b1d9f8ae7629d73a5cd94c4b7
    Gerrit-Change-Number: 746920
    Gerrit-PatchSet: 1
    Gerrit-Owner: Cyber Hamza <cyberha...@gmail.com>
    Gerrit-Reviewer: Cherry Mui <cher...@google.com>
    Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Reviewer: Russ Cox <r...@golang.org>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Attention: Russ Cox <r...@golang.org>
    Gerrit-Attention: Cyber Hamza <cyberha...@gmail.com>
    Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Comment-Date: Thu, 19 Feb 2026 14:18:34 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: Yes
    unsatisfied_requirement
    open
    diffy

    Cyber Hamza (Gerrit)

    unread,
    Feb 19, 2026, 11:53:20 PM (5 days ago) Feb 19
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
    Attention needed from Cyber Hamza, Ian Lance Taylor and Russ Cox

    Cyber Hamza uploaded new patchset

    Cyber Hamza uploaded patch set #2 to this change.
    Open in Gerrit

    Related details

    Attention is currently required from:
    • Cyber Hamza
    • Ian Lance Taylor
    • Russ Cox
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Holds
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: newpatchset
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Iefa372c43581504b1d9f8ae7629d73a5cd94c4b7
    Gerrit-Change-Number: 746920
    Gerrit-PatchSet: 2
    unsatisfied_requirement
    open
    diffy

    Cyber Hamza (Gerrit)

    unread,
    Feb 19, 2026, 11:58:42 PM (5 days ago) Feb 19
    to goph...@pubsubhelper.golang.org, Cherry Mui, Russ Cox, Ian Lance Taylor, Gopher Robot, golang-co...@googlegroups.com
    Attention needed from Ian Lance Taylor and Russ Cox

    Cyber Hamza added 1 comment

    Patchset-level comments
    File-level comment, Patchset 2 (Latest):
    Cyber Hamza . resolved

    Thank you for the feedback! I've updated the documentation to only mention Func and Chan kinds, per the discussion in CL 690616. I also fixed the test to properly trigger the panic by iterating over the returned function, not just calling Seq()/Seq2(). Please let me know if you'd prefer CL 690616 to handle this instead — happy to defer.

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Ian Lance Taylor
    • Russ Cox
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Holds
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Iefa372c43581504b1d9f8ae7629d73a5cd94c4b7
    Gerrit-Change-Number: 746920
    Gerrit-PatchSet: 2
    Gerrit-Owner: Cyber Hamza <cyberha...@gmail.com>
    Gerrit-Reviewer: Cherry Mui <cher...@google.com>
    Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Reviewer: Russ Cox <r...@golang.org>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Attention: Russ Cox <r...@golang.org>
    Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Comment-Date: Fri, 20 Feb 2026 04:58:36 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    open
    diffy

    Cherry Mui (Gerrit)

    unread,
    Feb 24, 2026, 9:21:51 PM (8 hours ago) Feb 24
    to Cyber Hamza, goph...@pubsubhelper.golang.org, Russ Cox, Ian Lance Taylor, Gopher Robot, golang-co...@googlegroups.com
    Attention needed from Cyber Hamza, Ian Lance Taylor and Russ Cox

    Cherry Mui voted and added 1 comment

    Votes added by Cherry Mui

    Hold+1

    1 comment

    Patchset-level comments
    Cyber Hamza . unresolved

    Thank you for the feedback! I've updated the documentation to only mention Func and Chan kinds, per the discussion in CL 690616. I also fixed the test to properly trigger the panic by iterating over the returned function, not just calling Seq()/Seq2(). Please let me know if you'd prefer CL 690616 to handle this instead — happy to defer.

    Cherry Mui

    The added documentation still says it panics in Seq and Seq2, which doesn't match the current behavior.

    Also, the test does not distinguish whether it panics in Seq/Seq2, or later when iterating them. If we want to be accurate, we probably want to have a test that can tell them apart.

    Still, we probably want to decide the behavior first.

    Thanks.

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Cyber Hamza
    • Ian Lance Taylor
    • Russ Cox
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Holds
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Iefa372c43581504b1d9f8ae7629d73a5cd94c4b7
    Gerrit-Change-Number: 746920
    Gerrit-PatchSet: 2
    Gerrit-Owner: Cyber Hamza <cyberha...@gmail.com>
    Gerrit-Reviewer: Cherry Mui <cher...@google.com>
    Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Reviewer: Russ Cox <r...@golang.org>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Attention: Russ Cox <r...@golang.org>
    Gerrit-Attention: Cyber Hamza <cyberha...@gmail.com>
    Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Comment-Date: Wed, 25 Feb 2026 02:21:48 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: Yes
    Comment-In-Reply-To: Cyber Hamza <cyberha...@gmail.com>
    unsatisfied_requirement
    open
    diffy
    Reply all
    Reply to author
    Forward
    0 new messages