Skip to content

Commit

Permalink
support for shadowing of global variabls (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
Krastanov authored Feb 7, 2025
1 parent 40b7bcb commit 95716c6
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# News

## v1.0.2 - 2025-02-07

- Support shadowing of globals, e.g. `@resumable function f(); a=a+1; end;` where `a` is already an existing global.

## v1.0.1 - 2024-11-23

- Better macro hygiene (e.g. for better support in Pluto.jl)
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ license = "MIT"
desc = "C# sharp style generators a.k.a. semi-coroutines for Julia."
authors = ["Ben Lauwens and volunteer maintainers"]
repo = "https://github.com/JuliaDynamics/ResumableFunctions.jl.git"
version = "1.0.1"
version = "1.0.2"

[deps]
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
Expand Down
15 changes: 8 additions & 7 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ end
# We solve this problem by renaming all variables.
#
# We use a ScopeTracker type to keep track of the things that are already
# renamned. It is basically just a Vector{Dict{Symbol, Symbol}},
# representing a stack, where the top records the renamend variables in the
# renamed. It is basically just a Vector{Dict{Symbol, Symbol}},
# representing a stack, where the top records the renamed variables in the
# current scope.
#
# The renaming is done as follows. If we encounter an assignment of the form
Expand All @@ -261,7 +261,7 @@ end
# This is done in lookup_lhs!. Note that some construction, like `let`, create
# a new variable in a new scope. This is handled by the `new` keyword.
#
# For any other symbol y (which is not the left hand side of an assignmetn),
# For any other symbol y (which is not the left hand side of an assignment),
# there are the following two cases:
# 1) y has been seen before in some scope. Then we replace y accordingly.
# 2) y has not been seen before, then we don't rename it.
Expand Down Expand Up @@ -467,13 +467,14 @@ function scoping(expr::Expr, scope)
return quote $(res...) end
end

# the LHS is a symbol or a tuple of symbols
expr.args[1] = lookup_lhs!(expr.args[1], scope)

# now transform the RHS, this can be anything
# first transform the RHS (this can be anything) to check for shadowing of globals
for i in 2:length(expr.args)
expr.args[i] = scoping(expr.args[i], scope)
end

# only then deal with the LHS (it is a symbol or a tuple of symbols)
expr.args[1] = lookup_lhs!(expr.args[1], scope)

return expr
end
if expr.head === :macrocall
Expand Down
46 changes: 46 additions & 0 deletions test/test_globals.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using ResumableFunctions
using Test

a = 1
g() = 1
h(x) = x+1

@resumable function f1()
b = a
b = b+1
@yield b
end

@test collect(f1()) == [2]

@resumable function f2()
a = a
a = a+1
@yield a
end

@test collect(f2()) == [2]

@resumable function f3()
g = g()
g = g+1
@yield g
end

@test collect(f3()) == [2]

@resumable function f4()
a = h(a)
a = a+1
@yield a
end

@test collect(f4()) == [3]

@resumable function f5()
g = h(g())
g = g+1
@yield g
end

@test collect(f5()) == [3]

2 comments on commit 95716c6

@Krastanov
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/124563

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.0.2 -m "<description of version>" 95716c6e344f91fb694a9238374dd4e3a93e941e
git push origin v1.0.2

Please sign in to comment.