Parser Problem: The expression "test || 0" yields "test"

RESOLVED DUPLICATE of bug 309957

Status

Rhino
Core
RESOLVED DUPLICATE of bug 309957
13 years ago
12 years ago

People

(Reporter: Abhinav Pobbati, Assigned: Igor Bukanov)

Tracking

Details

(Reporter)

Description

13 years ago
User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8b5) Gecko/20051006 Firefox/1.4.1
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8b5) Gecko/20051006 Firefox/1.4.1

Expression 1: "result = 0 || test"
Expression 2: "result = test"

The parser automatically changes the Expression 1 to Expression 2 and creates
the node for it. This poses to be a problem if test is null/undefined and thus
result  evaluates to null/undefined instead of 0.

This is a problem with creating the Syntax Tree.

Reproducible: Always

Steps to Reproduce:
1. Parse the expression "result = test || 0" (Expression 1)
2. Print the node returned by the parser.
3. Parse the expression "result = test" (Expression 2)
4. Print the node returned by the parser.
5. Compare the nodes of Expressions 1 and 2. They should be the same when they
shouldn't be.
Actual Results:  
"result = 0 || test" was converted to "result = test"

Expected Results:  
"result = 0 || test" should have not undergone any conversion.

Comment 1

12 years ago
Your example is flawed. "0 || test" will always evaluate to "test". The || operator operates as follows:

1) Evaluate the first operand.
2) If it's true, return it.
3) If it's false, return the second operand.

However, there is indeed a bug. Rhino incorrectly does the following:

"test || 0" => "test"
"test || 1" => "true"
"1 || test" => "true"

There's a similar bug for the && operator.

Also, this has nothing to do with the assignment operator.
Summary: Parser Problem: The expression "result = 0 || test" yields "result = test" → Parser Problem: The expression "test || 0" yields "test"

Comment 2

12 years ago

*** This bug has been marked as a duplicate of 309957 ***
Status: NEW → RESOLVED
Last Resolved: 12 years ago
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.