Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
decrypt error msg
Browse files Browse the repository at this point in the history
notJoon committed Aug 14, 2024

Verified

This commit was signed with the committer’s verified signature.
1 parent 0e3c050 commit cf9a3fd
Showing 3 changed files with 85 additions and 9 deletions.
2 changes: 1 addition & 1 deletion gnovm/pkg/gnolang/nodes.go
Original file line number Diff line number Diff line change
@@ -403,7 +403,7 @@ type IndexExpr struct { // X[Index]
Attributes
X Expr // expression
Index Expr // index expression
HasOK bool // if true, is form: `value, ok := <X>[<Key>]
HasOK bool // if true, is form: `value, ok := <X>[<Key>]`
}

type SelectorExpr struct { // X.Sel
33 changes: 25 additions & 8 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
@@ -2449,16 +2449,33 @@ func evalStaticTypeOf(store Store, last BlockNode, x Expr) Type {
t := evalStaticTypeOfRaw(store, last, x)
if tt, ok := t.(*tupleType); ok {
if len(tt.Elts) != 1 {
panic(fmt.Sprintf(
"evalStaticTypeOf() only supports *CallExpr with 1 result, got %s",
tt.String(),
))
} else {
return tt.Elts[0]
loc := last.GetLocation()
funcName := getFunctionName(x)
valueCount := len(tt.Elts)

var msg string
if valueCount == 0 {
msg = fmt.Sprintf("%s: %s() (no value) used as value", loc, funcName)
} else {
msg = fmt.Sprintf("%s: %s() (%d values) used as single value", loc, funcName, valueCount)
}

panic(fmt.Errorf("%s\nHint: Ensure the function returns a single value, or use multiple assignment", msg))
}
return tt.Elts[0]
}
return t
}

// getFunctionName attempts to extract the function name from the expression
func getFunctionName(x Expr) string {
switch expr := x.(type) {
case *CallExpr:
if id, ok := expr.Func.(*NameExpr); ok {
return string(id.Name)
}
} else {
return t
}
return "<unknown function>"
}

// like evalStaticTypeOf() but returns the raw *tupleType for *CallExpr.
59 changes: 59 additions & 0 deletions gnovm/pkg/gnolang/preprocess_test.go
Original file line number Diff line number Diff line change
@@ -60,3 +60,62 @@ func main() {
initStaticBlocks(store, pn, n)
Preprocess(store, pn, n)
}

func TestEvalStaticTypeOf_MultipleValues(t *testing.T) {
pn := NewPackageNode("main", "main", nil)
pv := pn.NewPackage()
store := gonativeTestStore(pn, pv)
store.SetBlockNode(pn)

const src = `package main
func multipleReturns() (int, string) {
return 1, "hello"
}
func main() {
x := multipleReturns()
}`
n := MustParseFile("main.go", src)

initStaticBlocks(store, pn, n)

defer func() {
err := recover()
assert.NotNil(t, err, "Expected panic")
errMsg := fmt.Sprint(err)
assert.Contains(t, errMsg, "multipleReturns() (2 values) used as single value", "Unexpected error message")
assert.Contains(t, errMsg, "Hint: Ensure the function returns a single value, or use multiple assignment", "Missing hint in error message")
}()

Preprocess(store, pn, n)
}

func TestEvalStaticTypeOf_NoValue(t *testing.T) {
pn := NewPackageNode("main", "main", nil)
pv := pn.NewPackage()
store := gonativeTestStore(pn, pv)
store.SetBlockNode(pn)

const src = `package main
func main() {
n := f()
}
func f() {
println("hello!")
}
`
n := MustParseFile("main.go", src)

initStaticBlocks(store, pn, n)

defer func() {
err := recover()
assert.NotNil(t, err, "Expected panic")
errMsg := fmt.Sprint(err)
assert.Contains(t, errMsg, "f() (no value) used as value", "Unexpected error message")
assert.Contains(t, errMsg, "Hint: Ensure the function returns a single value, or use multiple assignment", "Missing hint in error message")
}()

Preprocess(store, pn, n)
}

0 comments on commit cf9a3fd

Please sign in to comment.