Skip to content

Switch TypeApplications to given extensions #23512

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
27 changes: 13 additions & 14 deletions compiler/src/dotty/tools/dotc/core/TypeApplications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@ import StdNames.nme
import Flags.{Module, Provisional}
import dotty.tools.dotc.config.Config

object TypeApplications {
object TypeApplications:

type TypeParamInfo = ParamInfo.Of[TypeName]

/** Assert type is not a TypeBounds instance and return it unchanged */
def noBounds(tp: Type): Type = tp match {
case tp: TypeBounds => throw new AssertionError("no TypeBounds allowed")
case _ => tp
}

/** Extractor for
*
* [X1: B1, ..., Xn: Bn] -> C[X1, ..., Xn]
Expand Down Expand Up @@ -153,13 +147,13 @@ object TypeApplications {
mapOver(t)
}
}
}

import TypeApplications.*

/** A decorator that provides methods for modeling type application */
class TypeApplications(val self: Type) extends AnyVal {
/** Extensions that model type application.
*/
trait TypeApplications:
import TypeApplications.*

extension (self: Type) { // braces to avoid indent
/** The type parameters of this type are:
* For a ClassInfo type, the type parameters of its class.
* For a typeref referring to a class, the type parameters of the class.
Expand Down Expand Up @@ -554,7 +548,7 @@ class TypeApplications(val self: Type) extends AnyVal {
case _ => self.dropDependentRefinement.dealias.argInfos

/** Argument types where existential types in arguments are disallowed */
def argTypes(using Context): List[Type] = argInfos mapConserve noBounds
def argTypes(using Context): List[Type] = argInfos.mapConserve(_.noBounds)

/** Argument types where existential types in arguments are approximated by their lower bound */
def argTypesLo(using Context): List[Type] = argInfos.mapConserve(_.loBound)
Expand Down Expand Up @@ -588,4 +582,9 @@ class TypeApplications(val self: Type) extends AnyVal {
.orElse(self.baseType(defn.ArrayClass))
.argInfos.headOption.getOrElse(NoType)
}
}

/** Assert type is not a TypeBounds instance and return it unchanged */
def noBounds: self.type =
assert(!self.isInstanceOf[TypeBounds], "no TypeBounds allowed")
self
}
5 changes: 3 additions & 2 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2132,6 +2132,9 @@ object Types extends TypeUtils {
/** Is the `hash` of this type the same for all possible sequences of enclosing binders? */
def hashIsStable: Boolean = true
}
object Type:
// Extensions that model type application.
given TypeApplications()

// end Type

Expand Down Expand Up @@ -7160,8 +7163,6 @@ object Types extends TypeUtils {

// ----- Helpers and Decorator implicits --------------------------------------

implicit def decorateTypeApplications(tpe: Type): TypeApplications = new TypeApplications(tpe)

extension (tps1: List[Type]) {
@tailrec def hashIsStable: Boolean =
tps1.isEmpty || tps1.head.hashIsStable && tps1.tail.hashIsStable
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1897,9 +1897,9 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
dotc.core.Symbols.defn.isTupleNType(self)
def select(sym: Symbol): TypeRepr = self.select(sym)
def appliedTo(targ: TypeRepr): TypeRepr =
dotc.core.Types.decorateTypeApplications(self).appliedTo(targ)
Types.Type.given_TypeApplications.appliedTo(self)(targ)
def appliedTo(targs: List[TypeRepr]): TypeRepr =
dotc.core.Types.decorateTypeApplications(self).appliedTo(targs)
Types.Type.given_TypeApplications.appliedTo(self)(targs)
def substituteTypes(from: List[Symbol], to: List[TypeRepr]): TypeRepr =
self.subst(from, to)

Expand Down
Loading