Skip to content

Commit

Permalink
Fix for-comprehension with .withFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
susliko committed Apr 23, 2023
1 parent 4d14597 commit 47b35a9
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ class ForComprehension {
f
)
}
val x: Option[(Int, Int)] = None
for {
(_, _) <- x
} yield ()
}
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,15 @@ trait TextDocumentOps { self: SemanticdbOps =>
case _ =>
}
case select: g.Select if isSyntheticName(select) =>
tryFindMtree(select.qualifier)
select.qualifier match {
// This case handles multiple synthetics in a row, for example
//
// ```val foo: Option[(Int, Int)] = None
// for { (_, _) <- foo } yield ()```
// where `for` expands to `foo.withFilter(...).map(...)`
case g.Apply(s: g.Select, _) if isSyntheticName(s) => traverse(s)
case _ => tryFindMtree(select.qualifier)
}
tryFindSynthetic(select)
case gtree: g.AppliedTypeTree =>
tryFindMtree(gtree)
Expand Down
4 changes: 4 additions & 0 deletions tests/jvm/src/test/resources/example/ForComprehension.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ class ForComprehension/*<=example.ForComprehension#*/ {
f/*=>local15*/
)
}
val x/*<=example.ForComprehension#x.*/: Option/*=>scala.Option#*/[(Int/*=>scala.Int#*/, Int/*=>scala.Int#*/)] = None/*=>scala.None.*/
for {
(_, _) <- x/*=>example.ForComprehension#x.*/
} yield ()
}
30 changes: 26 additions & 4 deletions tests/jvm/src/test/resources/metac.expect_2.12
Original file line number Diff line number Diff line change
Expand Up @@ -1461,14 +1461,18 @@ Schema => SemanticDB v4
Uri => semanticdb/integration/src/main/scala/example/ForComprehension.scala
Text => non-empty
Language => Scala
Symbols => 13 entries
Occurrences => 50 entries
Synthetics => 12 entries
Symbols => 15 entries
Occurrences => 56 entries
Synthetics => 13 entries

Symbols:
example/ForComprehension# => class ForComprehension extends AnyRef { +1 decls }
example/ForComprehension# => class ForComprehension extends AnyRef { +2 decls }
AnyRef => scala/AnyRef#
example/ForComprehension#`<init>`(). => primary ctor <init>()
example/ForComprehension#x. => val method x: Option[Tuple2[Int, Int]]
Option => scala/Option#
Tuple2 => scala/Tuple2#
Int => scala/Int#
local0 => param a: Int
Int => scala/Int#
local1 => val local b: Int
Expand All @@ -1495,6 +1499,9 @@ local14 => val local e: Tuple4[Int, Int, Int, Int]
local15 => param f: Tuple4[Int, Int, Int, Int]
Tuple4 => scala/Tuple4#
Int => scala/Int#
local16 => param check$ifrefutable$2: Tuple2[Int, Int]
Tuple2 => scala/Tuple2#
Int => scala/Int#

Occurrences:
[0:8..0:15): example <= example/
Expand Down Expand Up @@ -1547,6 +1554,12 @@ Occurrences:
[39:6..39:7): d => local12
[40:6..40:7): e => local14
[41:6..41:7): f => local15
[44:6..44:7): x <= example/ForComprehension#x.
[44:9..44:15): Option => scala/Option#
[44:17..44:20): Int => scala/Int#
[44:22..44:25): Int => scala/Int#
[44:30..44:34): None => scala/None.
[46:14..46:15): x => example/ForComprehension#x.

Synthetics:
[3:2..8:19): for {
Expand Down Expand Up @@ -1715,6 +1728,15 @@ Synthetics:
apply => scala/collection/immutable/List.apply().
Tuple4 => scala/Tuple4#
Int => scala/Int#
[45:2..47:12): for {
(_, _) <- x
} yield () => orig(x).withFilter({(check$ifrefutable$2) => orig()}).map[Unit]({(local17) => orig((_, _) <- x
} yield ())})
withFilter => scala/Option#withFilter().
check$ifrefutable$2 => local16
map => scala/Option#WithFilter#map().
Unit => scala/Unit#
local17 => local17

semanticdb/integration/src/main/scala/example/ImplicitConversion.scala
----------------------------------------------------------------------
Expand Down
30 changes: 26 additions & 4 deletions tests/jvm/src/test/resources/metac.expect_2.13
Original file line number Diff line number Diff line change
Expand Up @@ -1485,14 +1485,18 @@ Schema => SemanticDB v4
Uri => semanticdb/integration/src/main/scala/example/ForComprehension.scala
Text => non-empty
Language => Scala
Symbols => 13 entries
Occurrences => 50 entries
Synthetics => 12 entries
Symbols => 15 entries
Occurrences => 56 entries
Synthetics => 13 entries

Symbols:
example/ForComprehension# => class ForComprehension extends AnyRef { +1 decls }
example/ForComprehension# => class ForComprehension extends AnyRef { +2 decls }
AnyRef => scala/AnyRef#
example/ForComprehension#`<init>`(). => primary ctor <init>()
example/ForComprehension#x. => val method x: Option[Tuple2[Int, Int]]
Option => scala/Option#
Tuple2 => scala/Tuple2#
Int => scala/Int#
local0 => param a: Int
Int => scala/Int#
local1 => val local b: Int
Expand All @@ -1519,6 +1523,9 @@ local14 => val local e: Tuple4[Int, Int, Int, Int]
local15 => param f: Tuple4[Int, Int, Int, Int]
Tuple4 => scala/Tuple4#
Int => scala/Int#
local16 => param check$ifrefutable$2: Tuple2[Int, Int]
Tuple2 => scala/Tuple2#
Int => scala/Int#

Occurrences:
[0:8..0:15): example <= example/
Expand Down Expand Up @@ -1571,6 +1578,12 @@ Occurrences:
[39:6..39:7): d => local12
[40:6..40:7): e => local14
[41:6..41:7): f => local15
[44:6..44:7): x <= example/ForComprehension#x.
[44:9..44:15): Option => scala/Option#
[44:17..44:20): Int => scala/Int#
[44:22..44:25): Int => scala/Int#
[44:30..44:34): None => scala/None.
[46:14..46:15): x => example/ForComprehension#x.

Synthetics:
[3:2..8:19): for {
Expand Down Expand Up @@ -1730,6 +1743,15 @@ Synthetics:
apply => scala/collection/IterableFactory#apply().
Tuple4 => scala/Tuple4#
Int => scala/Int#
[45:2..47:12): for {
(_, _) <- x
} yield () => orig(x).withFilter({(check$ifrefutable$2) => orig()}).map[Unit]({(local17) => orig((_, _) <- x
} yield ())})
withFilter => scala/Option#withFilter().
check$ifrefutable$2 => local16
map => scala/Option#WithFilter#map().
Unit => scala/Unit#
local17 => local17

semanticdb/integration/src/main/scala/example/ImplicitConversion.scala
----------------------------------------------------------------------
Expand Down
11 changes: 7 additions & 4 deletions tests/jvm/src/test/resources/metacp.expect_2.12
Original file line number Diff line number Diff line change
Expand Up @@ -1487,8 +1487,7 @@ com/javacp/Test#staticField. => private[javacp] static field staticField: Int
com/javacp/Test#staticMethod(). => private[javacp] static method staticMethod(): Unit
javacp => com/javacp/
Unit => scala/Unit#
com/javacp/Test#strictfpMethod(). => @strictfp private[javacp] method strictfpMethod(): Unit
strictfp => scala/annotation/strictfp#
com/javacp/Test#strictfpMethod(). => private[javacp] method strictfpMethod(): Unit
javacp => com/javacp/
Unit => scala/Unit#
com/javacp/Test#typeParams(). => private[javacp] method typeParams(a: ArrayList[HashMap[A, Array[String]]], b: Hashtable[String, B]): Unit
Expand Down Expand Up @@ -1764,12 +1763,16 @@ Schema => SemanticDB v4
Uri => example/ForComprehension.class
Text => empty
Language => Scala
Symbols => 2 entries
Symbols => 3 entries

Symbols:
example/ForComprehension# => class ForComprehension extends AnyRef { +1 decls }
example/ForComprehension# => class ForComprehension extends AnyRef { +2 decls }
AnyRef => scala/AnyRef#
example/ForComprehension#`<init>`(). => primary ctor <init>()
example/ForComprehension#x. => val method x: Option[Tuple2[Int, Int]]
Option => scala/Option#
Tuple2 => scala/Tuple2#
Int => scala/Int#

example/ImplicitConversion.class
--------------------------------
Expand Down
11 changes: 7 additions & 4 deletions tests/jvm/src/test/resources/metacp.expect_2.13
Original file line number Diff line number Diff line change
Expand Up @@ -1504,8 +1504,7 @@ com/javacp/Test#staticField. => private[javacp] static field staticField: Int
com/javacp/Test#staticMethod(). => private[javacp] static method staticMethod(): Unit
javacp => com/javacp/
Unit => scala/Unit#
com/javacp/Test#strictfpMethod(). => @strictfp private[javacp] method strictfpMethod(): Unit
strictfp => scala/annotation/strictfp#
com/javacp/Test#strictfpMethod(). => private[javacp] method strictfpMethod(): Unit
javacp => com/javacp/
Unit => scala/Unit#
com/javacp/Test#typeParams(). => private[javacp] method typeParams(a: ArrayList[HashMap[A, Array[String]]], b: Hashtable[String, B]): Unit
Expand Down Expand Up @@ -1781,12 +1780,16 @@ Schema => SemanticDB v4
Uri => example/ForComprehension.class
Text => empty
Language => Scala
Symbols => 2 entries
Symbols => 3 entries

Symbols:
example/ForComprehension# => class ForComprehension extends AnyRef { +1 decls }
example/ForComprehension# => class ForComprehension extends AnyRef { +2 decls }
AnyRef => scala/AnyRef#
example/ForComprehension#`<init>`(). => primary ctor <init>()
example/ForComprehension#x. => val method x: Option[Tuple2[Int, Int]]
Option => scala/Option#
Tuple2 => scala/Tuple2#
Int => scala/Int#

example/ImplicitConversion.class
--------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,21 @@ class TargetedSuite extends SemanticdbSuite {
assertEquals(toInt, "scala/Int#toLong().")
}
)

targeted(
"""
package n
| object ForCompWithFilter {
| val foo: Option[(Int, Int)] = None
| for {
| (_, _) <- <<foo>>
| (_, _) <- <<foo>>
| } yield ()
|}
""".stripMargin,
(_, foo1, foo2) => {
assertEquals(foo1, "n/ForComp.foo.")
assertEquals(foo2, "n/ForComp.foo.")
}
)
}

0 comments on commit 47b35a9

Please sign in to comment.