-
Notifications
You must be signed in to change notification settings - Fork 443
chore(gnovm): optimize err msg #4936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -207,9 +207,12 @@ func checkSame(at, bt Type, msg string) error { | |
| return nil | ||
| } | ||
|
|
||
| func assertAssignableTo(n Node, xt, dt Type) { | ||
| func mustAssignableTo(n Node, xt, dt Type) { | ||
| err := checkAssignableTo(n, xt, dt) | ||
| if err != nil { | ||
| if debug { | ||
| debug.Printf("checkAssignableTo fail: %v\n", err) | ||
| } | ||
| panic(err.Error()) | ||
| } | ||
| } | ||
|
|
@@ -734,7 +737,13 @@ func (x *BinaryExpr) AssertCompatible(lt, rt Type) { | |
| case EQL, NEQ: | ||
| assertComparable(xt, dt) | ||
| if !isUntyped(xt) && !isUntyped(dt) { | ||
| assertAssignableTo(x, xt, dt) | ||
| err := checkAssignableTo(x, xt, dt) | ||
| if err != nil { | ||
| if debug { | ||
| debug.Printf("checkAssignableTo fail: %v\n", err) | ||
| } | ||
| panic(fmt.Sprintf("invalid operation: %v (mismatched types %v and %v)", x, xt, dt)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. security-wise, we should maybe avoid panicking with the whole expression, as it might be very deep. what about making the error "types %v and %v are not comparable"? |
||
| } | ||
|
Comment on lines
+740
to
+746
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. log low-level error and return user friendly info, which is more in line with Go. |
||
| } | ||
| case LSS, LEQ, GTR, GEQ: | ||
| if checker, ok := binaryChecker[x.Op]; ok { | ||
|
|
@@ -793,6 +802,9 @@ func (x *BinaryExpr) checkCompatibility(n Node, xt, dt Type, checker func(t Type | |
| if !isUntyped(xt) && !isUntyped(dt) { | ||
| err := checkAssignableTo(n, xt, dt) | ||
| if err != nil { | ||
| if debug { | ||
| debug.Printf("checkAssignableTo fail: %v\n", err) | ||
| } | ||
| if swapped { | ||
| panic(fmt.Sprintf("invalid operation: %v (mismatched types %v and %v)", n, dt, untypedNil(xt))) | ||
| } else { | ||
|
|
@@ -850,19 +862,19 @@ func (x *RangeStmt) AssertCompatible(store Store, last BlockNode) { | |
| xt := evalStaticTypeOf(store, last, x.X) | ||
| switch cxt := xt.(type) { | ||
| case *MapType: | ||
| assertAssignableTo(x, cxt.Key, kt) | ||
| mustAssignableTo(x, cxt.Key, kt) | ||
| if vt != nil { | ||
| assertAssignableTo(x, cxt.Value, vt) | ||
| mustAssignableTo(x, cxt.Value, vt) | ||
| } | ||
| case *SliceType: | ||
| assertIndexTypeIsInt(kt) | ||
| if vt != nil { | ||
| assertAssignableTo(x, cxt.Elt, vt) | ||
| mustAssignableTo(x, cxt.Elt, vt) | ||
| } | ||
| case *ArrayType: | ||
| assertIndexTypeIsInt(kt) | ||
| if vt != nil { | ||
| assertAssignableTo(x, cxt.Elt, vt) | ||
| mustAssignableTo(x, cxt.Elt, vt) | ||
| } | ||
| case PrimitiveType: | ||
| if cxt.Kind() == StringKind { | ||
|
|
@@ -902,7 +914,7 @@ func (x *AssignStmt) AssertCompatible(store Store, last BlockNode) { | |
| assertValidAssignLhs(store, last, lx) | ||
| if !isBlankIdentifier(lx) { | ||
| lxt := evalStaticTypeOf(store, last, lx) | ||
| assertAssignableTo(x, cft.Results[i].Type, lxt) | ||
| mustAssignableTo(x, cft.Results[i].Type, lxt) | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -917,7 +929,7 @@ func (x *AssignStmt) AssertCompatible(store Store, last BlockNode) { | |
| if !isBlankIdentifier(x.Lhs[0]) { // see composite3.gno | ||
| dt := evalStaticTypeOf(store, last, x.Lhs[0]) | ||
| ift := evalStaticTypeOf(store, last, cx) | ||
| assertAssignableTo(x, ift, dt) | ||
| mustAssignableTo(x, ift, dt) | ||
| } | ||
| // check second value | ||
| assertValidAssignLhs(store, last, x.Lhs[1]) | ||
|
|
@@ -940,12 +952,12 @@ func (x *AssignStmt) AssertCompatible(store Store, last BlockNode) { | |
| if _, ok := cx.X.(*NameExpr); ok { | ||
| rt := evalStaticTypeOf(store, last, cx.X) | ||
| if mt, ok := rt.(*MapType); ok { | ||
| assertAssignableTo(x, mt.Value, lt) | ||
| mustAssignableTo(x, mt.Value, lt) | ||
| } | ||
| } else if _, ok := cx.X.(*CompositeLitExpr); ok { | ||
| cpt := evalStaticTypeOf(store, last, cx.X) | ||
| if mt, ok := cpt.(*MapType); ok { | ||
| assertAssignableTo(x, mt.Value, lt) | ||
| mustAssignableTo(x, mt.Value, lt) | ||
| } else { | ||
| panic("should not happen") | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package main | ||
|
|
||
| type A interface { | ||
| F() | ||
| } | ||
|
|
||
| type B interface { | ||
| X() | ||
| G() | ||
| } | ||
|
|
||
| type T struct{} | ||
|
|
||
| func (T) F() {} | ||
|
|
||
| func (T) G() {} | ||
|
|
||
| func (T) X() {} | ||
|
|
||
| func main() { | ||
| var a A = T{} | ||
| var b B = T{} | ||
|
|
||
| println(b == a) | ||
| println(a == b) | ||
| } | ||
|
|
||
| // Error: | ||
| // main/cmp_iface_9.gno:24:10-16: invalid operation: b<VPBlock(1,1)> == a<VPBlock(1,0)> (mismatched types main.B and main.A) | ||
|
|
||
| // TypeCheckError: | ||
| // main/cmp_iface_9.gno:24:15: invalid operation: b == a (mismatched types B and A); main/cmp_iface_9.gno:25:15: invalid operation: a == b (mismatched types A and B) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: but why must overt assert
IMO, i use must when i expect something in return like: "mustGetEntity" and assert when i don't expect a return but it's how i see it, just is there a reason behind this change ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for me,
mustis more of a convention from Go stdlibs, e.g.while
assertis always used in unit test, e.g.testify/assert.