-
Notifications
You must be signed in to change notification settings - Fork 373
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): sync code AssignStmt - ValueDecl #3017
base: master
Are you sure you want to change the base?
feat(gnovm): sync code AssignStmt - ValueDecl #3017
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #3017 +/- ##
==========================================
- Coverage 63.33% 63.32% -0.01%
==========================================
Files 548 548
Lines 78601 78559 -42
==========================================
- Hits 49780 49751 -29
+ Misses 25466 25454 -12
+ Partials 3355 3354 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
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.
Looks good to me 🔥
Co-authored-by: Mikael VALLENET <mikael.vallenetpro@gmail.com>
gnovm/pkg/gnolang/preprocess.go
Outdated
nx.Path = last.GetPathForName(nil, nx.Name) | ||
} | ||
} | ||
if len(n.Values) != 1 && len(n.Values) != 0 && len(n.NameExprs) != len(n.Values) { |
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.
why this len(n.Values) != 1 && len(n.Values) != 0
?
I think this will not handle this case:
package main
var a, b = 1
func main() {
}
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.
Originally, there were 3 cases:
if numNames > 1 && len(n.Values) == 1 {
...
} else if len(n.Values) != 0 && numNames != len(n.Values) {
...
} else {
...
}
To be able to refactor the code, I've moved this check first then 2 others. So I've added the condition but it looks strange like that, I've pushed the change.
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.
@omarsy I've checked, actually the errors for var a, b = 1 vs a, b := 1 are identical with Golang
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 don't know why but I cannot resolve the comment :/
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.
Overall, this refactoring is on the good track, and I agree with the general direction. Nice work.
In addition to the remarks in the code, could you please add a few more tests to cover some corner cases such as:
Mixing single and multiple return expression in the same assignment:
func f() (a, b int) {return 1, 2}
var x, y, z = 1, f()
Or covering slices, arrays, type conversions, etc.
Thanks
gnovm/pkg/gnolang/preprocess.go
Outdated
// - `a, b, c T := f()` | ||
// - `a, b := n.(T)` | ||
// - `a, b := n[i], where n is a map` | ||
func specialParseTypeVals( |
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 specialParseTypeVals( | |
func parseMultipleAssignFromOneExp( |
specialParseTypeVals
is unclear. Please use a descriptive function name as suggested.
Also, for the function comment, please follow Go conventions to describe the function:
// parseMultimpleAssignFromOneExp parses assignment to multiple variables from a single expression, etc...
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've updated the function name + comment
gnovm/pkg/gnolang/preprocess.go
Outdated
// - `var a, b = n.(T)` | ||
// - `var a, b = n[i], where n is a map` | ||
// Assign | ||
// - `a, b, c T := f()` |
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, b, c T := f()` | |
// - `a, b, c := f()` |
Type name makes no sense in short declarations.
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've updated :)
// var a, b, c T = f() | ||
// a, b, c := f() | ||
valueType := evalStaticTypeOfRaw(store, bn, valueExpr) | ||
tuple = valueType.(*tupleType) |
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 not you check this type assertion? Are you sure that valueType
is always a tuple
?
Also keep in mind that CallExpr
may also refer to a type conversion, not only a function call: var a = int8(b)
, and that should be covered (maybe not here).
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.
According to the doc of evalStaticTypeOfRaw: like evalStaticTypeOf() but returns the raw *tupleType for *CallExpr.
, so I think we can expect a raw tuple here.
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.
The case var a = int8(b)
is not handled here because we process only multi assignments from 1 expression
tuple = &tupleType{Elts: []Type{tt, BoolType}} | ||
expr.HasOK = true | ||
case *IndexExpr: | ||
// Map index case: |
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.
IndexExpr may refer either to a slice, an array or a map. Please make sure you handle also slice and array types here. I understand it was not covered prior to your change (so it's not your fault :), but I see no reason not to do the job here also for slices/arrays.
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.
The scope of this function handles only multi assignments from 1 expression, so arrays/slides index is not consider as this case, there is already a check to ensure that we process only map
mt, ok := baseOf(dt).(*MapType)
if !ok {
panic(fmt.Sprintf("invalid index expression on %T", dt))
}
gnovm/pkg/gnolang/preprocess.go
Outdated
|
||
for i := 0; i < numNames; i++ { | ||
if st != nil { | ||
// TODO check tt and nt compat. |
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, perform the required type check, as it is important to catch types mismatch as early as possible.
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've added the compat check :)
gnovm/pkg/gnolang/preprocess.go
Outdated
|
||
// This func aims at syncing logic between op define (:=) and declare(var/const) | ||
// for general cases (not handled by specialParseTypeVals) | ||
func generalParseTypeVals( |
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 generalParseTypeVals( | |
func parseAssignFromExprList( |
generalParseTypeVals
is unclear. Please name function with more descriptive names. Also please fix function comment to better follow Go conventions.
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've updated the name + comment :)
gnovm/pkg/gnolang/preprocess.go
Outdated
) { | ||
numNames := len(nameExprs) | ||
|
||
// ensure that function only return 1 value |
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.
// ensure that function only return 1 value | |
// Ensure that function only return 1 value. |
Comment lines should follow normal sentence rules (1st letter capital, punctuation, ...).
gnovm/pkg/gnolang/preprocess.go
Outdated
} | ||
} | ||
|
||
// evaluate types and convert consts. |
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.
// evaluate types and convert consts. | |
// Evaluate types and convert consts. |
gnovm/pkg/gnolang/preprocess.go
Outdated
for i := 0; i < numNames; i++ { | ||
sts[i] = nt | ||
} | ||
// convert if const to nt. |
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.
// convert if const to nt. | |
// Convert if const to nt. |
gnovm/pkg/gnolang/preprocess.go
Outdated
checkOrConvertType(store, bn, &valueExprs[i], nt, false) | ||
} | ||
} else if isConst { | ||
// derive static type from values. |
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.
// derive static type from values. | |
// Derive static type from values. |
thanks @mvertes for your time and your reviews , I've addressed all your feedbacks. Please re-check the PR, please. Thanksssss. |
This PR aims at fixing this issue 1958
Contributors' checklist...
BREAKING CHANGE: xxx
message was included in the description