Skip to content

Commit

Permalink
Preserve integer sizes in NumberLiteral#int_bin_op (#10713)
Browse files Browse the repository at this point in the history
* Preserve integer sizes in `NumberLiteral#int_bin_op`
* Refactor int_bin_op for inherent type safety

Co-authored-by: Johannes Müller <straightshoota@gmail.com>
  • Loading branch information
collidedscope and straight-shoota authored Jun 13, 2021
1 parent 10e5c25 commit 6103d5a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
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

0 comments on commit 6103d5a

Please sign in to comment.