-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #5965: Make scala.quoted.Type poly-kinded #5988
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
014831e
Enable kind polimorphism
nicolasstucki d97f2e3
Bootstrap quoted.Type and tasty.reflect.QuotedOps
nicolasstucki 1e26f7c
Fix #5965: Make scala.quoted.Type poly-kinded
nicolasstucki b722901
Remove useless case
nicolasstucki 242cb92
Fix after rebase
nicolasstucki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package scala.quoted | ||
|
||
import scala.quoted.Types.TaggedType | ||
import scala.reflect.ClassTag | ||
import scala.runtime.quoted.Unpickler.Pickled | ||
|
||
sealed abstract class Type[T <: AnyKind] { | ||
type `$splice` = T | ||
} | ||
|
||
/** Some basic type tags, currently incomplete */ | ||
object Type { | ||
/** A term quote is desugared by the compiler into a call to this method */ | ||
def apply[T <: AnyKind]: Type[T] = | ||
throw new Error("Internal error: this method call should have been replaced by the compiler") | ||
|
||
implicit def UnitTag: Type[Unit] = new TaggedType[Unit] | ||
implicit def BooleanTag: Type[Boolean] = new TaggedType[Boolean] | ||
implicit def ByteTag: Type[Byte] = new TaggedType[Byte] | ||
implicit def CharTag: Type[Char] = new TaggedType[Char] | ||
implicit def ShortTag: Type[Short] = new TaggedType[Short] | ||
implicit def IntTag: Type[Int] = new TaggedType[Int] | ||
implicit def LongTag: Type[Long] = new TaggedType[Long] | ||
implicit def FloatTag: Type[Float] = new TaggedType[Float] | ||
implicit def DoubleTag: Type[Double] = new TaggedType[Double] | ||
} | ||
|
||
/** All implementations of Type[T]. | ||
* These should never be used directly. | ||
*/ | ||
object Types { | ||
/** A Type backed by a pickled TASTY tree */ | ||
final class TastyType[T](val tasty: Pickled, val args: Seq[Any]) extends Type[T] { | ||
override def toString(): String = s"Type(<pickled tasty>)" | ||
} | ||
|
||
/** An Type backed by a value */ | ||
final class TaggedType[T](implicit val ct: ClassTag[T]) extends Type[T] { | ||
override def toString: String = s"Type($ct)" | ||
} | ||
|
||
/** An Type backed by a tree */ | ||
final class TreeType[Tree](val typeTree: Tree) extends quoted.Type[Any] { | ||
override def toString: String = s"Type(<tasty tree>)" | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
library/src-bootstrapped/scala/tasty/reflect/QuotedOps.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package scala.tasty.reflect | ||
|
||
/** Extension methods on scala.quoted.{Expr|Type} to convert to scala.tasty.Tasty objects */ | ||
trait QuotedOps extends Core { | ||
|
||
implicit class QuotedExprAPI[T](expr: scala.quoted.Expr[T]) { | ||
/** View this expression `Expr[T]` as a `Term` */ | ||
def unseal(implicit ctx: Context): Term = | ||
kernel.QuotedExpr_unseal(expr) | ||
} | ||
|
||
implicit class QuotedTypeAPI[T <: AnyKind](tpe: scala.quoted.Type[T]) { | ||
/** View this expression `Type[T]` as a `TypeTree` */ | ||
def unseal(implicit ctx: Context): TypeTree = | ||
kernel.QuotedType_unseal(tpe) | ||
} | ||
|
||
implicit class TermToQuotedAPI(term: Term) { | ||
/** Convert `Term` to an `Expr[T]` and check that it conforms to `T` */ | ||
def seal[T](implicit tpe: scala.quoted.Type[T], ctx: Context): scala.quoted.Expr[T] = | ||
kernel.QuotedExpr_seal(term)(tpe) | ||
} | ||
|
||
implicit class TypeToQuotedAPI(tpe: Type) { | ||
/** Convert `Type` to an `quoted.Type[T]` */ | ||
def seal(implicit ctx: Context): scala.quoted.Type[_] = | ||
kernel.QuotedType_seal(tpe) | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
trait Foo[T <: AnyKind] // error: Not found: type AnyKind |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
val y: collection.immutable.List[scala.Int] = scala.List.apply[scala.Int](1, 2, 3) | ||
|
||
(y: collection.immutable.List[scala.Int]) | ||
} | ||
List(1, 2, 3) | ||
{ | ||
val y: scala.Option[scala.Int] = scala.Option.apply[scala.Int](4) | ||
|
||
(y: scala.Option[scala.Int]) | ||
} | ||
Some(4) | ||
{ | ||
val y: collection.immutable.Map[scala.Int, scala.Int] = scala.Predef.Map.apply[scala.Int, scala.Int](scala.Predef.ArrowAssoc[scala.Int](4).->[scala.Int](1)) | ||
|
||
(y: collection.immutable.Map[scala.Int, scala.Int]) | ||
} | ||
Map(4 -> 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import scala.quoted._ | ||
|
||
import scala.tasty._ | ||
|
||
object Test { | ||
|
||
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make | ||
|
||
def main(args: Array[String]): Unit = { | ||
'[List] | ||
val list = bound('{List(1, 2, 3)}) | ||
println(list.show) | ||
println(list.run) | ||
|
||
val opt = bound('{Option(4)}) | ||
println(opt.show) | ||
println(opt.run) | ||
|
||
val map = bound('{Map(4 -> 1)}) | ||
println(map.show) | ||
println(map.run) | ||
} | ||
|
||
def bound[T: Type, S[_]: Type](x: Expr[S[T]]): Expr[S[T]] = '{ | ||
val y: S[T] = $x | ||
y | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
val y: collection.immutable.List[scala.Int] = scala.List.apply[scala.Int](1, 2, 3) | ||
|
||
(y: collection.immutable.List[scala.Int]) | ||
} | ||
List(1, 2, 3) | ||
{ | ||
val y: scala.Option[scala.Int] = scala.Option.apply[scala.Int](4) | ||
|
||
(y: scala.Option[scala.Int]) | ||
} | ||
Some(4) | ||
{ | ||
val y: collection.immutable.Map[scala.Int, scala.Int] = scala.Predef.Map.apply[scala.Int, scala.Int](scala.Predef.ArrowAssoc[scala.Int](4).->[scala.Int](1)) | ||
|
||
(y: collection.immutable.Map[scala.Int, scala.Int]) | ||
} | ||
Map(4 -> 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import scala.quoted._ | ||
|
||
import scala.tasty._ | ||
|
||
object Test { | ||
|
||
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make | ||
|
||
def main(args: Array[String]): Unit = { | ||
'[List] | ||
val list = bound('{List(1, 2, 3)}) | ||
println(list.show) | ||
println(list.run) | ||
|
||
val opt = bound('{Option(4)}) | ||
println(opt.show) | ||
println(opt.run) | ||
|
||
val map = bound('{Map(4 -> 1)}) | ||
println(map.show) | ||
println(map.run) | ||
} | ||
|
||
def bound[T: Type, S[_]: Type](x: Expr[S[T]]): Expr[S[T]] = '{ | ||
val y = $x | ||
y | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here I only added
<: AnyKind
andsrc-non-bootstrapped/scala/quoted/Type.scala
contatins the current version without the bound.