Skip to content

Commit 3433e7a

Browse files
committed
[GR-45043] Adopt latest changes in Prism
PullRequest: truffleruby/4202
2 parents db4908a + da307a0 commit 3433e7a

39 files changed

+3245
-812
lines changed

spec/ruby/language/case_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,19 @@ def test(v)
415415
self.test("bar").should == false
416416
self.test(true).should == true
417417
end
418+
419+
it "warns if there are identical when clauses" do
420+
-> {
421+
eval <<~RUBY
422+
case 1
423+
when 2
424+
:foo
425+
when 2
426+
:bar
427+
end
428+
RUBY
429+
}.should complain(/warning: duplicated .when' clause with line \d+ is ignored/, verbose: true)
430+
end
418431
end
419432

420433
describe "The 'case'-construct with no target expression" do

spec/ruby/language/if_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,15 @@
305305
6.times(&b)
306306
ScratchPad.recorded.should == [4, 5, 4, 5]
307307
end
308+
309+
it "warns when Integer literals are used instead of predicates" do
310+
-> {
311+
eval <<~RUBY
312+
10.times { |i| ScratchPad << i if 4..5 }
313+
RUBY
314+
}.should complain(/warning: integer literal in flip-flop/, verbose: true)
315+
ScratchPad.recorded.should == []
316+
end
308317
end
309318

310319
describe "when a branch syntactically does not return a value" do

spec/tags/core/warning/warn_tags.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ slow:Warning.warn has Warning as the method owner
22
slow:Warning.warn can be overridden
33
slow:Warning.warn does not add a newline
44
slow:Warning.warn returns nil
5-
fails(https://github.com/ruby/prism/issues/2005):Warning.warn is called by parser warnings

spec/tags/language/if_tags.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fails:The if expression with a boolean range ('flip-flop' operator) warns when Integer literals are used instead of predicates

spec/truffle/parsing/fixtures/case/with_expression_and_when/with_splat_operator_and_preceding_and_following_elements.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,20 +103,20 @@ ast: |
103103
sourceLength = 0
104104
children:
105105
values = [
106-
StringLiteralNode
106+
FrozenStringLiteralNode
107107
attributes:
108-
encoding = UTF-8
108+
definition = expression
109109
flags = 0
110+
frozenString = bar
110111
sourceCharIndex = 31
111112
sourceLength = 5
112-
tstring = bar
113-
StringLiteralNode
113+
FrozenStringLiteralNode
114114
attributes:
115-
encoding = UTF-8
115+
definition = expression
116116
flags = 0
117+
frozenString = baz
117118
sourceCharIndex = 38
118119
sourceLength = 5
119-
tstring = baz
120120
]
121121
]
122122
ReadLocalVariableNode
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
subject: "case expression"
2+
description: "with expression to match / and String literal in when clause (case exp when 'string' ... end)"
3+
notes: >
4+
String becomes frozen and is represented as FrozenStringLiteralNode
5+
focused_on_node: "org.truffleruby.language.control.IfElseNode"
6+
ruby: |
7+
case 42
8+
when "foo"
9+
true
10+
end
11+
ast: |
12+
IfElseNodeGen
13+
attributes:
14+
flags = 0
15+
sourceCharIndex = -1
16+
sourceLength = 0
17+
children:
18+
condition =
19+
InlinedCaseEqualNodeGen
20+
attributes:
21+
assumptions = [Assumption(valid, name=set_trace_func is not used)]
22+
flags = 0
23+
integerCaseEqualAssumption = Assumption(valid, name=inlined Integer#===)
24+
parameters = RubyCallNodeParameters{methodName='===', descriptor=NoKeywordArgumentsDescriptor, isSplatted=false, ignoreVisibility=true, isVCall=false, isSafeNavigation=false, isAttrAssign=false}
25+
sourceCharIndex = -1
26+
sourceLength = 0
27+
children:
28+
leftNode_ =
29+
FrozenStringLiteralNode
30+
attributes:
31+
definition = expression
32+
flags = 0
33+
frozenString = foo
34+
sourceCharIndex = 13
35+
sourceLength = 5
36+
rightNode_ =
37+
ReadLocalVariableNode
38+
attributes:
39+
flags = 0
40+
frameSlot = 2 # %case_0
41+
sourceCharIndex = -1
42+
sourceLength = 0
43+
type = FRAME_LOCAL
44+
elseBody =
45+
NilLiteralNode
46+
attributes:
47+
flags = 0
48+
sourceCharIndex = -1
49+
sourceLength = 0
50+
thenBody =
51+
BooleanLiteralNode
52+
attributes:
53+
flags = 1
54+
sourceCharIndex = 21
55+
sourceLength = 4
56+
value = true
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
subject: "case expression"
2+
description: "without expression to match / and String literal in when clause (case exp when 'string' ... end)"
3+
notes: >
4+
String becomes frozen and is represented as FrozenStringLiteralNode
5+
focused_on_node: "org.truffleruby.language.control.IfElseNode"
6+
ruby: |
7+
case
8+
when "foo"
9+
true
10+
end
11+
ast: |
12+
IfElseNodeGen
13+
attributes:
14+
flags = 1
15+
sourceCharIndex = 0
16+
sourceLength = 26
17+
children:
18+
condition =
19+
FrozenStringLiteralNode
20+
attributes:
21+
definition = expression
22+
flags = 0
23+
frozenString = foo
24+
sourceCharIndex = 10
25+
sourceLength = 5
26+
elseBody =
27+
NilLiteralNode
28+
attributes:
29+
flags = 0
30+
sourceCharIndex = -1
31+
sourceLength = 0
32+
thenBody =
33+
BooleanLiteralNode
34+
attributes:
35+
flags = 1
36+
sourceCharIndex = 18
37+
sourceLength = 4
38+
value = true

src/main/c/yarp/include/prism.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "prism/parser.h"
2222
#include "prism/prettyprint.h"
2323
#include "prism/regexp.h"
24+
#include "prism/static_literals.h"
2425
#include "prism/version.h"
2526

2627
#include <assert.h>

src/main/c/yarp/include/prism/ast.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/* if you are looking to modify the */
66
/* template */
77
/******************************************************************************/
8+
89
/**
910
* @file ast.h
1011
*

src/main/c/yarp/include/prism/diagnostic.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,10 @@ typedef enum {
306306
PM_WARN_AMBIGUOUS_SLASH,
307307
PM_WARN_EQUAL_IN_CONDITIONAL,
308308
PM_WARN_END_IN_METHOD,
309+
PM_WARN_DUPLICATED_HASH_KEY,
310+
PM_WARN_DUPLICATED_WHEN_CLAUSE,
309311
PM_WARN_FLOAT_OUT_OF_RANGE,
312+
PM_WARN_INTEGER_IN_FLIP_FLOP,
310313

311314
// This is the number of diagnostic codes.
312315
PM_DIAGNOSTIC_ID_LEN,

0 commit comments

Comments
 (0)