ssa: fix type assertion of same type

This commit is contained in:
Li Jie
2025-02-14 23:54:12 +08:00
parent 4e34ce7470
commit c6462cbcc7

View File

@@ -17,6 +17,7 @@
package ssa package ssa
import ( import (
"go/constant"
"go/token" "go/token"
"go/types" "go/types"
"log" "log"
@@ -243,6 +244,10 @@ func (b Builder) TypeAssert(x Expr, assertedTyp Type, commaOk bool) Expr {
var eq Expr var eq Expr
var val func() Expr var val func() Expr
if x.RawType() == assertedTyp.RawType() {
eq = b.Const(constant.MakeBool(true), b.Prog.Bool())
val = func() Expr { return x }
} else {
if rawIntf, ok := assertedTyp.raw.Type.Underlying().(*types.Interface); ok { if rawIntf, ok := assertedTyp.raw.Type.Underlying().(*types.Interface); ok {
eq = b.InlineCall(b.Pkg.rtFunc("Implements"), tabi, tx) eq = b.InlineCall(b.Pkg.rtFunc("Implements"), tabi, tx)
val = func() Expr { return Expr{b.unsafeInterface(rawIntf, tx, b.faceData(x.impl)), assertedTyp} } val = func() Expr { return Expr{b.unsafeInterface(rawIntf, tx, b.faceData(x.impl)), assertedTyp} }
@@ -250,6 +255,7 @@ func (b Builder) TypeAssert(x Expr, assertedTyp Type, commaOk bool) Expr {
eq = b.BinOp(token.EQL, tx, tabi) eq = b.BinOp(token.EQL, tx, tabi)
val = func() Expr { return b.valFromData(assertedTyp, b.faceData(x.impl)) } val = func() Expr { return b.valFromData(assertedTyp, b.faceData(x.impl)) }
} }
}
if commaOk { if commaOk {
prog := b.Prog prog := b.Prog