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

Add the possibility to create a typeSymbol in the Quotes API #20347

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hamzaremmal
Copy link
Member

Closes #19448

@hamzaremmal hamzaremmal self-assigned this May 6, 2024
@hamzaremmal
Copy link
Member Author

hamzaremmal commented May 6, 2024

Note that I didn't add a test for TypeBounds because calling tree.show will not print the bounds that were generated by TypeRepr.of[...]. Nevertheless, if we comment out the following two lines, the bounds will be printed too:

This is due to the fact that when a user writes the following code: type X >: Int, the compiler will infer the upper bound to Any. The following user written code (type X >: Int) will be represented as such in the Quotes API:

TypeDef(X,TypeBoundsTree(Ident(Int),TypeTree[TypeRef(ThisType(TypeRef(NoPrefix,module class scala)),class Any)],EmptyTree))

The same type tree generated with the Quotes API will the represented as this:

TypeDef(X,TypeTree[TypeBounds(TypeRef(ThisType(TypeRef(NoPrefix,module class scala)),class Int),TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class <root>)),object scala),Any))])

Both representations are equivalent but due to how the printer is designed, we cannot show the bounds of the second representation at the moment

@hamzaremmal hamzaremmal requested review from smarter and jchyb May 6, 2024 20:13
@hamzaremmal hamzaremmal marked this pull request as ready for review May 6, 2024 20:13
@hamzaremmal hamzaremmal force-pushed the i19448 branch 3 times, most recently from f53a3bc to 38eab54 Compare May 6, 2024 20:49

def testImpl(using Quotes): Expr[Unit] = {
import quotes.reflect.*
val sym = Symbol.newType(Symbol.spliceOwner, "mytype", Flags.EmptyFlags, TypeRepr.of[String], Symbol.noSymbol)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the compiler, the info of a type symbol can either be a ClassInfo (if we're defining a class) or a TypeBounds (if we're defining a type parameter, a type alias, an abstract type member or an opaque type). So String is not a valid info, it should be TypeAlias(String), but giving the user higher-level methods like newTypeAlias and newTypeBounds would be better.

Copy link
Contributor

@jchyb jchyb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also have a test of the use case from the original discussion:
Two macro methods: both a transparent inline and an inline macro method returning a trait with a bounded type member and a type alias type member, so the incorrect stuff we generate there can be caught by -Ycheck and -Xcheck-macros

@jchyb
Copy link
Contributor

jchyb commented May 7, 2024

But great work so far! Especially with investigating the printer issue with the type bounds. I personally think showing the inferred stuff would be better than showing nothing, but this is something we can change in the future.

The TypeAlias issue makes me wonder whether the previous uses of TypeDef.apply were correct (I thought the only way to use it was to get the symbol from elsewhere and effectively use it as a type alias, but I don't think we wrap it with TypeAlias anywhere)


inline def testMacro = ${ testImpl }

def testImpl(using Quotes): Expr[Unit] = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @experimental here and in def test I believe

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

Successfully merging this pull request may close these issues.

Add a method to create a new type symbol
3 participants