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

vm: variable declaration code in AssignStmt and ValueDecl should be synced (issues with map index / type assertion tuple assignment) #1958

Closed
thehowl opened this issue Apr 19, 2024 · 2 comments
Assignees
Labels
📦 🤖 gnovm Issues or PRs gnovm related 🌟 improvement performance improvements, refactors ...

Comments

@thehowl
Copy link
Member

thehowl commented Apr 19, 2024

The Go spec states:

A variable declaration creates one or more variables, binds corresponding identifiers to them, and gives each a type and an initial value. [...]

A short variable declaration [...] is shorthand for a regular variable declaration with initializer expressions but no types:

"var" IdentifierList "=" ExpressionList .

There is one notable exception, which is that the shorthand := allows for "redeclarations" on the LHS, so long as at least one name isn't being redeclared. The other difference is that the var syntax can be used to declare global variables, while := can't.

Currently, the GnoVM has two very different handling of these two statements, which according to the go spec should work the same (at least for their use in function bodies):

if n.Op == DEFINE {

case *ValueDecl:

The changes don't concern only this TRANS_LEAVE block, though it is one where the difference is most significant.

The practical consequence is that there are key differences in behaviour when using the var syntax or the := syntax; for instance, "tuple" map indexes and type assertions are not supported in the var syntax:

package hello

func main() {
  m := map[string]string{"hey": "1"}
  // a, ok := m["hey"]
  var a, ok = m["hey"]
  println(a, ok)
}

https://play.gno.land/p/wvIG65jLGmT

package hello

func main() {
  v := (interface{})(11)
  // i, ok := v.(int)
  var i, ok = v.(int)
  println(i, ok)
}

https://play.gno.land/p/FMC5fHKjlgg

You can easily note in the playground how, switching the statements to the corresponding := statements, the statement will work again.

To avoid further bugs, we should try to unify as much as possible the code for preprocessing both expressions, so that there is no surprising behaviour at runtime for two variables declared with the different syntaxes

@hthieu1110
Copy link
Contributor

hthieu1110 commented Oct 23, 2024

Just a first comment: seems that "tuple" map indexes and type assertions are now supported in the var syntax, I don't have any error when executing the code mentioned above.

I'm working on refactoring code.

ltzmaxwell pushed a commit that referenced this issue Nov 21, 2024
This PR aims at fixing this issue
[1958](#1958)

<!-- please provide a detailed description of the changes made in this
pull request. -->

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

- [ ] 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
- [ ] Added references to related issues and PRs
- [ ] Provided any useful hints for running manual tests
</details>

---------

Co-authored-by: hieu.ha <[email protected]>
Co-authored-by: Mikael VALLENET <[email protected]>
@mvertes
Copy link
Contributor

mvertes commented Nov 25, 2024

Fixed by #3017

@mvertes mvertes closed this as completed Nov 25, 2024
@github-project-automation github-project-automation bot moved this from In Progress to Done in 🧙‍♂️gno.land core team Nov 25, 2024
r3v4s pushed a commit to gnoswap-labs/gno that referenced this issue Dec 10, 2024
This PR aims at fixing this issue
[1958](gnolang#1958)

<!-- please provide a detailed description of the changes made in this
pull request. -->

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

- [ ] 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
- [ ] Added references to related issues and PRs
- [ ] Provided any useful hints for running manual tests
</details>

---------

Co-authored-by: hieu.ha <[email protected]>
Co-authored-by: Mikael VALLENET <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 🤖 gnovm Issues or PRs gnovm related 🌟 improvement performance improvements, refactors ...
Projects
Status: Teritori (confirmed)
Development

No branches or pull requests

5 participants