Skip to content

Commit 01320d1

Browse files
Backport "fix: Don't collect map, flatMap, withFilter in for-comprehension" to LTS (#20659)
Backports #18430 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents d089950 + 357ade3 commit 01320d1

File tree

3 files changed

+111
-1
lines changed

3 files changed

+111
-1
lines changed

presentation-compiler/src/main/dotty/tools/pc/PcCollector.scala

+14-1
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,9 @@ abstract class PcCollector[T](
409409
* All select statements such as:
410410
* val a = hello.<<b>>
411411
*/
412-
case sel: Select if sel.span.isCorrect && filter(sel) =>
412+
case sel: Select
413+
if sel.span.isCorrect && filter(sel) &&
414+
!isForComprehensionMethod(sel) =>
413415
occurrences + collect(
414416
sel,
415417
pos.withSpan(selectNameSpan(sel))
@@ -560,6 +562,17 @@ abstract class PcCollector[T](
560562
Span(span.start, span.start + realName.length, point)
561563
else Span(point, span.end, point)
562564
else span
565+
566+
private val forCompMethods =
567+
Set(nme.map, nme.flatMap, nme.withFilter, nme.foreach)
568+
569+
// We don't want to collect synthethic `map`, `withFilter`, `foreach` and `flatMap` in for-comprenhensions
570+
private def isForComprehensionMethod(sel: Select): Boolean =
571+
val syntheticName = sel.name match
572+
case name: TermName => forCompMethods(name)
573+
case _ => false
574+
val wrongSpan = sel.qualifier.span.contains(sel.nameSpan)
575+
syntheticName && wrongSpan
563576
end PcCollector
564577

565578
object PcCollector:

presentation-compiler/test/dotty/tools/pc/tests/edit/PcRenameSuite.scala

+13
Original file line numberDiff line numberDiff line change
@@ -471,3 +471,16 @@ class PcRenameSuite extends BasePcRenameSuite:
471471
|} yield <<a@@bc>>
472472
|""".stripMargin
473473
)
474+
475+
@Test def `map-method` =
476+
check(
477+
"""|case class Bar(x: List[Int]) {
478+
| def <<ma@@p>>(f: Int => Int): Bar = Bar(x.map(f))
479+
|}
480+
|
481+
|val f =
482+
| for {
483+
| b <- Bar(List(1,2,3))
484+
| } yield b
485+
|""".stripMargin,
486+
)

presentation-compiler/test/dotty/tools/pc/tests/highlight/DocumentHighlightSuite.scala

+84
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,90 @@ class DocumentHighlightSuite extends BaseDocumentHighlightSuite:
758758
| }
759759
|}""".stripMargin
760760
)
761+
762+
@Test def `for-comp-map` =
763+
check(
764+
"""|object Main {
765+
| val x = List(1).<<m@@ap>>(_ + 1)
766+
| val y = for {
767+
| a <- List(1)
768+
| } yield a + 1
769+
|}
770+
|""".stripMargin,
771+
)
772+
773+
@Test def `for-comp-map1` =
774+
check(
775+
"""|object Main {
776+
| val x = List(1).<<m@@ap>>(_ + 1)
777+
| val y = for {
778+
| a <- List(1)
779+
| if true
780+
| } yield a + 1
781+
|}
782+
|""".stripMargin,
783+
)
784+
785+
@Test def `for-comp-foreach` =
786+
check(
787+
"""|object Main {
788+
| val x = List(1).<<for@@each>>(_ => ())
789+
| val y = for {
790+
| a <- List(1)
791+
| } {}
792+
|}
793+
|""".stripMargin,
794+
)
795+
796+
@Test def `for-comp-withFilter` =
797+
check(
798+
"""|object Main {
799+
| val x = List(1).<<with@@Filter>>(_ => true)
800+
| val y = for {
801+
| a <- List(1)
802+
| if true
803+
| } {}
804+
|}
805+
|""".stripMargin,
806+
)
807+
808+
@Test def `for-comp-withFilter1` =
809+
check(
810+
"""|object Main {
811+
| val x = List(1).withFilter(_ => true).<<m@@ap>>(_ + 1)
812+
| val y = for {
813+
| a <- List(1)
814+
| if true
815+
| } yield a + 1
816+
|}
817+
|""".stripMargin,
818+
)
819+
820+
@Test def `for-comp-flatMap1` =
821+
check(
822+
"""|object Main {
823+
| val x = List(1).<<flat@@Map>>(_ => List(1))
824+
| val y = for {
825+
| a <- List(1)
826+
| b <- List(2)
827+
| if true
828+
| } yield a + 1
829+
|}
830+
|""".stripMargin,
831+
)
832+
833+
@Test def `for-comp-flatMap2` =
834+
check(
835+
"""|object Main {
836+
| val x = List(1).withFilter(_ => true).<<flat@@Map>>(_ => List(1))
837+
| val y = for {
838+
| a <- List(1)
839+
| if true
840+
| b <- List(2)
841+
| } yield a + 1
842+
|}
843+
|""".stripMargin,
844+
)
761845

762846
@Test def `enum1` =
763847
check(

0 commit comments

Comments
 (0)