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: default untyped to uint when necessary #2024

Merged
merged 6 commits into from
Jun 14, 2024
Merged
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
2 changes: 1 addition & 1 deletion gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -2477,7 +2477,7 @@
// convert type
if isUntyped(xt) { // convert if x is untyped literal
if t == nil {
t = defaultTypeOf(xt)
t = defaultTypeOf(xt, nil)

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

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2480

Added line #L2480 was not covered by tests
}
// Push type into expr if qualifying binary expr.
if bx, ok := (*x).(*BinaryExpr); ok {
Expand Down
19 changes: 14 additions & 5 deletions gnovm/pkg/gnolang/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1203,8 +1203,8 @@
continue
} else if vargt == nil {
vargt = varg.T
} else if isUntyped(varg.T) && vargt.TypeID() == defaultTypeOf(varg.T).TypeID() {
vargt = defaultTypeOf(varg.T)
} else if isUntyped(varg.T) && vargt.TypeID() == defaultTypeOf(varg.T, varg.V).TypeID() {
vargt = defaultTypeOf(varg.T, varg.V)

Check warning on line 1207 in gnovm/pkg/gnolang/types.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/types.go#L1207

Added line #L1207 was not covered by tests
} else if vargt.TypeID() != varg.T.TypeID() {
panic(fmt.Sprintf(
"incompatible varg types: expected %v, got %s",
Expand Down Expand Up @@ -2281,14 +2281,23 @@

// TODO move untyped const stuff to preprocess.go.
// TODO associate with ConvertTo() in documentation.
func defaultTypeOf(t Type) Type {
func defaultTypeOf(t Type, v Value) Type {
switch t {
case UntypedBoolType:
return BoolType
case UntypedRuneType:
return Int32Type
case UntypedBigintType:
return IntType
typeVal := IntType
if bigintValue, ok := v.(BigintValue); ok {
if bigintValue.V != nil && bigintValue.V.Sign() == 1 && !bigintValue.V.IsInt64() {
// Use an unsigned type if the value is positive and we know
// it won't fit in an int64.
typeVal = Uint64Type

Check warning on line 2296 in gnovm/pkg/gnolang/types.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/types.go#L2296

Added line #L2296 was not covered by tests
}
}

return typeVal
case UntypedBigdecType:
return Float64Type
case UntypedStringType:
Expand Down Expand Up @@ -2514,7 +2523,7 @@
return // ok
} else {
if isUntyped(spec) {
ltzmaxwell marked this conversation as resolved.
Show resolved Hide resolved
spec = defaultTypeOf(spec)
spec = defaultTypeOf(spec, nil)

Check warning on line 2526 in gnovm/pkg/gnolang/types.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/types.go#L2526

Added line #L2526 was not covered by tests
}
lookup[ct.Generic] = spec
return // ok
Expand Down
2 changes: 1 addition & 1 deletion gnovm/pkg/gnolang/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -1594,7 +1594,7 @@ func (tv *TypedValue) Assign(alloc *Allocator, tv2 TypedValue, cu bool) {
}
*tv = tv2.Copy(alloc)
if cu && isUntyped(tv.T) {
ConvertUntypedTo(tv, defaultTypeOf(tv.T))
ConvertUntypedTo(tv, defaultTypeOf(tv.T, tv.V))
}
}

Expand Down
2 changes: 1 addition & 1 deletion gnovm/pkg/gnolang/values_conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ func ConvertUntypedTo(tv *TypedValue, t Type) {
}
// general case
if t == nil {
t = defaultTypeOf(tv.T)
t = defaultTypeOf(tv.T, tv.V)
}
switch tv.T {
case UntypedBoolType:
Expand Down
14 changes: 14 additions & 0 deletions gnovm/tests/files/bigint1.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"math"
)

func main() {
println(float64(math.MaxUint32))
println(float64(math.MaxUint64))
}

// Output:
// 4.294967295e+09
// 1.8446744073709552e+19
Loading