-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathchecktypeassertexpr.go
More file actions
34 lines (30 loc) · 869 Bytes
/
Copy pathchecktypeassertexpr.go
File metadata and controls
34 lines (30 loc) · 869 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package eval
import (
"reflect"
"go/ast"
)
func checkTypeAssertExpr(assert *ast.TypeAssertExpr, env Env) (*TypeAssertExpr, []error) {
aexpr := &TypeAssertExpr{TypeAssertExpr: assert}
x, errs := CheckExpr(assert.X, env)
aexpr.X = x
if errs != nil && !x.IsConst() {
return aexpr, errs
} else if xT, err := expectSingleType(x); err != nil {
errs = append(errs, err)
} else if xT == ConstNil {
errs = append(errs, ErrUntypedNil{x})
} else if xT.Kind() != reflect.Interface {
errs = append(errs, ErrInvalidTypeAssert{aexpr})
} else {
typ, t, _, moreErrs := checkType(assert.Type, env)
aexpr.Type = typ
errs = append(errs, moreErrs...)
if t != nil {
aexpr.knownType = knownType{t}
if t.Kind() != reflect.Interface && !unhackType(t).Implements(xT) {
errs = append(errs, ErrImpossibleTypeAssert{aexpr})
}
}
}
return aexpr, errs
}