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

error: assignment of read-only variable ‘const_a’ #80

Closed
3 tasks done
stefanos82 opened this issue Jul 15, 2021 · 5 comments
Closed
3 tasks done

error: assignment of read-only variable ‘const_a’ #80

stefanos82 opened this issue Jul 15, 2021 · 5 comments
Labels

Comments

@stefanos82
Copy link
Contributor

WARNING: Please, read this note carefully before submitting a new bug:

It is important to realise that this is NOT A SUPPORT FORUM, this is for reproducible BUGS ONLY.

If you need support, have questions, ideas to discuss you should use
the Nelua Discussions forum or
the Nelua Discord server.

Please, before submitting a new issue verify and check:

  • I tested it on latest Nelua version from master branch.
  • I checked there is no similar issue already reported.
  • My code has no errors or misuse of Nelua.

Bug description

A clear and concise description of the bug you are experiencing.
Tell what you were trying to do and what happened instead.

The latest commit f448b29 breaks the code below.

$ nelua const.nelua 
C compilation for 'nelua_cache/const' failed:
nelua_cache/const.c: In function ‘nelua_main’:
nelua_cache/const.c:53:11: error: assignment of read-only variable ‘const_a’
   53 |   const_a = const_x;
      |           ^

Code example

Provide minimal reproduction code to test the bug.
Please, format the code properly and try to keep it as simple as possible, just focusing on the experienced bug.

local x <const> = 1
local a <const> = x
print(a) -- outputs: 1

-- uncommenting the following will trigger the compile error:
-- error: cannot assign a constant variable
-- a = 2

Expected behavior

A clear and concise description of what you expected to happen.

To see 1 as output

Environment

Provide relevant information about your environment:

  • Nelua version (you can check with nelua -v).
Nelua 0.2.0-dev
Build number: 1155
Git date: 2021-07-15 17:28:09 -0300
Git hash: f448b29674a399600d4f1b19cd74b273233b5b36
Copyright (C) 2019-2021 Eduardo Bart (https://nelua.io/)
  • Platform (e.g. Windows/MSYS2, Ubuntu Linux, MacOS)
OS: GNU / Linux Debian testing 64-bit
  • C compiler if your bug has C compiler errors. (e.g. GCC 10.2, Clang 11)
gcc version 10.2.1 20210110 (Debian 10.2.1-6) 
@edubart
Copy link
Owner

edubart commented Jul 15, 2021

Thanks for reporting! I've just fixed. In recent commits I've started to emit more const keyword in the C code generator and this happened. I've added your example to the test suite, so this regression should not happen again.

@stefanos82
Copy link
Contributor Author

stefanos82 commented Jul 15, 2021

So local a <const> = x is not a const in C? 🤔

This is what I mean

image

@stefanos82
Copy link
Contributor Author

lol forget what I wrote above; in my mind I saw them in reverse order 🤦

@edubart
Copy link
Owner

edubart commented Jul 16, 2021

So local a <const> = x is not a const in C?

The <const> annotation is there to check at compile time for variable mutation (like in Lua), <const> in Nelua does not necessarily mean that the variable will be const in C. But some consts in Nelua depending on its semantics it will be const in C, note that this is usually not a problem because in C const does not impact performance, I've just added const in C code generator to suppress some warnings when dealing with const pointers, as side effect some integers are marked as const too and this is harmless, unless triggers a C compile error (this issue).

@stefanos82
Copy link
Contributor Author

But some consts in Nelua depending on its semantics it will be const in C, note that this is usually not a problem because in C const does not impact performance, I've just added const in C code generator to suppress some warnings when dealing with const pointers, as side effect some integers are marked as const too and this is harmless, unless triggers a C compile error (this issue).

Ah OK, I see.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants