Skip to content

Commit

Permalink
handle undefined/nil type conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
notJoon committed Nov 9, 2023
1 parent 789f4de commit 73b28ae
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 2 deletions.
96 changes: 96 additions & 0 deletions gnovm/pkg/gnolang/gno_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,102 @@ func main() {
}`, `aaa,bbb`)
}

func TestVarDeclTypeConversion(t *testing.T) {
m := NewMachine("test", nil)
c := `package test
func main() {
var x float64 = 10.5
println(int(x))
}`
n := MustParseFile("main.go", c)
m.RunFiles(n)
m.RunMain()
}

func TestShortenVarDeclTypeConversion(t *testing.T) {
m := NewMachine("test", nil)
c := `package test
func main() {
x := 10.5 // default type is float64
println(int(x))
}`
n := MustParseFile("main.go", c)
m.RunFiles(n)
m.RunMain()
}


Check failure on line 134 in gnovm/pkg/gnolang/gno_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` (gofmt)

Check failure on line 134 in gnovm/pkg/gnolang/gno_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)

Check failure on line 134 in gnovm/pkg/gnolang/gno_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)
func TestInvalidTypeConversion(t *testing.T) {
m := NewMachine("test", nil)

Check failure on line 136 in gnovm/pkg/gnolang/gno_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
testCases := []struct {
name string
code string
}{
{
name: "UndefinedIntType",
code: `package test
func main(){
println(int(10.5))
}`,
},
{

Check failure on line 148 in gnovm/pkg/gnolang/gno_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
name: "NilToInt",
code: `package test
func main(){
println(int(nil))
}`,
},

Check failure on line 154 in gnovm/pkg/gnolang/gno_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
{
name: "NilToFloat",
code: `package test
func main(){
println(float64(nil))
}`,
},
{

Check failure on line 162 in gnovm/pkg/gnolang/gno_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
name: "NilToBool",
code: `package test
func main(){
println(bool(nil))
}`,
},

Check failure on line 168 in gnovm/pkg/gnolang/gno_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
{
name: "BoolToInt",
code: `package test
func main(){
println(int(true))
}`,
},
{
name: "IntToBool",
code: `package test
func main(){
x := 1
println(bool(x))
}`,
},
}

Check failure on line 184 in gnovm/pkg/gnolang/gno_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
n := MustParseFile("main.go", tc.code)
defer func() {
if r := recover(); r != nil {
t.Logf("Recovered from panic as expected: %v", r)
} else {
t.Errorf("Expected a panic for invalid type conversion")
}
}()
m.RunFiles(n)
m.RunMain()

if err := m.CheckEmpty(); err != nil {
t.Fatal(err)
}
})

Check failure on line 202 in gnovm/pkg/gnolang/gno_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
}
}

// ----------------------------------------
// Benchmarks

Expand Down
6 changes: 4 additions & 2 deletions gnovm/pkg/gnolang/values_conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ GNO_CASE:
}
// special case for undefined/nil source
if tv.IsUndefined() {
tv.T = t
return
panic(fmt.Sprintf(
"cannot convert %s (untyped %s constant) to type %s",
tv.String(), tv.T.String(), t.String()),
)
}
// general case
tvk := tv.T.Kind()
Expand Down

0 comments on commit 73b28ae

Please sign in to comment.