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

Matching on an Expr that takes in a context function fails when the context param is used at call site #18197

Closed
arainko opened this issue Jul 12, 2023 · 1 comment · Fixed by #18198
Assignees
Labels
area:metaprogramming:quotes Issues related to quotes and splices itype:bug
Milestone

Comments

@arainko
Copy link
Contributor

arainko commented Jul 12, 2023

Compiler version

3.3.0, 3.3.1-RC3, 3.3.2-RC1-bin-20230710-ed319e8-NIGHTLY

Minimized code

//> using scala "3.3.1-RC3"

import scala.quoted.*

object ReproMacro {
  inline def readPath[A, B](inline config: Config[A, B]) = ${ readPathMacro[A, B]('config) }

  def readPathMacro[A: Type, B: Type](expr: Expr[Config[A, B]])(using Quotes) = {
    import quotes.reflect.*

    expr match {
      case '{ Field.const[a, b, tpe]($selector) } =>
        // the type of 'selector' is just a Expr[Function1[a, tpe]] instead of Expr[Selector ?=> Function1[a, tpe]]
        val weirdInferredType: Expr[a => tpe] = selector
        report.info(s"Matched!")
        '{}
      case other =>
        report.errorAndAbort("woops, I did not match")
    }
  }
}
trait Selector {
  extension [A](self: A) def at[B <: A]: B
}

trait Config[A, B]

object Field {
  def const[A, B, FieldTpe](selector: Selector ?=> A => FieldTpe): Config[A, B] = ???
}

final case class Example(int: Int)

@main def main = {
  // compiles just fine
  ReproMacro.readPath[Example, Example](Field.const(_.int))

  // doesn't compile
  ReproMacro.readPath[Example, Example](Field.const(_.int.at[Int]))
}

Output

Compiling project (Scala 3.3.1-RC3, JVM)
[error] ./Field.scala:18:3: woops, I did not match
[error]   ReproMacro.readPath[Example, Example](Field.const(_.int.at[Int]))
[error]   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expectation

Both

ReproMacro.readPath[Example, Example](Field.const(_.int))

and

ReproMacro.readPath[Example, Example](Field.const(_.int.at[Int]))

should compile.

@arainko arainko added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jul 12, 2023
@nicolasstucki nicolasstucki added area:metaprogramming:quotes Issues related to quotes and splices and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Jul 13, 2023
@nicolasstucki
Copy link
Contributor

Minimized

import scala.quoted.*

def readPathMacro[A: Type, B: Type](expr: Expr[Any])(using Quotes) =
  expr match
    case '{ foo($y) } => y: Expr[Int ?=> Int]

def foo(x: Int ?=> Int): Any = ???

When we type a splice with a contextual function type, there is an issue. We seem to be dropping the contextual parameters. Possibly a bad interaction with the logic that tries to apply the implicit arguments.

This wrong type might be causing the runtime failure as well.

nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Jul 13, 2023
@nicolasstucki nicolasstucki self-assigned this Jul 13, 2023
odersky added a commit that referenced this issue Jul 23, 2023
…n types (#18198)

The change in `typedUnnamed` fixes beta-reduction on the type that
eliminated the implicit argument. Then the second fix in
`adaptNoArgsOther` ensures that we do not transform `${x}` into `(ev ?=>
${x}.apply(x))`.

Fixes #18197
olhotak pushed a commit to dotty-staging/dotty that referenced this issue Jul 24, 2023
@Kordyjan Kordyjan added this to the 3.4.0 milestone Aug 1, 2023
Kordyjan pushed a commit that referenced this issue Dec 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:metaprogramming:quotes Issues related to quotes and splices itype:bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants