Skip to content

Commit ba87da0

Browse files
SrTobibuilduser
authored andcommitted
[investigation] SequencesSpecialSupportDfaTest.testNilReference
(cherry picked from commit 18e19888c8ef616ea354e9cfec3b14c8b2e6e342)
1 parent ef79334 commit ba87da0

5 files changed

Lines changed: 14 additions & 7 deletions

File tree

scala/scala-impl/src/org/jetbrains/plugins/scala/lang/dfa/analysis/invocations/specialSupport/SpecialSupportUtils.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ object SpecialSupportUtils {
4242
}
4343
}
4444

45-
private def tryRetrieveCollectionSizeDirectly(dfType: DfType): Option[Int] = dfType match {
45+
def tryRetrieveCollectionSizeDirectly(dfType: DfType): Option[Int] = dfType match {
4646
case referenceType: DfReferenceType => referenceType.getSpecialFieldType match {
4747
case intConstant: DfIntConstantType => Some(intConstant.getValue.intValue)
4848
case _ => None

scala/scala-impl/src/org/jetbrains/plugins/scala/lang/dfa/controlFlow/ScalaDfaVariableDescriptor.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ case class ScalaDfaVariableDescriptor(variable: PsiElement,
2323
case _ => "<unknown>"
2424
}
2525

26-
override def getDfType(qualifier: DfaVariableValue): DfType = variable match {
26+
override def getDfType(qualifier: DfaVariableValue): DfType = dfType
27+
28+
def dfType: DfType = variable match {
2729
case typeable: Typeable =>
2830
val scType = typeable.`type`().getOrAny
2931
val nullability = variable match {

scala/scala-impl/src/org/jetbrains/plugins/scala/lang/dfa/controlFlow/transform/ExpressionTransformation.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.intellij.psi.{CommonClassNames, PsiElement, PsiMethod}
1111
import com.intellij.util.containers.FList
1212
import org.jetbrains.plugins.scala.extensions._
1313
import org.jetbrains.plugins.scala.lang.dfa.analysis.framework.ScalaStatementAnchor
14+
import org.jetbrains.plugins.scala.lang.dfa.analysis.invocations.specialSupport.SpecialSupportUtils.tryRetrieveCollectionSizeDirectly
1415
import org.jetbrains.plugins.scala.lang.dfa.controlFlow.{ScalaDfaControlFlowBuilder, ScalaDfaVariableDescriptor, TransformationFailedException}
1516
import org.jetbrains.plugins.scala.lang.dfa.utils.ScalaDfaTypeUtils.{inferExpressionType, literalToDfType}
1617
import org.jetbrains.plugins.scala.lang.psi.api.base.{ScInterpolatedStringLiteral, ScLiteral}
@@ -194,7 +195,12 @@ trait ExpressionTransformation { this: ScalaDfaControlFlowBuilder =>
194195
ScalaDfaVariableDescriptor.fromReferenceExpression(expression) match {
195196
case Some(descriptor) =>
196197
rreq.result {
197-
pushVariable(descriptor, expression)
198+
val dfType = descriptor.dfType
199+
// A reference like `Nil` already knows its collection size from its type, but the memory
200+
// state only tracks sizes of values assigned into it, so pushing it as a variable would
201+
// hide the size from collection access assertions. Push its df type directly instead.
202+
if (tryRetrieveCollectionSizeDirectly(dfType).isDefined) push(dfType, ScalaStatementAnchor(expression))
203+
else pushVariable(descriptor, expression)
198204
//buildImplicitConversion(Some(expression), Some(expectedType))
199205
}
200206
case _ =>

scala/scala-impl/test/org/jetbrains/plugins/scala/lang/dfa/analysis/tests/invocations/SequencesSpecialSupportDfaTest.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package org.jetbrains.plugins.scala.lang.dfa.analysis.tests.invocations
33
import org.jetbrains.plugins.scala.lang.dfa.Messages._
44
import org.jetbrains.plugins.scala.lang.dfa.analysis.ScalaDfaTestBase
55
import org.jetbrains.plugins.scala.lang.dfa.analysis.framework.ScalaCollectionAccessProblem.{indexOutOfBoundsProblem, noSuchElementProblem}
6-
import org.junit.{Ignore, Test}
6+
import org.junit.Test
77

88
class SequencesSpecialSupportDfaTest extends ScalaDfaTestBase {
99

@@ -80,7 +80,6 @@ class SequencesSpecialSupportDfaTest extends ScalaDfaTestBase {
8080
"list.head" -> noSuchElementProblem.alwaysMessage
8181
)
8282

83-
@Ignore("Failing test")
8483
@Test
8584
def testNilReference(): Unit = test(codeFromMethodBody(returnType = "Int") {
8685
"""

scala/scala-impl/test/org/jetbrains/plugins/scala/lang/dfa/controlFlow/transform/tests/ReferenceExpressionsControlFlowTest.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,12 @@ class ReferenceExpressionsControlFlowTest extends ScalaDfaControlFlowBuilderTest
118118
|""".stripMargin
119119
}) {
120120
"""
121-
|0: PUSH Nil
121+
|0: PUSH_VAL NOT_NULL UNMODIFIABLE size=0
122122
|1: ASSIGN_TO grades
123123
|2: POP
124124
|3: PUSH Student
125125
|4: PUSH_VAL 22
126-
|5: PUSH grades
126+
|5: PUSH_VAL NOT_NULL UNMODIFIABLE size=0
127127
|6: CALL Student#apply
128128
|7: ASSIGN_TO s1
129129
|8: POP

0 commit comments

Comments
 (0)