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

Possible value assignment issue #6681

Open
IGI-111 opened this issue Oct 31, 2024 · 0 comments
Open

Possible value assignment issue #6681

IGI-111 opened this issue Oct 31, 2024 · 0 comments
Assignees
Labels
bug Something isn't working compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen

Comments

@IGI-111
Copy link
Contributor

IGI-111 commented Oct 31, 2024

From the report:

I've a small doubt regarding how to reference variables to each other in sway. I have this code, where I reset the cached valueamount_cached after using it.

    let mut amount_cached = 0u64;
    let mut amount_in_used = if current_amount_in != 0 {
        current_amount_in
    } else {
        let am = amount_cached;
        // reset amount cached after it was used
        amount_cached = 0;
        am
    };

it appears that when trying to reset the amount_cached to zero the value of am also gets reset to 0 , thus always return 0 from the else condition. While assigning the value to am , it seems that it is doing it by reference rather than the value as the following snippet does work correctly where I've added + 0 while assigning value

    let mut amount_in_used = if current_amount_in != 0 {
        current_amount_in
    } else {
        let am = amount_cached + 0;
        // reset amount cached after it was used
        amount_cached = 0;
        am
    };

So, was wondering how can we enforce value assignment without using + 0 ?
here is the link to the github

@IGI-111 IGI-111 added bug Something isn't working compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen labels Oct 31, 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: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen
Projects
None yet
Development

No branches or pull requests

2 participants