Skip to content

Commit 9e44459

Browse files
ysbaddadenSija
andauthored
Fix: TupleLiteral#* [fixup #16154] (#16206)
Co-authored-by: Sijawusz Pur Rahnama <[email protected]>
1 parent a72ae75 commit 9e44459

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

spec/compiler/macro/macro_methods_spec.cr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,6 +1545,10 @@ module Crystal
15451545
end
15461546
end
15471547
end
1548+
1549+
it "executes *" do
1550+
assert_macro %({{ {"na"} * 5}}), %({"na", "na", "na", "na", "na"})
1551+
end
15481552
end
15491553

15501554
describe "regex methods" do

src/compiler/crystal/macros.cr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,10 @@ module Crystal::Macros
11231123
# Similar to `Array#-`.
11241124
def -(other : TupleLiteral) : TupleLiteral
11251125
end
1126+
1127+
# Similar to `Tuple#*`
1128+
def *(other : NumberLiteral) : TupleLiteral
1129+
end
11261130
end
11271131

11281132
# A fictitious node representing a variable or instance

src/compiler/crystal/macros/methods.cr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ module Crystal
702702
num = arg.to_number
703703

704704
unless num.is_a?(Int)
705-
arg.raise "argument to StringLiteral#* cannot be a float"
705+
arg.raise "argument to StringLiteral#* must be an integer"
706706
end
707707

708708
StringLiteral.new(@value * num)
@@ -3188,13 +3188,13 @@ private def interpret_array_or_tuple_method(object, klass, method, args, named_a
31883188
when "*"
31893189
interpret_check_args(node: object) do |arg|
31903190
unless arg.is_a?(Crystal::NumberLiteral)
3191-
arg.raise "argument to ArrayLiteral#* must be a number, not #{arg.class_desc}"
3191+
arg.raise "argument to `#{klass}#*` must be a number, not #{arg.class_desc}"
31923192
end
31933193

31943194
num = arg.to_number
31953195

31963196
unless num.is_a?(Int)
3197-
arg.raise "argument to ArrayLiteral#* cannot be a float"
3197+
arg.raise "argument to `#{klass}#*` must be an integer"
31983198
end
31993199

32003200
klass.new(object.elements * num)

0 commit comments

Comments
 (0)