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

Broken redefinition of variable inside if-then expression #6211

Open
Akirathan opened this issue Apr 5, 2023 · 4 comments
Open

Broken redefinition of variable inside if-then expression #6211

Akirathan opened this issue Apr 5, 2023 · 4 comments
Assignees

Comments

@Akirathan
Copy link
Member

Akirathan commented Apr 5, 2023

Inside then branch of an if expression, a variable assignment is syntactically correct. The following snippet should create a new variable named a inside the then expression, and return Nothing.

a = 5
if False then a = 10 else a = 15

But it fails with

Execution finished with an error: Method `=` of 5 (Integer) could not be found.
@github-project-automation github-project-automation bot moved this to ❓New in Issues Board Apr 5, 2023
@jdunkerley jdunkerley added this to the Beta Release milestone Apr 11, 2023
@jdunkerley jdunkerley moved this from ❓New to 📤 Backlog in Issues Board Apr 11, 2023
@JaroslavTulach JaroslavTulach moved this from 📤 Backlog to ⚙️ Design in Issues Board Apr 27, 2023
@JaroslavTulach
Copy link
Member

Looks like the situation is more complex. Looking for help by language and parser experts, CCing @wdanilo and @kazcw. Following program offers "block" and "one liner" version of the same code:

block =
    a = 5
    if a == 3 then a = 10 else
        a = 15
        a # needn't be here, still OK without this line

line =
    a = 5
    if a == 3 then a = 10 else a = 15

Running block is fine. Running line generates an error as described in the issue description.

Of course using a = 10 in the "one liner" makes little sense. However using a = 10 in the block (without a being used further) makes little sense as well.

How shall the engine behave? Should the "one liner" behave just like the "block" (without a being returned)? Thank you in advance for your advises.

@JaroslavTulach
Copy link
Member

Wojciech wrote:

these 2 codes should behave the same way

I will modify the code so the "one liner" behaves the same as the block version.

@JaroslavTulach
Copy link
Member

The Tree we get from the Rust parser is causing the difference. While in the case of "oneliner" it is:

OprApp[" ", "a = 15", Ident["", "a", Ident["", "a", false, 0, false, false]], 
Either{right=Operator[" ", "=", ]}, Number[" ", "15", null, Digits["", "15", null], null]]

in case of the "block" version it is:

Assignment["        ", "a = 15", Ident["", "a", Ident["", "a", false, 0, false, false]], 
Operator[" ", "=", ], Number[" ", "15", null, Digits["", "15", null], null]]

I believe the right fix is to modify the Rust parser to treat even the "oneliner" as an Assignment. Passing to @kazcw

@enso-bot
Copy link

enso-bot bot commented May 13, 2023

Jaroslav Tulach reports a new STANDUP for yesterday (2023-05-12):

Progress: - TypeNameTest and RuntimeErrorsTest: f2df70c

Next Day: Finding a way to join Dmitry's #6655 and my #6584

Discord
Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.

@jdunkerley jdunkerley removed this from the Beta Release milestone Jul 18, 2023
@farmaazon farmaazon added this to the Future Release milestone Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: 📤 Backlog
Development

No branches or pull requests

5 participants