Skip to content

Desugared guard (withFilter) in for comp fails position check #13037

Closed
scala/scala
#10870
@som-snytt

Description

@som-snytt

Reproduction steps

Scala version: 2.13.15

//> using options -Yvalidate-pos:typer
object A {
  def f(list: List[String]) = for (string <- list if string.length > 5)
    println(string)
}

Problem

def f(list: List[String]) = list.withFilter(((string) => string.length.$greater(5))).foreach(((string) => println(string)))

The problem is with the position of the string parameter.

For patvar usage tracking scala/scala#10812, the pattern tree and its duplicate in the construction of a guard expression were reversed at https://github.com/scala/scala/blob/v2.13.15/src/reflect/scala/reflect/internal/TreeGen.scala#L728 because one patvar is now tracked as an "alias" of the original other. The result is that the tree positions, which are subtly different, are reversed. In particular, one pos is an offset and the other is a range.

In 2.13.14, the offset range of the parameter to the withFilter function ([111]) is also out of range of the enclosing trees ([121:146]), but by some quirk, it doesn't fail validation. (The quirky feature is that "range" means "non-synthetic", so the enclosed tree must have a range to be checkable.)

Noticed at scoverage/scalac-scoverage-plugin#641

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions