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] Constants in a loop fail #627

Closed
Ph0enixKM opened this issue Dec 4, 2024 · 2 comments · Fixed by #630
Closed

[BUG] Constants in a loop fail #627

Ph0enixKM opened this issue Dec 4, 2024 · 2 comments · Fixed by #630
Labels
bug Something isn't working compiler tests urgent Requires immediate actions

Comments

@Ph0enixKM
Copy link
Member

Describe the bug
Declaring constant with declare -r or readonly commands seem to remove the ability to overshadow them with local variables. Meaning that once we create a constant variable A and then we reference it in some function that has some local variable inside with the same name A, then we still get an error, because name A is already globally reserved for that particular constant variable created initially.

foo() {
    declare -r A=12
}

declare -r A=12

foo # ERROR: A is already declared

The same error occurs for readonly.

To Reproduce

Here is a simple shell script that reproduces the error:

foo() {
    readonly test=12
}

# Declaring a constant will cause an error if it's being redeclared in the subsequent lines
readonly test=12

echo "FUNCTIONS"

foo
foo
foo

echo "LOOPS & FUNCTIONS"

for i in 0 1 2
do
    foo
done

echo "LOOPS"

for i in 0 1 2
do
    readonly test=12
done

Expected behavior
It should never error in the code above. Each constant is created for each iteration and then goes out of scope. The same fact applies to functions.

I think that the solution to this problem is to replace bash constants with regular variables and just check the constant usage in the compilation process.

@Ph0enixKM Ph0enixKM added the bug Something isn't working label Dec 4, 2024
@github-project-automation github-project-automation bot moved this to 🆕 New in Amber Project Dec 4, 2024
@Ph0enixKM
Copy link
Member Author

Ph0enixKM commented Dec 4, 2024

This issue should cover tests for declaring constants in cases of loops, functions, conditions.

@Ph0enixKM Ph0enixKM added tests compiler urgent Requires immediate actions labels Dec 4, 2024
@Mte90
Copy link
Member

Mte90 commented Dec 5, 2024

I think that the solution to this problem is to replace bash constants with regular variables and just check the constant usage in the compilation process.

That was my idea :-P Anyway the community voted at the time to do not do it in this way, but if there is no technical solution right now is the best option.

@Ph0enixKM Ph0enixKM linked a pull request Dec 5, 2024 that will close this issue
@b1ek b1ek closed this as completed in #630 Dec 16, 2024
@github-project-automation github-project-automation bot moved this from 🆕 New to 🏁 Done in Amber Project Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler tests urgent Requires immediate actions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants