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

[BUG] Redeclare const variable #606

Open
1 task done
RevengerWizard opened this issue Feb 20, 2023 · 4 comments
Open
1 task done

[BUG] Redeclare const variable #606

RevengerWizard opened this issue Feb 20, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@RevengerWizard
Copy link
Contributor

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

Currently, redeclaring a variable from const to var and trying to assign to it causes the variable to sort of "remember" it was a const variable before, therefore preventing the assignment, even though it's not a constant anymore. This problem only happens with global variables. A possible solution would be to check for redeclaration and delete the identifier from the constants table, thus allowing assignment later to it.

Related to this, importing from a file that declared global constants in the REPL brings this issue of declaration, because in the REPL the constants table isn't freed after every compilation.

Expected Behavior

The expected behaviour should be to allow the redeclaration into var and allowing assignment, or not allowing redeclaration entirely.

Steps To Reproduce

  1. Declare a const variable globally
  2. Try to redeclare it as var
  3. Assign to the redeclared variable

Anything else?

No response

@RevengerWizard RevengerWizard added the bug Something isn't working label Feb 20, 2023
@briandowns
Copy link
Contributor

Just my 2c but I think I'd prefer there be a runtime error preventing the declaration of variable of the same name.

@Jason2605
Copy link
Member

Yeah this is one that I've always questioned, it actually comes as a design decision from Lox:

This code doesn’t check to see if the key is already in the table. Lox is pretty lax with global variables and lets you redefine them without error. That’s useful in a REPL session, so the VM supports that by simply overwriting the value if the key happens to already be in the hash table.

https://craftinginterpreters.com/global-variables.html

If we are going to error this out, it'd be good to pick it up at compile time and even potentially switch the internals of the global variable to be the same as how local variables work

@briandowns
Copy link
Contributor

Completely agree. I wrote runtime but meant compile time. Given the amount of rope that scripting languages provide, so to speak, it's nice to know there's a safe guard like this.

@RevengerWizard
Copy link
Contributor Author

Indeed, though perhaps checking for local variable redeclaration might be a bit more difficult compared to globals/module ones.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants