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

No way to get a TypeRepr directly from a Symbol #13553

Closed
neko-kai opened this issue Sep 17, 2021 · 3 comments · Fixed by #14124
Closed

No way to get a TypeRepr directly from a Symbol #13553

neko-kai opened this issue Sep 17, 2021 · 3 comments · Fixed by #14124
Labels
area:metaprogramming:reflection Issues related to the quotes reflection API itype:bug
Milestone

Comments

@neko-kai
Copy link
Contributor

neko-kai commented Sep 17, 2021

Compiler version

3.1.0-RC1

Gist

Inspired by the discussion in https://gitter.im/scala/contributors?at=613e981df3449e13a46d2ea3, the recommended way to get a TypeRepr of a Symbol is to use tp.memberType(symbol) where tp is a prefix of symbol, or the type of its symbol.owner:

@smarter

if you don't have a specific prefix you care about and want to get the type as seen from its definition, the prefix should be the ThisType of the owner of the symbol

However, when we don't have a prefix type already at hand, there's no way to get a TypeRepr of the owner of the symbol - there are no methods that return TypeRepr on the Symbol! So the only way is through symbol.tree.tpe which may be very costly.

@smarter

and if it's a common enough usecase maybe we should relent and add a tpe method to symbol, if only to steer people away from tree.tpe which could be much more expensive since it can involve unpickling a tree from tasty

I think that would be the best outcome, it's a common enough usecase in my experience, and there's actually no way to get the ThisType of the owner of the symbol to do it the proper way and avoid tree parsing.

Expectation

Expected Symbol to have a method def tpe: TypeRepr

/cc @nicolasstucki

@LPTK
Copy link
Contributor

LPTK commented Sep 18, 2021

the prefix should be the ThisType of the owner of the symbol

However, when we don't have a prefix type already at hand, there's no way to get a TypeRepr of the owner of the symbol

Shouldn't it be possible to build a ThisType from a plain symbol? It seems there is no currently constructor for ThisType in the API.

Expected Symbol to have a method def tpe: TypeRepr

Maybe name the method relativeType or localType, just to make it clear that in general the type is only valid within the scope of the owner. Otherwise, users may assume they can just pick types out of their context, which will work in simple cases, but would easily lead to brittle macro implementations that yield weird errors when applied to more complicated types.

@cchantep
Copy link
Contributor

cchantep commented Oct 24, 2021

Quick (and dirty?) draft: e986a97

As workaround for paramSymss for a method symbol, the type of a parameter seems to be resolvable as below.

// p : a method parameter Symbol
p.tree match {
  case n: ValDef =>
    val t: TypeRepr = n.tpt.tpe
    println(s"param = $n / type = ${n.tpt.tpe}")

  case _ => // KO
}

@nicolasstucki
Copy link
Contributor

This seems to be linked with #9707

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:metaprogramming:reflection Issues related to the quotes reflection API itype:bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants