-
Notifications
You must be signed in to change notification settings - Fork 372
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
Additional details and impacted files@@ Coverage Diff @@
## master #2828 +/- ##
==========================================
+ Coverage 63.37% 63.40% +0.03%
==========================================
Files 561 565 +4
Lines 79199 80149 +950
==========================================
+ Hits 50189 50821 +632
- Misses 25625 25912 +287
- Partials 3385 3416 +31
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
c9b02d2
to
f49c088
Compare
355d799
to
e9a9be7
Compare
@omarsy , can you fix the lint-pr-title CI check? |
consider this one, it should be handled: package main
var x = 1
const ff = +(1 << x)
func main() {
println("ok")
} and this one: package main
const a = func() { println("hey") }
func main() {
println("ok")
} this triggers an error message like: |
for _, arg := range vx.Args { | ||
checkConstantExpr(store, last, arg) | ||
} | ||
case *NativeType: |
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.
a better msg? a test?
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.
Removed the |
} | ||
case *BinaryExpr: | ||
checkConstantExpr(store, last, vx.Left) | ||
checkConstantExpr(store, last, vx.Right) |
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 one as a special case:
package main
var x = 1
var y = 1
const b = x == y
func main() {
println("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.
this one return an error in my PR, I think. Do you want I added as a test ?
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.
please see this: https://go.dev/play/p/dhxEqNA7p60
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 think that when you run it, you'll encounter an error like x (variable of type int) is not constant
. While it's not the same error, I think the description of the error in my PR better clarifies the issue we have.
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.
IMO, it would be more natural that the RHS is not const, rather than its child node.
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 find the current error message a bit unclear. For example, consider the following code:
package main
const x = 1
var y = 1
const b = x == y
func main() {
println("ok")
}
In Go, this will produce the error: x == y (untyped bool value) is not constant
. However, in the PR, the error we get is: y (variable of type int) is not constant
.
This difference suggests that we should change our code to make it work, like so:
package main
const x = 1
const y = 1
const b = x == y
func main() {
println("ok")
}
The Go error message is more informative, indicating that the expression itself is not considered constant rather than simply pointing out that y
is a variable.
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.
But I can the logic of the code if you think it is a blocker (to be more go like)
// break Main | ||
// } | ||
// panic(fmt.Sprintf("%s (variable of type %s) is not constant", vx.String(), xt)) | ||
case *PointerType, *DeclaredType, *StructType, *InterfaceType: |
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.
more tests on this?
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.
right, done here: e74e6e9
Partial, will have a final one after. |
nice catch @ltzmaxwell ^^ |
955eb18
to
90bd7a8
Compare
case *FuncType: | ||
tup := evalStaticTypeOfRaw(store, last, vx).(*tupleType) | ||
|
||
// check for built-in functions |
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.
If real
and imag
are added later, it would need to be handled here as well.
Can you add a comment for this?
Related Issues:
#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