-
Notifications
You must be signed in to change notification settings - Fork 383
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
feat(gnovm): align Gno constant handling with Go specifications #2828
base: master
Are you sure you want to change the base?
Conversation
Codecov ReportAttention: Patch coverage is
📢 Thoughts on this report? Let us know! |
c9b02d2
to
f49c088
Compare
355d799
to
e9a9be7
Compare
@omarsy , can you fix the lint-pr-title CI check? |
const t = 1 + 2 + len([]string{}) | ||
fmt.Println(t) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I test this one, seems not caught by your new added logic
package main
import "fmt"
func main() {
const t = 1 + 2 + len([2]string{})
fmt.Println(t)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will work because an array is permitted in a constant expression, whereas a slice is not.
gnovm/pkg/gnolang/type_check.go
Outdated
switch vx := vx.(type) { | ||
case *NameExpr: | ||
t := evalStaticTypeOf(store, last, vx) | ||
if _, ok := t.(*ArrayType); ok { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doubt about this, only with len(arr)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes array is only variable allowed in a constant.
this should work on go:
package main
func main() {
var l = [2]string{}
const i = len(l)
println(i)
}
this should not work:
package main
func main() {
var l = "test"
const i = len(l)
println(i)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider this:
package main
func main() {
const a = [2]int{1, 2}
println(a)
}
and
package main
func main() {
a := [2]int{1, 2}
const b = a
}
these should not work, but works on the current branch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice catch I did a bad merge ^^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmmm, this logic seems incorrect:
if _, ok := t.(*ArrayType); ok {
break Main
}
Due to this logic, the error in const43.gno
was not caught.
I guess your intention is to check len(arr), right? I think that needs additional context to know if the outer expr is a built func call: len
or max
?
Not so sure, but we need to focus on ensuring that the logic of this function is correct(not limited to the above-mentioned).
🛠 PR Checks Summary🔴 Maintainers must be able to edit this pull request (more info) Manual Checks (for Reviewers):
Read More🤖 This bot helps streamline PR reviews by verifying automated checks and providing guidance for contributors and reviewers. ✅ Automated Checks (for Contributors):🔴 Maintainers must be able to edit this pull request (more info) ☑️ Contributor Actions:
☑️ Reviewer Actions:
📚 Resources:Debug
|
I think it is better to handle it in another PR. WDYT ? |
gnovm/pkg/gnolang/type_check.go
Outdated
switch vx := vx.(type) { | ||
case *NameExpr: | ||
t := evalStaticTypeOf(store, last, vx) | ||
if _, ok := t.(*ArrayType); ok { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmmm, this logic seems incorrect:
if _, ok := t.(*ArrayType); ok {
break Main
}
Due to this logic, the error in const43.gno
was not caught.
I guess your intention is to check len(arr), right? I think that needs additional context to know if the outer expr is a built func call: len
or max
?
Not so sure, but we need to focus on ensuring that the logic of this function is correct(not limited to the above-mentioned).
gnovm/pkg/gnolang/type_check.go
Outdated
assertValidConstantExprRecursively(store, last, expr, nil) | ||
} | ||
|
||
func assertValidConstantExprRecursively(store Store, last BlockNode, currExpr, parentExpr Expr) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
func assertValidConstantExprRecursively(store Store, last BlockNode, currExpr, parentExpr Expr) { | |
func assertValidConstValue(store Store, last BlockNode, currExpr, parentExpr Expr) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be done here 54878f0
Related Issues:
Closes #2628
This PR aims to align Gno's constant handling with the Go specification regarding constant expressions (see Go Specification on Constants).
Primitive Type Requirement
Function Calls Disallowed
Only built-in functions should be allowed.
Constant Operands Requirement
Constant expressions may contain only constant operands and are evaluated at compile time.
Type Assertion Forbidden
Index Expression Forbidden
Contributors' checklist...
BREAKING CHANGE: xxx
message was included in the description