Last active
January 10, 2018 21:09
-
-
Save splattael/1073b89bc8b9bce0542980e65517afab to your computer and use it in GitHub Desktop.
Strange macro expansions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Bug | |
macro my_method(name) | |
{{ yield }} | |
end | |
def counter | |
@counter ||= Hash(Symbol, Int32).new { |hash, key| hash[key] = 0 } | |
end | |
def run | |
begin | |
last_uid = counter[:last_uid] += 1 | |
p value: last_uid # OK | |
end | |
my_method "ok" do | |
counter[:last_uid] += 1 | |
last_uid = counter[:last_uid] | |
p value: last_uid # OK | |
end | |
my_method "breaks" do | |
last_uid = counter[:last_uid] += 1 | |
p value: last_uid # FAIL? | |
end | |
end | |
end | |
Bug.new.run | |
## Result | |
# {value: 1} | |
# {value: 2} | |
# {value: {:last_uid => 3}} <-- strange? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Macro expansions 0.23.1 | |
## "breaks" | |
1 expansion found | |
expansion 1: | |
my_method("breaks") do | |
last_uid = begin | |
__temp_6 = counter | |
__temp_6[:last_uid] = __temp_6[:last_uid] + 1 | |
end | |
p(value: last_uid) | |
end | |
~> last_uid = begin | |
__temp_6 = counter | |
__temp_6[:last_uid] = __temp_6[:last_uid] + 1 | |
end | |
p(value: last_uid) | |
## "ok" | |
1 expansion found | |
expansion 1: | |
my_method("ok") do | |
__temp_4 = counter | |
__temp_4[:last_uid] = __temp_4[:last_uid] + 1 | |
last_uid = counter[:last_uid] | |
p(value: last_uid) | |
end | |
~> __temp_4 = counter | |
__temp_4[:last_uid] = __temp_4[:last_uid] + 1 | |
last_uid = counter[:last_uid] | |
p(value: last_uid) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Macro expansions 0.24.1 | |
### "breaks" | |
# 1 expansion found | |
# expansion 1: | |
# my_method("breaks") do | |
# last_uid = __temp_4 = counter | |
# __temp_4[:last_uid] = __temp_4[:last_uid] + 1 | |
# | |
# p(value: last_uid) | |
# end | |
# | |
# # expand macro 'my_method' (/home/peter/devel/minitest.cr/test/bug_test.cr:2:3) | |
# ~> begin | |
# last_uid = __temp_4 = counter | |
# __temp_4[:last_uid] = __temp_4[:last_uid] + 1 | |
# p(value: last_uid) | |
# end | |
### "ok" | |
# 1 expansion found | |
# expansion 1: | |
# my_method("ok") do | |
# __temp_6 = counter | |
# __temp_6[:last_uid] = __temp_6[:last_uid] + 1 | |
# last_uid = counter[:last_uid] | |
# p(value: last_uid) | |
# end | |
# | |
# # expand macro 'my_method' (/home/peter/devel/minitest.cr/test/bug_test.cr:2:3) | |
# ~> begin | |
# __temp_6 = counter | |
# __temp_6[:last_uid] = __temp_6[:last_uid] + 1 | |
# last_uid = counter[:last_uid] | |
# p(value: last_uid) | |
# end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment