Skip to content

Commit

Permalink
Ignore private methods for binary types during type assertion
Browse files Browse the repository at this point in the history
Fixes #1373
  • Loading branch information
cclerget committed Apr 7, 2022
1 parent f07f25f commit 1cf9d34
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
17 changes: 17 additions & 0 deletions _test/issue-1373.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"fmt"
"go/ast"
)

func NewBadExpr() ast.Expr {
return &ast.BadExpr{}
}

func main() {
fmt.Printf("%T\n", NewBadExpr().(*ast.BadExpr))
}

// Output:
// *ast.BadExpr
7 changes: 7 additions & 0 deletions interp/typecheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package interp
import (
"errors"
"go/constant"
"go/token"
"math"
"reflect"
)
Expand Down Expand Up @@ -591,6 +592,12 @@ func (check typecheck) typeAssertionExpr(n *node, typ *itype) error {
continue
}
if tm == nil {
// Lookup for non-exported methods is impossible
// for bin types, ignore them as they can't be used
// directly by the interpreted programs.
if !token.IsExported(name) && isBin(typ) {
continue
}
return n.cfgErrorf("impossible type assertion: %s does not implement %s (missing %v method)", typ.id(), n.typ.id(), name)
}
if tm.recv != nil && tm.recv.TypeOf().Kind() == reflect.Ptr && typ.TypeOf().Kind() != reflect.Ptr {
Expand Down

0 comments on commit 1cf9d34

Please sign in to comment.