Skip to content

Fix reflect ByName docs and align method names #11143

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
Jan 18, 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
Original file line number Diff line number Diff line change
Expand Up @@ -1611,7 +1611,8 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
def =:=(that: TypeRepr): Boolean = self =:= that
def <:<(that: TypeRepr): Boolean = self <:< that
def widen: TypeRepr = self.widen
def widenTermRefExpr: TypeRepr = self.widenTermRefExpr
def widenTermRefByName: TypeRepr = self.widenTermRefExpr
def widenByName: TypeRepr = self.widenExpr
def dealias: TypeRepr = self.dealias
def simplified: TypeRepr = self.simplified
def classSymbol: Option[Symbol] =
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/scala/quoted/runtime/impl/Matcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ object Matcher {
case Block(List(DefDef("$anonfun", _, _, _, Some(Apply(Ident(name), _)))), _) => name
case arg => arg.symbol.name
}
val argTypes = args.map(x => x.tpe.widenTermRefExpr)
val argTypes = args.map(x => x.tpe.widenTermRefByName)
val resType = pattern.tpe
val res = Lambda(Symbol.spliceOwner, MethodType(names)(_ => argTypes, _ => resType), (meth, x) => bodyFn(x).changeOwner(meth))
matched(res.asExpr)
Expand Down
43 changes: 27 additions & 16 deletions library/src-bootstrapped/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2089,25 +2089,25 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
def <:<(that: TypeRepr): Boolean

/** Widen from singleton type to its underlying non-singleton
* base type by applying one or more `underlying` dereferences,
* Also go from => T to T.
* Identity for all other types. Example:
*
* class Outer { class C ; val x: C }
* def o: Outer
* <o.x.type>.widen = o.C
*/
* base type by applying one or more `underlying` dereferences,
* Also go from => T to T.
* Identity for all other types. Example:
*
* class Outer { class C ; val x: C }
* def o: Outer
* <o.x.type>.widen = o.C
*/
def widen: TypeRepr

/** Widen from TermRef to its underlying non-termref
* base type, while also skipping `=>T` types.
*/
def widenTermRefExpr: TypeRepr
* base type, while also skipping ByName types.
*/
def widenTermRefByName: TypeRepr

/** Follow aliases and dereferences LazyRefs, annotated types and instantiated
* TypeVars until type is no longer alias type, annotated type, LazyRef,
* or instantiated type variable.
*/
/** Widen from ByName type to its result type. */
def widenByName: TypeRepr

/** Follow aliases, annotated types until type is no longer alias type, annotated type. */
def dealias: TypeRepr

/** A simplified version of this type which is equivalent wrt =:= to this type.
Expand Down Expand Up @@ -2439,7 +2439,18 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
end extension
end MatchTypeMethods

/** Type of a by by name parameter */
/** Type of a by-name definition of type `=>T`.
*
* May represent by-name parameter such as `thunk` in
* ```scala
* def log[T](thunk: =>T): T = ...
* ```
*
* May also represent a the return type of a parameterless method definition such as
* ```scala
* def foo: Int = ...
* ```
*/
type ByNameType <: TypeRepr

/** `TypeTest` that allows testing at runtime in a pattern match if a `TypeRepr` is a `ByNameType` */
Expand Down
1 change: 1 addition & 0 deletions library/src-non-bootstrapped/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
def widen: TypeRepr

def widenTermRefExpr: TypeRepr
def widenTermRefByName: TypeRepr = widenTermRefExpr

def dealias: TypeRepr

Expand Down
4 changes: 2 additions & 2 deletions tests/run-macros/refined-selectable-macro/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ object Macro {
private def toTupleImpl(s: Expr[Selectable])(using qctx:Quotes) : Expr[Tuple] = {
import quotes.reflect._

val repr = s.asTerm.tpe.widenTermRefExpr.dealias
val repr = s.asTerm.tpe.widenTermRefByName.dealias

def rec(tpe: TypeRepr): List[(String, TypeRepr)] = {
tpe match {
Expand Down Expand Up @@ -51,7 +51,7 @@ object Macro {
private def fromTupleImpl[T: Type](s: Expr[Tuple], newRecord: Expr[Array[(String, Any)] => T])(using qctx:Quotes) : Expr[Any] = {
import quotes.reflect._

val repr = s.asTerm.tpe.widenTermRefExpr.dealias
val repr = s.asTerm.tpe.widenTermRefByName.dealias

def isTupleCons(sym: Symbol): Boolean = sym.owner == defn.ScalaPackageClass && sym.name == "*:"

Expand Down