Skip to content
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

Parameters are not substituted in lambdas in type annotation #19846

Closed
mbovel opened this issue Mar 1, 2024 · 2 comments · Fixed by #22002
Closed

Parameters are not substituted in lambdas in type annotation #19846

mbovel opened this issue Mar 1, 2024 · 2 comments · Fixed by #22002

Comments

@mbovel
Copy link
Member

mbovel commented Mar 1, 2024

Compiler version

Scala 3.4.2-RC1-bin-20240229-9fe0111-NIGHTLY-git-9fe0111 (17.0.8, Java OpenJDK 64-Bit Server VM)

Minimized code

Let dependentAnnotation.scala contain:

package dependentAnnotation

class lambdaAnnot(g: () => Int) extends annotation.StaticAnnotation
def f(x: Int): Int @lambdaAnnot(() => x + 1) = x

@main def main =
  val y: Int = 5
  val z = f(y)

Output

scala-cli compile -S 3.nightly -Xprint:typer --server=false dependentAnnotation.scala

outputs

...
val z:
    
      Int @lambdaAnnot(
        {
          def $anonfun(): Int = x.+(1) // Not substituted 😱
          closure($anonfun)
        }
      )
    
    = dependentAnnotation.f(y)
  ()
}
...

Expectation

x should be substituted to y.

This is the case if the annotation takes an Int parameter instead of () => Int.

Example without a lambda
package dependentAnnotation2

class intAnnot(predicate: Int) extends annotation.StaticAnnotation
def f(x: Int): Int @intAnnot(x + 1) = x

@main def main =
  val y: Int = 5
  val z = f(y)
scala-cli compile -S 3.nightly -Xprint:typer --server=false dependentAnnotation2.scala
...
val z: Int @intAnnot(y.+(1)) = dependentAnnotation2.f(y)
...
@mbovel
Copy link
Member Author

mbovel commented Mar 1, 2024

Maybe related: #18064.

@mbovel mbovel added the Spree Suitable for a future Spree label Mar 5, 2024
@mbovel mbovel removed the Spree Suitable for a future Spree label Apr 8, 2024
@mbovel
Copy link
Member Author

mbovel commented Apr 15, 2024

Also fails to pickle, similarly to #20035:

class qualified[T](predicate: T => Boolean) extends annotation.StaticAnnotation

class EqualPair(val x: Int, val y: Int @qualified[Int](it => it == x))

@main def main =
  val p = EqualPair(42, 42)
  val y = p.y
  println(42)
java.lang.AssertionError: assertion failed: unresolved symbols: <span class="ansi-color-yellow">parameter</span> <span class="ansi-color-magenta">x</span> (line 3) #7588 when pickling /tmp/scastie5434625360140563661/src/main/scala/main.scala
	at scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8)
	at dotty.tools.dotc.core.tasty.TreePickler.pickle(TreePickler.scala:829)
	at dotty.tools.dotc.transform.Pickler.run$$anonfun$1$$anonfun$1(Pickler.scala:90)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
	at scala.collection.immutable.List.foreach(List.scala:333)
	at dotty.tools.dotc.transform.Pickler.run$$anonfun$1(Pickler.scala:142)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
	at scala.collection.immutable.List.foreach(List.scala:333)
	at dotty.tools.dotc.transform.Pickler.run(Pickler.scala:142)
	at dotty.tools.dotc.core.Phases$Phase.runOn$$anonfun$1(Phases.scala:327)
	at scala.collection.immutable.List.map(List.scala:246)
	at dotty.tools.dotc.core.Phases$Phase.runOn(Phases.scala:331)
	at dotty.tools.dotc.transform.Pickler.runOn(Pickler.scala:150)
	at dotty.tools.dotc.Run.runPhases$1$$anonfun$1(Run.scala:246)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)

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

Successfully merging a pull request may close this issue.

1 participant