Skip to content

Invent given pattern name in for comprehension #23121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

som-snytt
Copy link
Contributor

@som-snytt som-snytt commented May 8, 2025

Fixes #23119

A given pattern in a for comprehension results in a fresh val in the body of the mapping function, but it should have the same (arbitrary) name as in the rest of the expansion.

This commit gives the given its usual given name (in makeIdPat) so that the unused check can check it.

Currently, without -preview, showing that $1$ is called given_Int by typer:

➜  scala-cli repl --server=false -S 3.7.2-RC1 -Vprint:typer,refchecks -Wunused:all
Welcome to Scala 3.7.2-RC1 (17.0.15, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> for given Int <- 1 to 2; j: Int = summon[Int] yield j
1 warning found
[[syntax trees at end of                     typer]] // rs$line$1
package <empty> {
  final lazy module val rs$line$1: rs$line$1 = new rs$line$1()
  final module class rs$line$1() extends Object() { this: rs$line$1.type =>
    val res0: IndexedSeq[Int] =
      intWrapper(1).to(2).map[(Int, Int)]((x$1: Int) =>
        x$1:Int @unchecked match
          {
            case given $1$ @ _:Int =>
              val j: Int = $1$
              Tuple2.apply[Int, Int]($1$, j)
          }
      ).map[Int]((x$1: (Int, Int)) =>
        x$1:(x$1 : (Int, Int)) @unchecked match
          {
            case Tuple2.unapply[Int, Int](given given_Int @ _:Int, j @ _:Int)
               => j:Int
          }
      )
  }
}

Without -preview, this commit:

scala> for given Int <- 1 to 2; j: Int = summon[Int] yield j
[[syntax trees at end of                     typer]] // rs$line$1
package <empty> {
  final lazy module val rs$line$1: rs$line$1 = new rs$line$1()
  final module class rs$line$1() extends Object() { this: rs$line$1.type =>
    val res0: IndexedSeq[Int] =
      intWrapper(1).to(2).map[(Int, Int)]((x$1: Int) =>
        x$1:Int @unchecked match
          {
            case given given_Int @ _:Int =>
              val j: Int = given_Int
              Tuple2.apply[Int, Int](given_Int, j)
          }
      ).map[Int]((x$1: (Int, Int)) =>
        x$1:(x$1 : (Int, Int)) @unchecked match
          {
            case Tuple2.unapply[Int, Int](given given_Int @ _:Int, j @ _:Int)
               => j:Int
          }
      )
  }
}

With -preview, the tupling map is eliminated early:

scala> for given Int <- 1 to 2; j: Int = summon[Int] yield j
[[syntax trees at end of                     typer]] // rs$line$1
package <empty> {
  final lazy module val rs$line$1: rs$line$1 = new rs$line$1()
  final module class rs$line$1() extends Object() { this: rs$line$1.type =>
    val res0: IndexedSeq[Int] =
      intWrapper(1).to(2).map[Int]((x$1: Int) =>
        x$1:Int @unchecked match
          {
            case given given_Int @ _:Int =>
              val j: Int = given_Int
              j:Int
          }
      )
  }
}

@som-snytt som-snytt force-pushed the issue/23119-for-given branch from def4dd7 to 4d0f402 Compare May 9, 2025 06:19
@som-snytt som-snytt changed the title Detect anonymized patvar in given pattern De-anonymize patvar in given pattern May 9, 2025
@som-snytt som-snytt marked this pull request as ready for review May 9, 2025 06:55
@som-snytt som-snytt force-pushed the issue/23119-for-given branch 2 times, most recently from 1df2c1d to c9a9dbc Compare May 9, 2025 07:08
@Gedochao Gedochao requested a review from tgodzik May 9, 2025 08:25
@Gedochao Gedochao requested a review from sjrd May 9, 2025 12:15
@Gedochao Gedochao assigned sjrd and unassigned tgodzik May 9, 2025
@som-snytt som-snytt force-pushed the issue/23119-for-given branch from c9a9dbc to 26578ca Compare June 28, 2025 19:22
@som-snytt som-snytt changed the title De-anonymize patvar in given pattern Invent given pattern name in for comprehension Jun 28, 2025
@som-snytt som-snytt removed the request for review from tgodzik June 28, 2025 20:05
@som-snytt
Copy link
Contributor Author

@sjrd Minimized commit!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

false positive warning
3 participants