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] Variable passed by reference uses global scope #526

Open
hdwalters opened this issue Oct 21, 2024 · 3 comments
Open

[BUG] Variable passed by reference uses global scope #526

hdwalters opened this issue Oct 21, 2024 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@hdwalters
Copy link
Contributor

hdwalters commented Oct 21, 2024

Describe the bug
If a function declares a variable by reference, but an identically named variable exists in another scope, the value is not returned correctly.

N.B. This only seems to be a problem with passing array variables by reference.

To Reproduce
Run the following script to reproduce:

$ cat ref.ab
#!/usr/bin/env amber

fun set_ref(ref numbers: [Num]): Null {
    echo "In fun before set: {numbers}"
    numbers = [4, 5, 6]
    echo "In fun after set: {numbers}"
}

main {
    let numbers = [1, 2, 3]
    echo "In main before call: {numbers}"
    set_ref(numbers)
    echo "In main after call: {numbers}"
}
$ ./ref.ab
In main before call: 1 2 3
In fun before set: numbers // <======== Expected "1 2 3"
In fun after set: 4 5 6
In main after call: 1 2 3 // <========= Expected "4 5 6"

Expected behavior
The script above should behave as it does if a different variable name is used:

$ cat ref.ab
#!/usr/bin/env amber

fun set_ref(ref byref: [Num]): Null {
    echo "In fun before set: {byref}"
    byref = [4, 5, 6]
    echo "In fun after set: {byref}"
}

main {
    let numbers = [1, 2, 3]
    echo "In main before call: {numbers}"
    set_ref(numbers)
    echo "In main after call: {numbers}"
}
$ ./ref.ab
In main before call: 1 2 3
In fun before set: 1 2 3
In fun after set: 4 5 6
In main after call: 4 5 6

Additional context
N/A

@hdwalters hdwalters added the bug Something isn't working label Oct 21, 2024
@github-project-automation github-project-automation bot moved this to 🆕 New in Amber Project Oct 21, 2024
@hdwalters hdwalters changed the title [BUG] Variables passed by reference use global scope [BUG] Variable passed by reference uses global scope Oct 21, 2024
@hdwalters
Copy link
Contributor Author

The solution to this may fall out of @Ph0enixKM's proposed translation layer, if it can generate temporary Bash variable names.

@mks-h
Copy link
Member

mks-h commented Nov 17, 2024

Just stumbled upon this one. With the shorthand add operator, it simply fails to append.

let verification = "eeded"

fun test(ref the_bug: Text): Null {
    the_bug += verification
}

main {
    let the_bug = "Succ"
    test(the_bug)
    echo the_bug // Outputs "Succ"
}

@hdwalters
Copy link
Contributor Author

Yep, that would do it; the_bug is the same in both function and main block. I don't think it's worth trying to fix anything until @Ph0enixKM has done his translation layer thingy though.

@hdwalters hdwalters self-assigned this Dec 3, 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
Projects
None yet
Development

No branches or pull requests

2 participants