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

Preserve integer sizes in NumberLiteral#int_bin_op #10713

Merged
merged 3 commits into from
Jun 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions spec/compiler/macro/macro_methods_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ module Crystal
assert_macro "", "{{5 % 3}}", [] of ASTNode, "2"
end

it "preserves integer size (#10713)" do
assert_macro "", "{{ 3000000000u64 % 2 }}", [] of ASTNode, "0_u64"
end

it "executes &" do
assert_macro "", "{{5 & 3}}", [] of ASTNode, "1"
end
Expand Down
17 changes: 8 additions & 9 deletions src/compiler/crystal/macros/methods.cr
Original file line number Diff line number Diff line change
Expand Up @@ -520,18 +520,17 @@ module Crystal
end

def int_bin_op(op, args)
if @kind == :f32 || @kind == :f64
raise "undefined method '#{op}' for float literal: #{self}"
end

NumberLiteral.new(bin_op(op, args) do |me, other|
other_kind = args.first.as(NumberLiteral).kind
if other_kind == :f32 || other_kind == :f64
result = bin_op(op, args) do |me, other|
if me.is_a?(Int) && other.is_a?(Int)
yield me, other
elsif me.is_a?(Float)
raise "undefined method '#{op}' for float literal: #{self}"
else
raise "argument to NumberLiteral##{op} can't be float literal: #{self}"
end
end

yield me.to_i, other.to_i
end)
NumberLiteral.new result
end

def bin_op(op, args)
Expand Down