[go] reflect: fix TypeAssert on nil interface values

5 views
Skip to first unread message

Joseph Tsai (Gerrit)

unread,
Jun 27, 2025, 2:11:20 PMJun 27
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Joseph Tsai has uploaded the change for review

Commit message

reflect: fix TypeAssert on nil interface values

In the Go language a type assertion of a nil interface value
will always report false:

var err error
v, ok := err.(error) // always reports (nil, false)

Consequently, assertion on a reflect.Value.Interface()
will also report false:

var err error
rv := ValueOf(&err).Elem()
v, ok := rv.Interface().(error) // reports (nil, false)

However, prior to this change, a TypeAssert would report true:

var err error
rv := ValueOf(&err).Elem()
v, ok := TypeAssert[error](rv) // reports (nil, true)

when it should report false.

This fixes TypeAssert to match the Go language by
pushing the typ != v.typ check to the very end after
we have validated that neither v nor T are interface kinds.

Fixes #74404
Change-Id: Ie14d5cf18c8370c3e27ce4bdf4570c89519d8a16

Change diff

diff --git a/src/reflect/all_test.go b/src/reflect/all_test.go
index fb1a29d..cd3e306 100644
--- a/src/reflect/all_test.go
+++ b/src/reflect/all_test.go
@@ -8719,6 +8719,11 @@
testTypeAssert(t, any(int(1)), int(1), true)
testTypeAssert(t, any(int(1)), byte(0), false)
testTypeAssert(t, fmt.Stringer(vv), vv, true)
+
+ testTypeAssert(t, any(nil), any(nil), false)
+ testTypeAssert(t, any(nil), error(nil), false)
+ testTypeAssert(t, error(nil), any(nil), false)
+ testTypeAssert(t, error(nil), error(nil), false)
}

func testTypeAssert[T comparable, V any](t *testing.T, val V, wantVal T, wantOk bool) {
diff --git a/src/reflect/value.go b/src/reflect/value.go
index 68b97e9..effe2f2 100644
--- a/src/reflect/value.go
+++ b/src/reflect/value.go
@@ -1514,46 +1514,46 @@
}

typ := abi.TypeFor[T]()
+
+ // Return the element inside the interface.
+ //
+ // T is a concrete type and v is an interface. For example:
+ //
+ // var v any = int(1)
+ // val := ValueOf(&v).Elem()
+ // TypeAssert[int](val) == val.Interface().(int)
+ //
+ // T is a interface and v is an interface, but the iface types are different. For example:
+ //
+ // var v any = &someError{}
+ // val := ValueOf(&v).Elem()
+ // TypeAssert[error](val) == val.Interface().(error)
+ //
+ // T is a interface and v is a nil interface value. For example:
+ //
+ // var v errror
+ // val := ValueOf(&v).Elem()
+ // TypeAssert[error](val) == val.Interface().(error)
+ if v.kind() == Interface {
+ v, ok := packIfaceValueIntoEmptyIface(v).(T)
+ return v, ok
+ }
+
+ // T is an interface, v is a concrete type. For example:
+ //
+ // TypeAssert[any](ValueOf(1)) == ValueOf(1).Interface().(any)
+ // TypeAssert[error](ValueOf(&someError{})) == ValueOf(&someError{}).Interface().(error)
+ if typ.Kind() == abi.Interface {
+ v, ok := packEface(v).(T)
+ return v, ok
+ }
+
+ // Both v and T must be concrete types.
+ // The only way for an type-assertion to match is if the types are equal.
if typ != v.typ() {
- // We can't just return false here:
- //
- // var zero T
- // return zero, false
- //
- // since this function should work in the same manner as v.Interface().(T) does.
- // Thus we have to handle two cases specially.
-
- // Return the element inside the interface.
- //
- // T is a concrete type and v is an interface. For example:
- //
- // var v any = int(1)
- // val := ValueOf(&v).Elem()
- // TypeAssert[int](val) == val.Interface().(int)
- //
- // T is a interface and v is an interface, but the iface types are different. For example:
- //
- // var v any = &someError{}
- // val := ValueOf(&v).Elem()
- // TypeAssert[error](val) == val.Interface().(error)
- if v.kind() == Interface {
- v, ok := packIfaceValueIntoEmptyIface(v).(T)
- return v, ok
- }
-
- // T is an interface, v is a concrete type. For example:
- //
- // TypeAssert[any](ValueOf(1)) == ValueOf(1).Interface().(any)
- // TypeAssert[error](ValueOf(&someError{})) == ValueOf(&someError{}).Interface().(error)
- if typ.Kind() == abi.Interface {
- v, ok := packEface(v).(T)
- return v, ok
- }
-
var zero T
return zero, false
}
-
if v.flag&flagIndir == 0 {
return *(*T)(unsafe.Pointer(&v.ptr)), true
}

Change information

Files:
  • M src/reflect/all_test.go
  • M src/reflect/value.go
Change size: M
Delta: 2 files changed, 41 insertions(+), 36 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: Ie14d5cf18c8370c3e27ce4bdf4570c89519d8a16
Gerrit-Change-Number: 684675
Gerrit-PatchSet: 1
Gerrit-Owner: Joseph Tsai <joe...@digital-static.net>
unsatisfied_requirement
satisfied_requirement
open
diffy

Joseph Tsai (Gerrit)

unread,
Jun 27, 2025, 2:12:35 PMJun 27
to goph...@pubsubhelper.golang.org, Cherry Mui, Keith Randall, golang-co...@googlegroups.com
Attention needed from Cherry Mui and Keith Randall

Joseph Tsai voted

Auto-Submit+1
Commit-Queue+1
Open in Gerrit

Related details

Attention is currently required from:
  • Cherry Mui
  • Keith Randall
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: Ie14d5cf18c8370c3e27ce4bdf4570c89519d8a16
Gerrit-Change-Number: 684675
Gerrit-PatchSet: 1
Gerrit-Owner: Joseph Tsai <joe...@digital-static.net>
Gerrit-Reviewer: Cherry Mui <cher...@google.com>
Gerrit-Reviewer: Joseph Tsai <joe...@digital-static.net>
Gerrit-Reviewer: Keith Randall <k...@google.com>
Gerrit-Attention: Keith Randall <k...@google.com>
Gerrit-Attention: Cherry Mui <cher...@google.com>
Gerrit-Comment-Date: Fri, 27 Jun 2025 18:12:29 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Mateusz Poliwczak (Gerrit)

unread,
Jun 27, 2025, 2:27:31 PMJun 27
to Joseph Tsai, goph...@pubsubhelper.golang.org, Go LUCI, Cherry Mui, Keith Randall, golang-co...@googlegroups.com
Attention needed from Cherry Mui, Joseph Tsai and Keith Randall

Mateusz Poliwczak added 1 comment

File src/reflect/value.go
Line 1526, Patchset 1 (Latest): // T is a interface and v is an interface, but the iface types are different. For example:
Mateusz Poliwczak . unresolved

I think that this is not true anymore.

Open in Gerrit

Related details

Attention is currently required from:
  • Cherry Mui
  • Joseph Tsai
  • Keith Randall
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • 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: Ie14d5cf18c8370c3e27ce4bdf4570c89519d8a16
    Gerrit-Change-Number: 684675
    Gerrit-PatchSet: 1
    Gerrit-Owner: Joseph Tsai <joe...@digital-static.net>
    Gerrit-Reviewer: Cherry Mui <cher...@google.com>
    Gerrit-Reviewer: Joseph Tsai <joe...@digital-static.net>
    Gerrit-Reviewer: Keith Randall <k...@google.com>
    Gerrit-CC: Mateusz Poliwczak <mpoliw...@gmail.com>
    Gerrit-Attention: Joseph Tsai <joe...@digital-static.net>
    Gerrit-Attention: Keith Randall <k...@google.com>
    Gerrit-Attention: Cherry Mui <cher...@google.com>
    Gerrit-Comment-Date: Fri, 27 Jun 2025 18:27:22 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    open
    diffy

    Mateusz Poliwczak (Gerrit)

    unread,
    Jun 27, 2025, 2:36:00 PMJun 27
    to Joseph Tsai, goph...@pubsubhelper.golang.org, Go LUCI, Cherry Mui, Keith Randall, golang-co...@googlegroups.com
    Attention needed from Cherry Mui, Joseph Tsai and Keith Randall

    Mateusz Poliwczak added 1 comment

    File src/reflect/value.go
    Line 1534, Patchset 1 (Latest): // var v errror
    Mateusz Poliwczak . unresolved
    ```suggestion
    // var v error
    ```
    Gerrit-Comment-Date: Fri, 27 Jun 2025 18:35:53 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    open
    diffy

    Joseph Tsai (Gerrit)

    unread,
    Jun 27, 2025, 3:12:05 PMJun 27
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
    Attention needed from Cherry Mui, Joseph Tsai and Keith Randall

    Joseph Tsai uploaded new patchset

    Joseph Tsai uploaded patch set #2 to this change.
    Following approvals got outdated and were removed:
    • TryBots-Pass: LUCI-TryBot-Result+1 by Go LUCI
    Open in Gerrit

    Related details

    Attention is currently required from:
    • Cherry Mui
    • Joseph Tsai
    • Keith Randall
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • 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: Ie14d5cf18c8370c3e27ce4bdf4570c89519d8a16
    Gerrit-Change-Number: 684675
    Gerrit-PatchSet: 2
    Gerrit-Owner: Joseph Tsai <joe...@digital-static.net>
    Gerrit-Reviewer: Cherry Mui <cher...@google.com>
    Gerrit-Reviewer: Joseph Tsai <joe...@digital-static.net>
    Gerrit-Reviewer: Keith Randall <k...@google.com>
    unsatisfied_requirement
    open
    diffy

    Joseph Tsai (Gerrit)

    unread,
    Jun 27, 2025, 3:12:07 PMJun 27
    to goph...@pubsubhelper.golang.org, Go LUCI, Mateusz Poliwczak, Cherry Mui, Keith Randall, golang-co...@googlegroups.com
    Attention needed from Cherry Mui, Keith Randall and Mateusz Poliwczak

    Joseph Tsai voted and added 2 comments

    Votes added by Joseph Tsai

    Auto-Submit+1
    Commit-Queue+1

    2 comments

    File src/reflect/value.go
    Line 1526, Patchset 1: // T is a interface and v is an interface, but the iface types are different. For example:
    Mateusz Poliwczak . resolved

    I think that this is not true anymore.

    Joseph Tsai

    Done

    Line 1534, Patchset 1: // var v errror
    Mateusz Poliwczak . resolved
    ```suggestion
    // var v error
    ```
    Joseph Tsai

    Done

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Cherry Mui
    • Keith Randall
    • Mateusz Poliwczak
    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: Ie14d5cf18c8370c3e27ce4bdf4570c89519d8a16
      Gerrit-Change-Number: 684675
      Gerrit-PatchSet: 1
      Gerrit-Owner: Joseph Tsai <joe...@digital-static.net>
      Gerrit-Reviewer: Cherry Mui <cher...@google.com>
      Gerrit-Reviewer: Joseph Tsai <joe...@digital-static.net>
      Gerrit-Reviewer: Keith Randall <k...@google.com>
      Gerrit-CC: Mateusz Poliwczak <mpoliw...@gmail.com>
      Gerrit-Attention: Keith Randall <k...@google.com>
      Gerrit-Attention: Mateusz Poliwczak <mpoliw...@gmail.com>
      Gerrit-Attention: Cherry Mui <cher...@google.com>
      Gerrit-Comment-Date: Fri, 27 Jun 2025 19:12:03 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: Yes
      Comment-In-Reply-To: Mateusz Poliwczak <mpoliw...@gmail.com>
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      Mateusz Poliwczak (Gerrit)

      unread,
      Jun 27, 2025, 3:28:10 PMJun 27
      to Joseph Tsai, goph...@pubsubhelper.golang.org, Go LUCI, Cherry Mui, Keith Randall, golang-co...@googlegroups.com
      Attention needed from Cherry Mui, Joseph Tsai and Keith Randall

      Mateusz Poliwczak voted and added 1 comment

      Votes added by Mateusz Poliwczak

      Code-Review+1

      1 comment

      Patchset-level comments
      File-level comment, Patchset 2 (Latest):
      Mateusz Poliwczak . resolved

      Thanks for the fix.

      Open in Gerrit

      Related details

      Attention is currently required from:
      • Cherry Mui
      • Joseph Tsai
      • Keith Randall
      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: Ie14d5cf18c8370c3e27ce4bdf4570c89519d8a16
      Gerrit-Change-Number: 684675
      Gerrit-PatchSet: 2
      Gerrit-Owner: Joseph Tsai <joe...@digital-static.net>
      Gerrit-Reviewer: Cherry Mui <cher...@google.com>
      Gerrit-Reviewer: Joseph Tsai <joe...@digital-static.net>
      Gerrit-Reviewer: Keith Randall <k...@google.com>
      Gerrit-Reviewer: Mateusz Poliwczak <mpoliw...@gmail.com>
      Gerrit-Attention: Joseph Tsai <joe...@digital-static.net>
      Gerrit-Attention: Keith Randall <k...@google.com>
      Gerrit-Attention: Cherry Mui <cher...@google.com>
      Gerrit-Comment-Date: Fri, 27 Jun 2025 19:28:02 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: Yes
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      Cherry Mui (Gerrit)

      unread,
      Jun 27, 2025, 3:28:36 PMJun 27
      to Joseph Tsai, goph...@pubsubhelper.golang.org, Mateusz Poliwczak, Go LUCI, Keith Randall, golang-co...@googlegroups.com
      Attention needed from Joseph Tsai and Keith Randall

      Cherry Mui voted and added 2 comments

      Votes added by Cherry Mui

      Code-Review+2

      2 comments

      Patchset-level comments
      Cherry Mui . resolved

      Thanks.

      File src/reflect/value.go
      Line 1518, Patchset 2 (Latest): // Return the element inside the interface.
      Cherry Mui . unresolved

      After the rearrange, this line perhaps read better with

      // If v is an interface, return the element inside the interface.

      Open in Gerrit

      Related details

      Attention is currently required from:
      • Joseph Tsai
      • Keith Randall
      Submit Requirements:
      • requirement satisfiedCode-Review
      • 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: Ie14d5cf18c8370c3e27ce4bdf4570c89519d8a16
      Gerrit-Change-Number: 684675
      Gerrit-PatchSet: 2
      Gerrit-Owner: Joseph Tsai <joe...@digital-static.net>
      Gerrit-Reviewer: Cherry Mui <cher...@google.com>
      Gerrit-Reviewer: Joseph Tsai <joe...@digital-static.net>
      Gerrit-Reviewer: Keith Randall <k...@google.com>
      Gerrit-Reviewer: Mateusz Poliwczak <mpoliw...@gmail.com>
      Gerrit-Attention: Joseph Tsai <joe...@digital-static.net>
      Gerrit-Attention: Keith Randall <k...@google.com>
      Gerrit-Comment-Date: Fri, 27 Jun 2025 19:28:32 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: Yes
      satisfied_requirement
      unsatisfied_requirement
      open
      diffy

      Joseph Tsai (Gerrit)

      unread,
      Jun 27, 2025, 5:30:43 PMJun 27
      to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
      Attention needed from Joseph Tsai and Keith Randall

      Joseph Tsai uploaded new patchset

      Joseph Tsai uploaded patch set #3 to this change.
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Joseph Tsai
      • Keith Randall
      Submit Requirements:
      • requirement satisfiedCode-Review
      • 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: Ie14d5cf18c8370c3e27ce4bdf4570c89519d8a16
      Gerrit-Change-Number: 684675
      Gerrit-PatchSet: 3
      satisfied_requirement
      unsatisfied_requirement
      open
      diffy

      Joseph Tsai (Gerrit)

      unread,
      Jun 27, 2025, 5:31:47 PMJun 27
      to goph...@pubsubhelper.golang.org, Cherry Mui, Mateusz Poliwczak, Go LUCI, Keith Randall, golang-co...@googlegroups.com
      Attention needed from Keith Randall

      Joseph Tsai voted and added 1 comment

      Votes added by Joseph Tsai

      Auto-Submit+1
      Commit-Queue+1

      1 comment

      File src/reflect/value.go
      Line 1518, Patchset 2: // Return the element inside the interface.
      Cherry Mui . resolved

      After the rearrange, this line perhaps read better with

      // If v is an interface, return the element inside the interface.

      Joseph Tsai

      Done

      Open in Gerrit

      Related details

      Attention is currently required from:
      • Keith Randall
      Submit Requirements:
      • requirement 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: Ie14d5cf18c8370c3e27ce4bdf4570c89519d8a16
      Gerrit-Change-Number: 684675
      Gerrit-PatchSet: 3
      Gerrit-Owner: Joseph Tsai <joe...@digital-static.net>
      Gerrit-Reviewer: Cherry Mui <cher...@google.com>
      Gerrit-Reviewer: Joseph Tsai <joe...@digital-static.net>
      Gerrit-Reviewer: Keith Randall <k...@google.com>
      Gerrit-Reviewer: Mateusz Poliwczak <mpoliw...@gmail.com>
      Gerrit-Attention: Keith Randall <k...@google.com>
      Gerrit-Comment-Date: Fri, 27 Jun 2025 21:31:42 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: Yes
      Comment-In-Reply-To: Cherry Mui <cher...@google.com>
      satisfied_requirement
      unsatisfied_requirement
      open
      diffy

      Dmitri Shuralyov (Gerrit)

      unread,
      Jun 27, 2025, 7:34:21 PMJun 27
      to Joseph Tsai, goph...@pubsubhelper.golang.org, Dmitri Shuralyov, Go LUCI, Cherry Mui, Mateusz Poliwczak, Keith Randall, golang-co...@googlegroups.com
      Attention needed from Keith Randall

      Dmitri Shuralyov voted Code-Review+1

      Code-Review+1
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Keith Randall
      Submit Requirements:
        • requirement satisfiedCode-Review
        • requirement satisfiedNo-Unresolved-Comments
        • requirement satisfiedReview-Enforcement
        • requirement 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: Ie14d5cf18c8370c3e27ce4bdf4570c89519d8a16
        Gerrit-Change-Number: 684675
        Gerrit-PatchSet: 3
        Gerrit-Owner: Joseph Tsai <joe...@digital-static.net>
        Gerrit-Reviewer: Cherry Mui <cher...@google.com>
        Gerrit-Reviewer: Dmitri Shuralyov <dmit...@google.com>
        Gerrit-Reviewer: Joseph Tsai <joe...@digital-static.net>
        Gerrit-Reviewer: Keith Randall <k...@google.com>
        Gerrit-Reviewer: Mateusz Poliwczak <mpoliw...@gmail.com>
        Gerrit-CC: Dmitri Shuralyov <dmit...@golang.org>
        Gerrit-Attention: Keith Randall <k...@google.com>
        Gerrit-Comment-Date: Fri, 27 Jun 2025 23:34:17 +0000
        Gerrit-HasComments: No
        Gerrit-Has-Labels: Yes
        satisfied_requirement
        open
        diffy

        Gopher Robot (Gerrit)

        unread,
        Jun 27, 2025, 7:34:53 PMJun 27
        to Joseph Tsai, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Dmitri Shuralyov, Dmitri Shuralyov, Go LUCI, Cherry Mui, Mateusz Poliwczak, Keith Randall, golang-co...@googlegroups.com

        Gopher Robot submitted the change with unreviewed changes

        Unreviewed changes

        2 is the latest approved patch-set.
        The change was submitted with unreviewed changes in the following files:

        ```
        The name of the file: src/reflect/value.go
        Insertions: 4, Deletions: 4.

        @@ -1515,7 +1515,7 @@


        typ := abi.TypeFor[T]()

        -	// Return the element inside the interface.
        + // If v is an interface, return the element inside the interface.
        //

        // T is a concrete type and v is an interface. For example:
         	//
        @@ -1523,7 +1523,7 @@
        // val := ValueOf(&v).Elem()

        // TypeAssert[int](val) == val.Interface().(int)
         	//
        - // T is a interface and v is an interface. For example:
        + // T is a interface and v is a non-nil interface value. For example:
        //

        // var v any = &someError{}
         	//	val := ValueOf(&v).Elem()
        @@ -1531,7 +1531,7 @@
        //

        // T is a interface and v is a nil interface value. For example:
         	//
        - // var v error
        + // var v error = nil
        // val := ValueOf(&v).Elem()

        // TypeAssert[error](val) == val.Interface().(error)
         	if v.kind() == Interface {
        @@ -1539,7 +1539,7 @@
        return v, ok

        }

        - // T is an interface, v is a concrete type. For example:
        +	// If T is an interface and v is a concrete type. For example:
        //

        // TypeAssert[any](ValueOf(1)) == ValueOf(1).Interface().(any)
         	//	TypeAssert[error](ValueOf(&someError{})) == ValueOf(&someError{}).Interface().(error)
        ```

        Change information

        Commit message:
        reflect: fix TypeAssert on nil interface values

        In the Go language a type assertion of a nil interface value
        will always report false:

        var err error
        v, ok := err.(error) // always reports (nil, false)

        Consequently, assertion on a reflect.Value.Interface()
        will also report false:

        var err error
        rv := ValueOf(&err).Elem()
        v, ok := rv.Interface().(error) // reports (nil, false)

        However, prior to this change, a TypeAssert would report true:

        var err error
        rv := ValueOf(&err).Elem()
        v, ok := TypeAssert[error](rv) // reports (nil, true)

        when it should report false.

        This fixes TypeAssert to match the Go language by
        pushing the typ != v.typ check to the very end after
        we have validated that neither v nor T are interface kinds.

        Fixes #74404
        Change-Id: Ie14d5cf18c8370c3e27ce4bdf4570c89519d8a16
        Reviewed-by: Cherry Mui <cher...@google.com>
        Auto-Submit: Joseph Tsai <joe...@digital-static.net>
        Reviewed-by: Mateusz Poliwczak <mpoliw...@gmail.com>
        Reviewed-by: Dmitri Shuralyov <dmit...@google.com>
        Files:
        • M src/reflect/all_test.go
        • M src/reflect/value.go
        Change size: M
        Delta: 2 files changed, 41 insertions(+), 36 deletions(-)
        Branch: refs/heads/master
        Submit Requirements:
        • requirement satisfiedCode-Review: +2 by Cherry Mui, +1 by Mateusz Poliwczak, +1 by Dmitri Shuralyov
        • requirement satisfiedTryBots-Pass: LUCI-TryBot-Result+1 by Go LUCI
        Open in Gerrit
        Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
        Gerrit-MessageType: merged
        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: Ie14d5cf18c8370c3e27ce4bdf4570c89519d8a16
        Gerrit-Change-Number: 684675
        Gerrit-PatchSet: 4
        Gerrit-Owner: Joseph Tsai <joe...@digital-static.net>
        Gerrit-Reviewer: Cherry Mui <cher...@google.com>
        Gerrit-Reviewer: Dmitri Shuralyov <dmit...@google.com>
        Gerrit-Reviewer: Gopher Robot <go...@golang.org>
        open
        diffy
        satisfied_requirement
        Reply all
        Reply to author
        Forward
        0 new messages