Skip to content

Instantly share code, notes, and snippets.

@splattael
Last active January 10, 2018 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save splattael/1073b89bc8b9bce0542980e65517afab to your computer and use it in GitHub Desktop.
Save splattael/1073b89bc8b9bce0542980e65517afab to your computer and use it in GitHub Desktop.
Strange macro expansions
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?
## 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)
## 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