Skip to content

Commit

Permalink
fix: interface to concrete type by T(ifaceVal) is not allowed
Browse files Browse the repository at this point in the history
  • Loading branch information
petar-dambovaliev committed Feb 14, 2025
1 parent 9884ba1 commit e195990
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 0 deletions.
164 changes: 164 additions & 0 deletions gnovm/pkg/gnolang/gno_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,170 @@ func TestConvertTo(t *testing.T) {
tests := []cases{
{
`package test
func main() {
var t interface{}
t = 2
var g = float32(t)
println(g)
}
`, `test/main.go:5:10: cannot convert interface{} to float32: need type assertion`,
},
{
`package test
func main() {
var t interface{}
t = 2
var g = int(t)
println(g)
}
`, `test/main.go:5:13: cannot convert interface{} to int: need type assertion`,
},
{
`package test
func main() {
var t interface{}
t = 2
var g = int8(t)
println(g)
}
`, `test/main.go:5:13: cannot convert interface{} to int8: need type assertion`,
},
{
`package test
func main() {
var t interface{}
t = 2
var g = int16(t)
println(g)
}
`, `test/main.go:5:13: cannot convert interface{} to int16: need type assertion`,
},
{
`package test
func main() {
var t interface{}
t = 2
var g = int32(t)
println(g)
}
`, `test/main.go:5:14: cannot convert interface{} to int32: need type assertion`,
},
{
`package test
func main() {
var t interface{}
t = 2
var g = int64(t)
println(g)
}
`, `test/main.go:5:13: cannot convert interface{} to int64: need type assertion`,
},
{
`package test
func main() {
var t interface{}
t = 2
var g = uint(t)
println(g)
}
`, `test/main.go:5:13: cannot convert interface{} to uint: need type assertion`,
},
{
`package test
func main() {
var t interface{}
t = 2
var g = uint8(t)
println(g)
}
`, `test/main.go:5:13: cannot convert interface{} to uint8: need type assertion`,
},
{
`package test
func main() {
var t interface{}
t = 2
var g = uint16(t)
println(g)
}
`, `test/main.go:5:13: cannot convert interface{} to uint16: need type assertion`,
},
{
`package test
func main() {
var t interface{}
t = 2
var g = uint32(t)
println(g)
}
`, `test/main.go:5:13: cannot convert interface{} to uint32: need type assertion`,
},
{
`package test
func main() {
var t interface{}
t = 2
var g = uint64(t)
println(g)
}
`, `test/main.go:5:13: cannot convert interface{} to uint64: need type assertion`,
},

// Built-in non-numeric types
{
`package test
func main() {
var t interface{}
t = "hello"
var g = string(t)
println(g)
}
`, `test/main.go:5:13: cannot convert interface{} to string: need type assertion`,
},
{
`package test
func main() {
var t interface{}
t = true
var g = bool(t)
println(g)
}
`, `test/main.go:5:13: cannot convert interface{} to bool: need type assertion`,
},
{
`package test
func main() {
var t interface{}
t = 'a'
var g = rune(t)
println(g)
}
`, `test/main.go:5:13: cannot convert interface{} to int32: need type assertion`,
},
{
`package test
func main() {
var t interface{}
t = byte(65)
var g = byte(t)
println(g)
}
`, `test/main.go:5:13: cannot convert interface{} to uint8: need type assertion`,
},

{
`package test
type MyInt int
func main() {
var t interface{}
t = MyInt(2)
var g = MyInt(t)
println(g)
}
`, `test/main.go:6:13: cannot convert interface{} to test.MyInt: need type assertion`,
},
{
`package test
func main() {
const a int = -1
Expand Down
8 changes: 8 additions & 0 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,14 @@ func preprocess1(store Store, ctx BlockNode, n Node) Node {
n.NumArgs = 1
ct := evalStaticType(store, last, n.Func)
at := evalStaticTypeOf(store, last, n.Args[0])

_, isCTInterface := ct.(*InterfaceType)
_, isATInterface := at.(*InterfaceType)

if !isCTInterface && isATInterface {
panic(fmt.Sprintf("cannot convert %v to %v: need type assertion", at.TypeID(), ct.TypeID()))
}

var constConverted bool
switch arg0 := n.Args[0].(type) {
case *ConstExpr:
Expand Down

0 comments on commit e195990

Please sign in to comment.