diff --git a/compiler/src/dotty/tools/dotc/core/StdNames.scala b/compiler/src/dotty/tools/dotc/core/StdNames.scala index 9f128a71be7b..410f9ac83891 100644 --- a/compiler/src/dotty/tools/dotc/core/StdNames.scala +++ b/compiler/src/dotty/tools/dotc/core/StdNames.scala @@ -474,6 +474,7 @@ object StdNames { val flatMap: N = "flatMap" val floatHash: N = "floatHash" val foreach: N = "foreach" + val forEach: N = "forEach" val format: N = "format" val fromDigits: N = "fromDigits" val fromProduct: N = "fromProduct" diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 9b7db49f8843..4bd0ceb893ab 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -629,6 +629,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer assignType(tree2, TryDynamicCallType) else typedDynamicSelect(tree2, Nil, pt) + else if tree0.name == nme.foreach then + typedSelect(cpy.Select(tree0)(tree0.qualifier, nme.forEach), pt, qual) else assignType(tree, rawType match diff --git a/tests/pos/forEach-desugar.scala b/tests/pos/forEach-desugar.scala new file mode 100644 index 000000000000..471187db4ac8 --- /dev/null +++ b/tests/pos/forEach-desugar.scala @@ -0,0 +1,18 @@ +import java.util.{List, ArrayList} + +object ForEachDesugar { + + val listA = scala.List(1, 2, 3) + val listB = List.of(1, 2, 3) + val listC = ArrayList[Int]() + + for i <- listB do listB.add(i) + + for + x <- listA + y <- listB + z <- listC + do + listB.add(x + y) + +} \ No newline at end of file