Skip to content

Commit

Permalink
Merge pull request jruby#7354 from enebo/fix_7317
Browse files Browse the repository at this point in the history
Fix 7317 it warns me about "warning: found = in conditional, should be =="
  • Loading branch information
enebo authored Sep 9, 2022
2 parents 42a012f + 41b08cc commit 489cd92
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 17 deletions.
8 changes: 8 additions & 0 deletions core/src/main/java/org/jruby/ast/BignumNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
import java.math.BigInteger;
import java.util.List;

import org.jruby.Ruby;
import org.jruby.RubyBignum;
import org.jruby.ast.visitor.NodeVisitor;
import org.jruby.runtime.builtin.IRubyObject;

/**
* Represents a big integer literal.
Expand Down Expand Up @@ -77,4 +80,9 @@ public List<Node> childNodes() {
public void setValue(BigInteger value) {
this.value = value;
}

@Override
public IRubyObject literalValue(Ruby runtime) {
return RubyBignum.newBignum(runtime, value);
}
}
9 changes: 9 additions & 0 deletions core/src/main/java/org/jruby/ast/ComplexNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
package org.jruby.ast;

import java.util.List;

import org.jruby.Ruby;
import org.jruby.ast.visitor.NodeVisitor;
import org.jruby.ir.runtime.IRRuntimeHelpers;
import org.jruby.runtime.builtin.IRubyObject;

/**
*
Expand Down Expand Up @@ -44,4 +48,9 @@ public NumericNode getNumber() {
public void setNumber(NumericNode y) {
this.y = y;
}

@Override
public IRubyObject literalValue(Ruby runtime) {
return IRRuntimeHelpers.newComplexRaw(runtime.getCurrentContext(), y.literalValue(runtime));
}
}
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/FixnumNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
/**
* Represents an integer literal.
*/
public class FixnumNode extends NumericNode implements ILiteralNode, LiteralValue, SideEffectFree {
public class FixnumNode extends NumericNode implements ILiteralNode, SideEffectFree {
private long value;

public FixnumNode(int line, long value) {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/ast/NumericNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Any node representing a numeric value.
*/
public abstract class NumericNode extends Node implements ILiteralNode {
public abstract class NumericNode extends Node implements ILiteralNode, LiteralValue {
public NumericNode(int line) {
super(line, false);
}
Expand Down
12 changes: 11 additions & 1 deletion core/src/main/java/org/jruby/ast/RationalNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
package org.jruby.ast;

import java.util.List;

import org.jruby.Ruby;
import org.jruby.RubyRational;
import org.jruby.ast.visitor.NodeVisitor;
import org.jruby.runtime.builtin.IRubyObject;

/**
*
* @author enebo
*/
public class RationalNode extends NumericNode implements SideEffectFree {
public class RationalNode extends NumericNode implements LiteralValue, SideEffectFree {
private final NumericNode numerator;
private final NumericNode denominator;

Expand Down Expand Up @@ -51,4 +55,10 @@ public NumericNode getNumerator() {
public NumericNode getDenominator() {
return denominator;
}

@Override
public IRubyObject literalValue(Ruby runtime) {
return RubyRational.newRationalCanonicalize(runtime.getCurrentContext(),
numerator.literalValue(runtime), denominator.literalValue(runtime));
}
}
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/parser/ParserSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ private boolean checkAssignmentInCondition(Node node) {
if (node instanceof MultipleAsgnNode || node instanceof LocalAsgnNode || node instanceof DAsgnNode || node instanceof GlobalAsgnNode || node instanceof InstAsgnNode) {
Node valueNode = ((AssignableNode) node).getValueNode();
if (isStaticContent(valueNode)) {
warnings.warn(ID.ASSIGNMENT_IN_CONDITIONAL, lexer.getFile(), valueNode.getLine(), "found = in conditional, should be ==");
warnings.warn(ID.ASSIGNMENT_IN_CONDITIONAL, lexer.getFile(), valueNode.getLine(), "found `= literal' in conditional, should be ==");
}
return true;
}
Expand All @@ -617,7 +617,7 @@ private static boolean isStaticContent(Node node) {
return true;
}

return node instanceof ILiteralNode || node instanceof NilNode || node instanceof TrueNode || node instanceof FalseNode;
return node instanceof LiteralValue || node instanceof NilNode || node instanceof TrueNode || node instanceof FalseNode;
}

protected Node makeNullNil(Node node) {
Expand Down
12 changes: 6 additions & 6 deletions core/src/main/java/org/jruby/parser/RubyParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1860,12 +1860,12 @@ public Object yyparse (RubyLexer yyLex) throws java.io.IOException {
return yyVal;
};
states[27] = (ParserSupport support, RubyLexer lexer, Object yyVal, ProductionState[] yyVals, int yyTop, int count, int yychar) -> {
yyVal = support.new_if(support.getPosition(((Node)yyVals[-2+yyTop].value)), support.cond(((Node)yyVals[0+yyTop].value)), ((Node)yyVals[-2+yyTop].value), null);
yyVal = support.new_if(support.getPosition(((Node)yyVals[-2+yyTop].value)), ((Node)yyVals[0+yyTop].value), ((Node)yyVals[-2+yyTop].value), null);
support.fixpos(((Node)yyVal), ((Node)yyVals[0+yyTop].value));
return yyVal;
};
states[28] = (ParserSupport support, RubyLexer lexer, Object yyVal, ProductionState[] yyVals, int yyTop, int count, int yychar) -> {
yyVal = support.new_if(support.getPosition(((Node)yyVals[-2+yyTop].value)), support.cond(((Node)yyVals[0+yyTop].value)), null, ((Node)yyVals[-2+yyTop].value));
yyVal = support.new_if(support.getPosition(((Node)yyVals[-2+yyTop].value)), ((Node)yyVals[0+yyTop].value), null, ((Node)yyVals[-2+yyTop].value));
support.fixpos(((Node)yyVal), ((Node)yyVals[0+yyTop].value));
return yyVal;
};
Expand Down Expand Up @@ -2854,7 +2854,7 @@ public Object yyparse (RubyLexer yyLex) throws java.io.IOException {
};
states[262] = (ParserSupport support, RubyLexer lexer, Object yyVal, ProductionState[] yyVals, int yyTop, int count, int yychar) -> {
support.value_expr(lexer, ((Node)yyVals[-5+yyTop].value));
yyVal = support.new_if(support.getPosition(((Node)yyVals[-5+yyTop].value)), support.cond(((Node)yyVals[-5+yyTop].value)), ((Node)yyVals[-3+yyTop].value), ((Node)yyVals[0+yyTop].value));
yyVal = support.new_if(support.getPosition(((Node)yyVals[-5+yyTop].value)), ((Node)yyVals[-5+yyTop].value), ((Node)yyVals[-3+yyTop].value), ((Node)yyVals[0+yyTop].value));
return yyVal;
};
states[263] = (ParserSupport support, RubyLexer lexer, Object yyVal, ProductionState[] yyVals, int yyTop, int count, int yychar) -> {
Expand Down Expand Up @@ -3165,11 +3165,11 @@ public Object yyparse (RubyLexer yyLex) throws java.io.IOException {
return yyVal;
};
states[337] = (ParserSupport support, RubyLexer lexer, Object yyVal, ProductionState[] yyVals, int yyTop, int count, int yychar) -> {
yyVal = support.new_if(((Integer)yyVals[-5+yyTop].value), support.cond(((Node)yyVals[-4+yyTop].value)), ((Node)yyVals[-2+yyTop].value), ((Node)yyVals[-1+yyTop].value));
yyVal = support.new_if(((Integer)yyVals[-5+yyTop].value), ((Node)yyVals[-4+yyTop].value), ((Node)yyVals[-2+yyTop].value), ((Node)yyVals[-1+yyTop].value));
return yyVal;
};
states[338] = (ParserSupport support, RubyLexer lexer, Object yyVal, ProductionState[] yyVals, int yyTop, int count, int yychar) -> {
yyVal = support.new_if(((Integer)yyVals[-5+yyTop].value), support.cond(((Node)yyVals[-4+yyTop].value)), ((Node)yyVals[-1+yyTop].value), ((Node)yyVals[-2+yyTop].value));
yyVal = support.new_if(((Integer)yyVals[-5+yyTop].value), ((Node)yyVals[-4+yyTop].value), ((Node)yyVals[-1+yyTop].value), ((Node)yyVals[-2+yyTop].value));
return yyVal;
};
states[339] = (ParserSupport support, RubyLexer lexer, Object yyVal, ProductionState[] yyVals, int yyTop, int count, int yychar) -> {
Expand Down Expand Up @@ -3364,7 +3364,7 @@ public Object yyparse (RubyLexer yyLex) throws java.io.IOException {
return yyVal;
};
states[377] = (ParserSupport support, RubyLexer lexer, Object yyVal, ProductionState[] yyVals, int yyTop, int count, int yychar) -> {
yyVal = support.new_if(((Integer)yyVals[-4+yyTop].value), support.cond(((Node)yyVals[-3+yyTop].value)), ((Node)yyVals[-1+yyTop].value), ((Node)yyVals[0+yyTop].value));
yyVal = support.new_if(((Integer)yyVals[-4+yyTop].value), ((Node)yyVals[-3+yyTop].value), ((Node)yyVals[-1+yyTop].value), ((Node)yyVals[0+yyTop].value));
return yyVal;
};
states[379] = (ParserSupport support, RubyLexer lexer, Object yyVal, ProductionState[] yyVals, int yyTop, int count, int yychar) -> {
Expand Down
12 changes: 6 additions & 6 deletions core/src/main/java/org/jruby/parser/RubyParser.y
Original file line number Diff line number Diff line change
Expand Up @@ -472,11 +472,11 @@ stmt : keyword_alias fitem {
$$ = $2;
}
| stmt modifier_if expr_value {
$$ = support.new_if(support.getPosition($1), support.cond($3), $1, null);
$$ = support.new_if(support.getPosition($1), $3, $1, null);
support.fixpos($<Node>$, $3);
}
| stmt modifier_unless expr_value {
$$ = support.new_if(support.getPosition($1), support.cond($3), null, $1);
$$ = support.new_if(support.getPosition($1), $3, null, $1);
support.fixpos($<Node>$, $3);
}
| stmt modifier_while expr_value {
Expand Down Expand Up @@ -1293,7 +1293,7 @@ arg : lhs '=' arg_rhs {
}
| arg '?' arg opt_nl ':' arg {
support.value_expr(lexer, $1);
$$ = support.new_if(support.getPosition($1), support.cond($1), $3, $6);
$$ = support.new_if(support.getPosition($1), $1, $3, $6);
}
| primary {
$$ = $1;
Expand Down Expand Up @@ -1575,10 +1575,10 @@ primary : literal
$$ = $2;
}
| keyword_if expr_value then compstmt if_tail keyword_end {
$$ = support.new_if($1, support.cond($2), $4, $5);
$$ = support.new_if($1, $2, $4, $5);
}
| keyword_unless expr_value then compstmt opt_else keyword_end {
$$ = support.new_if($1, support.cond($2), $5, $4);
$$ = support.new_if($1, $2, $5, $4);
}
| keyword_while {
lexer.getConditionState().push1();
Expand Down Expand Up @@ -1741,7 +1741,7 @@ do : term

if_tail : opt_else
| keyword_elsif expr_value then compstmt if_tail {
$$ = support.new_if($1, support.cond($2), $4, $5);
$$ = support.new_if($1, $2, $4, $5);
}

opt_else : none
Expand Down

0 comments on commit 489cd92

Please sign in to comment.