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

summonFrom and using clause should act the same #12252

Closed
liewhite opened this issue Apr 28, 2021 · 5 comments
Closed

summonFrom and using clause should act the same #12252

liewhite opened this issue Apr 28, 2021 · 5 comments

Comments

@liewhite
Copy link

liewhite commented Apr 28, 2021

3.0.0-RC3

If a report.error in a given macro and use NotGiven to deal with it, It works with a summonFrom but compile error in a using clause.

Minimized code

MkGiven.scala:

import scala.compiletime.*
import scala.quoted.*

object MkGiven:
  inline def mkInt:Int = ${ mkIntMacro }

  inline given Int = mkInt

  def mkIntMacro(using q: Quotes): Expr[Int] = 
    import q.reflect._
    report.error("no int found") // this break using but  summonFrom works well
    Expr(1)

Main.scala:

import scala.compiletime.{summonFrom}
import scala.util.NotGiven
import MkGiven.given

trait OptionGiven[T]:
  def give: Option[T]

object OptionGiven:
  given exist[T](using NotGiven[T]): OptionGiven[T] with
    def give = None

  given notExist[T](using t: T): OptionGiven[T] with
    def give = Some(t)

inline def mySummon[T] = summonFrom {
  case t: T => t
}

def usingFailed(using i:Int) = i

@main def hello: Unit = {
  val ok = mySummon[OptionGiven[Int]].give
  val error = usingFailed
}

Output

no int found

Expectation

I think the using clause and summonFrom should act the same.

@odersky
Copy link
Contributor

odersky commented Apr 29, 2021

The purpose of summonFrom is that it is not equivalent to using since it gets elaborated at the call site not the definition site. So the argument "they should act the same" does not hold per se. Can we have a self-contained example? Without that it's hard to make progress here.

@liewhite
Copy link
Author

liewhite commented May 5, 2021

@odersky update the code to reproduce the problem.

@som-snytt
Copy link
Contributor

If MkGiven uses transparent inline, then implicit search continues as described at https://dotty.epfl.ch/docs/reference/metaprogramming/inline.html#transparent-inline-methods

The no implicit argument error message adds:

But given instance given_Int in object MkGiven does not match type Int.

which is potentially confusing.

There is a similar question of how Scala 2 macro error reporting ought to interact with implicit search at scala/bug#12388

@nicolasstucki
Copy link
Contributor

The code seems to work as intended. What did you mean by this break using but summonFrom works well?

@liewhite
Copy link
Author

@nicolasstucki Oh, I forgot to close this. using and summonFrom should act differently as design.

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

No branches or pull requests

4 participants