Skip to content
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

fix: correct typed-nil handling #1891

Draft
wants to merge 1 commit into
base: fix/maxwell/type_comparison
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -1231,9 +1231,9 @@
panic("type conversion requires single argument")
}
n.NumArgs = 1
ct := evalStaticType(store, last, n.Func)
if arg0, ok := n.Args[0].(*ConstExpr); ok {
var constConverted bool
ct := evalStaticType(store, last, n.Func)
// As a special case, if a decimal cannot
// be represented as an integer, it cannot be converted to one,
// and the error is handled here.
Expand Down Expand Up @@ -1263,6 +1263,19 @@
convertConst(store, last, arg0, nil)
}

// check legal type for nil
if arg0.IsUndefined() {
switch ct.Kind() { // special case for nil conversion check.
case SliceKind, PointerKind, FuncKind, MapKind, InterfaceKind:
//dt = ct // convert nil to typed-nil
convertConst(store, last, arg0, ct)
default:
panic(fmt.Sprintf(
"cannot convert %v to %v",
arg0, ct.Kind()))

Check warning on line 1275 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L1272-L1275

Added lines #L1272 - L1275 were not covered by tests
}
}

// evaluate the new expression.
cx := evalConst(store, last, n)
// Though cx may be undefined if ct is interface,
Expand Down
5 changes: 5 additions & 0 deletions gnovm/pkg/gnolang/values_conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ func ConvertTo(alloc *Allocator, store Store, tv *TypedValue, t Type) {
GNO_CASE:
// special case for interface target
if t.Kind() == InterfaceKind {
if tv.IsUndefined() && tv.T == nil {
if _, ok := t.(*NativeType); !ok { // no support for native now
tv.T = t
}
}
return
}
// special case for undefined/nil source
Expand Down
18 changes: 18 additions & 0 deletions gnovm/tests/files/types/typed_nil_a.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import "fmt"

type integer int

func main() {
// illegal conversion
// should not work
if integer(nil) == nil {
fmt.Println("integer is nil")
} else {
fmt.Println("integer is not nil")
}
}

// Error:
// main/files/types/typed_nil_a.gno:10:5: cannot convert (undefined) to main.integer
16 changes: 16 additions & 0 deletions gnovm/tests/files/types/typed_nil_b.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import "fmt"

type integer *int

func main() {
println(integer(nil))
fmt.Println(integer(nil))
fmt.Printf("%T \n", integer(nil))
}

// Output:
// (nil main.integer)
// <nil>
// *int
12 changes: 12 additions & 0 deletions gnovm/tests/files/types/typed_nil_c.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import "fmt"

func main() {
println(interface{}(nil) == (*int)(nil))
fmt.Printf("%T \n", interface{}(nil))
}

// Output:
// false
// <nil>
Loading