Skip to content

Add TermParamClause.isGiven #12042

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

Merged
merged 1 commit into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1510,6 +1510,8 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
def params: List[ValDef] = self
def isImplicit: Boolean =
self.nonEmpty && self.head.symbol.is(dotc.core.Flags.Implicit)
def isGiven: Boolean =
self.nonEmpty && self.head.symbol.is(dotc.core.Flags.Given)
end TermParamClauseMethods

type TypeParamClause = List[tpd.TypeDef]
Expand Down
4 changes: 3 additions & 1 deletion library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2156,8 +2156,10 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
extension (self: TermParamClause)
/** List of parameters of the clause */
def params: List[ValDef]
/** Is this a given parameter clause `(implicit X1, ..., Xn)`, `(given X1, ..., Xn)` or `(given x1: X1, ..., xn: Xn)` */
/** Is this an implicit parameter clause `(implicit x1: X1, ..., xn: Xn)` */
def isImplicit: Boolean
/** Is this a given parameter clause `(using X1, ..., Xn)` or `(using x1: X1, ..., xn: Xn)` */
def isGiven: Boolean
end TermParamClauseMethods

/** A type parameter clause `[X1, ..., Xn]` */
Expand Down
2 changes: 2 additions & 0 deletions tests/run-macros/i12021.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
X1: (i: scala.Int) isImplicit=true, isGiven=false
X2: (i: scala.Int) isImplicit=false, isGiven=true
15 changes: 15 additions & 0 deletions tests/run-macros/i12021/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import scala.quoted.*

inline def inspect[A]: String =
${ inspect2[A] }

def inspect2[A: Type](using Quotes): Expr[String] = {
import quotes.reflect.*

val DefDef(_, List(Nil, ps: TermParamClause), _, _) =
TypeRepr.of[A].typeSymbol.primaryConstructor.tree

val names = ps.params.map(p => s"${p.name}: ${p.tpt.show}").mkString("(", ", ", ")")

Expr(s"${Type.show[A]}: $names isImplicit=${ps.isImplicit}, isGiven=${ps.isGiven}")
}
8 changes: 8 additions & 0 deletions tests/run-macros/i12021/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

class X1(implicit i: Int)
class X2(using i: Int)

@main def Test = {
println(inspect[X1])
println(inspect[X2])
}