Type of BinaryExpr is not correctly resolved in certain situations #364
Comments
It most certainly seems like a bug. Here JSS is assuming your code is compiling, and when it is compilable code the result of a sum has the same type of the element on the left I think. |
No, the type should be a String. See JLS 15.18.1 (https://docs.oracle.com/javase/specs/jls/se9/html/jls-15.html#jls-15.18.1) on the string concatenation operator:
In other words, if either the left or the right side is a String, then the result of the concatenation is also a String. Please also note that the expression |
Oh, ok, I did not expect that. It should return a string then. |
Indeed, it is quite an unusual expression... but it's valid. 😄 |
I just noticed that this also happens for other types, not only booleans:
That is, JSS seems to always report the type of the left operand for |
A similar problem appears for the remainder operator
This prints: For mulitplication and division, this works fine (i.e.,
I am going to test a few other operators at the start of next week, so maybe we'll find out that this bug affects other operators as well. So far, the following two operators are not handled correctly in all situations:
|
Nice, thank you @malteskoruppa |
A similar problem also occurs with the binary and operator Examples:
Until this point, we can say that JSS erroneously reports the type of the left operand (instead of taking both operands into account) when trying to determine the type of a binary expression for the following operators:
|
Ok, exactly the same bug as for So up to now, JSS erroneously reports the type of the left operand in certain situations when determining the type of a binary expression for the following operators:
I have tested all other operators too, except for the shift operators ( |
(I accidentally clicked the wrong button and closed by mistake 😉) |
Ok, I finished with my experiments, here comes the conclusion. First, for the shift operators (which I hadn't tested yet), it mostly works, because in the case of shift operators, the type of the whole binary expression is usually the type of the left operand, and that is exactly the type JSS seems to report. However, I noticed that unary numerical promotion is not implemented here. Consider the following snippet concerning shift operators from https://docs.oracle.com/javase/specs/jls/se9/html/jls-15.html#jls-15.19:
This means that in the following cases, for example....
...the types of the left operands of the shift expressions are So to sum all of this up, JSS has problems in resolving the types of certain binary expressions. This is partly due to the typing rules of the operators not being implemented correctly (as in the case of The operators for which I could induce the erroneous behavior are:
Examples can be found in the above posts. 😃 The correct type inference rules for all types of binary expressions can be found in JLS 15.17 through 15.24. Unary and binary numeric promotion are detailed in JLS 5.6.1 and 5.6.2, respectively. |
I think @ftomassetti recently made a fix for this. Can I close this? |
Well, I have solved it for the PLUS operator, but this remain open for other operations. I will create a new task on JP |
Moved to JP, closing |
Consider the following expression:
String s = false + "str";
Trying to resolve the type of the subexpression
false + "str"
gives me the followingResolvedType
:PrimitiveTypeUsage{name='boolean'}
.That's wrong of course, it should be a string. When the literals are the other way around, i.e., when I try to resolve
"str" + false
in the following expression:String s = "str" + false;
...it works fine, I get a
ReferenceType{java.lang.String, typeParametersMap=TypeParametersMap{nameToValue={}}}
. That's what I would have expected in the first case too.Is this a bug?
I tried to resolve the expression as follows. I used
JavaParserFacade.get(typeSolver).getType(node)
. Here,typeSolver
is aCombinedTypeSolver
of aReflectionTypeSolver
and aJavaParserTypeSolver
of a directory with a file that contains this above expression. And of course,node
is theExpression
that corresponds to the sub-expressionfalse + "str"
(like,expressionStmt.getExpression().asVariableDeclarationExpr() .getVariable(0).getInitializer().get()
).The text was updated successfully, but these errors were encountered: