Skip to content

Commit 8f2f505

Browse files
makenowjustysbaddaden
authored andcommittedAug 1, 2017
Fix chained comparisons to_s to work #4663 (#4708)
Fix to work #4663 The ASTNode of `1 <= 2 <= 3` is `to_s`-ed to `(1 <= 2) <= 3`. Right side parenthesis is not required and it causes #4653 error.
1 parent 919cba9 commit 8f2f505

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed
 

‎spec/compiler/crystal/tools/playground_spec.cr

+4
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ describe Playground::AgentInstrumentorTransformer do
9898
assert_agent %(a || b), %(_p.i(1) { a || b })
9999
end
100100

101+
it "instrument chained comparisons (#4663)" do
102+
assert_agent %(1 <= 2 <= 3), %(_p.i(1) { 1 <= 2 <= 3 })
103+
end
104+
101105
it "instrument unary expressions" do
102106
assert_agent %(pointerof(x)), %(_p.i(1) { pointerof(x) })
103107
end

‎spec/compiler/normalize/chained_comparisons_spec.cr

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ require "../../spec_helper"
22

33
describe "Normalize: chained comparisons" do
44
it "normalizes one comparison with literal" do
5-
assert_normalize "1 <= 2 <= 3", "(1 <= 2) && (2 <= 3)"
5+
assert_normalize "1 <= 2 <= 3", "1 <= 2 && 2 <= 3"
66
end
77

88
it "normalizes one comparison with var" do
9-
assert_normalize "b = 1; 1 <= b <= 3", "b = 1\n(1 <= b) && (b <= 3)"
9+
assert_normalize "b = 1; 1 <= b <= 3", "b = 1\n1 <= b && b <= 3"
1010
end
1111

1212
it "normalizes one comparison with call" do
13-
assert_normalize "1 <= b <= 3", "(1 <= (__temp_1 = b)) && (__temp_1 <= 3)"
13+
assert_normalize "1 <= b <= 3", "1 <= (__temp_1 = b) && __temp_1 <= 3"
1414
end
1515

1616
it "normalizes two comparisons with literal" do
17-
assert_normalize "1 <= 2 <= 3 <= 4", "((1 <= 2) && (2 <= 3)) && (3 <= 4)"
17+
assert_normalize "1 <= 2 <= 3 <= 4", "(1 <= 2 && 2 <= 3) && 3 <= 4"
1818
end
1919

2020
it "normalizes two comparisons with calls" do
21-
assert_normalize "1 <= a <= b <= 4", "((1 <= (__temp_2 = a)) && (__temp_2 <= (__temp_1 = b))) && (__temp_1 <= 4)"
21+
assert_normalize "1 <= a <= b <= 4", "(1 <= (__temp_2 = a) && __temp_2 <= (__temp_1 = b)) && __temp_1 <= 4"
2222
end
2323
end

‎spec/compiler/parser/to_s_spec.cr

+3
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,7 @@ describe "ASTNode#to_s" do
105105
expect_to_s %(alias Foo = Void)
106106
expect_to_s %(type(Foo = Void))
107107
expect_to_s %(return true ? 1 : 2), %(return begin\n if true\n 1\n else\n 2\n end\nend)
108+
expect_to_s %(1 <= 2 <= 3)
109+
expect_to_s %((1 <= 2) <= 3)
110+
expect_to_s %(1 <= (2 <= 3))
108111
end

‎src/compiler/crystal/syntax/to_s.cr

+1-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ module Crystal
425425
!letter_or_underscore?(obj.name)
426426
else
427427
case obj.name
428-
when "[]", "[]?"
428+
when "[]", "[]?", "<", "<=", ">", ">="
429429
false
430430
else
431431
true

0 commit comments

Comments
 (0)
Please sign in to comment.