Skip to content

Commit

Permalink
fix: untyped values use calling builtin function's type (#1423)
Browse files Browse the repository at this point in the history
<!-- please provide a detailed description of the changes made in this
pull request. -->
Addresses #1085 

The call to `convConst` should include the type in this case. This
happens while processing a `CallExpr` and in this case the expression is
a function call to the built function `uint64`. We have the expected
type, so use when converting the constant to a typed value. If this type
is still nil, then it will behave as it does currently.

However, the concrete type should only be supplied to `convertConst` if
the call expression's type and the constant expression's type both have
the same underlying _unsigned type_ -- the base type of any value of
these types were they to be evaluated as literals before being assigned
a type.

<details><summary>Contributors' checklist...</summary>

- [x] Added new tests, or not needed, or not feasible
- [ ] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [ ] Updated the official documentation or not needed
- [ ] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [x] Added references to related issues and PRs
- [ ] Provided any useful hints for running manual tests
- [ ] Added new benchmarks to [generated
graphs](https://gnoland.github.io/benchmarks), if any. More info
[here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
</details>
  • Loading branch information
deelawn committed Jan 11, 2024
1 parent 2acf839 commit 68dd403
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
10 changes: 9 additions & 1 deletion gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,7 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node {
}
n.NumArgs = 1
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,
Expand All @@ -974,10 +975,17 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node {
arg0))
}
}

convertConst(store, last, arg0, ct)
constConverted = true
}

// (const) untyped decimal -> float64.
// (const) untyped bigint -> int.
convertConst(store, last, arg0, nil)
if !constConverted {
convertConst(store, last, arg0, nil)
}

// evaluate the new expression.
cx := evalConst(store, last, n)
// Though cx may be undefined if ct is interface,
Expand Down
20 changes: 20 additions & 0 deletions gnovm/tests/files/issue-1085.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

func main() {
a := uint64(9223372036854775808)
var b uint64 = uint64(9223372036854775808)

const c = uint64(9223372036854775808)
const d uint64 = uint64(9223372036854775808)

println(a)
println(b)
println(c)
println(d)
}

// Output:
// 9223372036854775808
// 9223372036854775808
// 9223372036854775808
// 9223372036854775808

0 comments on commit 68dd403

Please sign in to comment.