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

unreachable code interacts with scoping rules and changes program behavior #56874

Closed
sadish-d opened this issue Dec 20, 2024 · 4 comments
Closed

Comments

@sadish-d
Copy link

The code below prints:

I'm not guilty.

If you uncomment the line marked unleash me, it prints:

I'm guilty. But you're never going to know. Unless you've messed with time travel.

let
    let
        admit_guilt = true
    end
    if @isdefined(admit_guilt)
        admit_guilt && println("I'm guilty. But you're never going to know. Unless you've messed with time travel.")
    else
        println("I'm not guilty.")
        if false # code inside this block never executes
            println("a secret never spilled")
            # admit_guilt = true #---------------- unleash me
        end
    end
end

It reminds me of this #5148 (comment) , but I think there, the inner functions are getting pulled out and getting defined in the global scope (maybe?). I'm not sure what's going on here.

@vtjnash
Copy link
Member

vtjnash commented Dec 20, 2024

It should change the scope from a local in the let block and a different global in the isdefined test, to a local over the whole function, though I don't think that should have changed the result as observed

@Keno
Copy link
Member

Keno commented Dec 20, 2024

I'm not sure what's going on here.

The presence of the assignment later in the scope declares it as a local variable for the entire scope. In the let block that assignment then applies to the outer scope rather than introducing a new local in the let scope, yielding the observed behavior.

@vtjnash vtjnash closed this as completed Dec 20, 2024
@sadish-d
Copy link
Author

sadish-d commented Dec 20, 2024

presence of the assignment

I understand @Keno that that's what the behavior would be if the assignment was not in an if false ... else. But in this case, the assignment never executes.

let
    let
        admit_guilt = true
    end
    if @isdefined(admit_guilt)
        admit_guilt && println("I'm guilty. But you're never going to know. Unless you've messed with time travel.")
    else
        println("I'm not guilty.")
        if false # code inside this block never executes
            println("a secret never spilled")
            admit_guilt = false #---------------- unleash me ---------EDITED
        end
    end
    @assert admit_guilt #------------------------------------------EDITED
end

More importantly, @vtjnash, are you closing the issue because the current behavior is correct and expected, or because it is unfortunate but there is not much we can do about it, or something else?

@Keno
Copy link
Member

Keno commented Dec 20, 2024

I understand @Keno that that's what the behavior would be if the assignment was not in an if false ... else

Scoping rules are static and not path sensitive, so the if is irrelevant

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

No branches or pull requests

3 participants