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

Design synthetic tree represents "missing identifier" (e.g. anonymous given and using params in Scala3) #2458

Open
tanishiking opened this issue Aug 28, 2021 · 1 comment

Comments

@tanishiking
Copy link
Member

The discussion originally from scala/scala3#13288

Background

In Scala3, we can write anonymous using params using <TypeName> , and the compiler synthesizes the variable name like x$1 for it.

def foo(using Int) = ???
 
// synthetic
def foo(using x$1: Int) = ???

We can also write anonymous given, and the compiler will synthesize the variable name by combining given_ and it's type name.

given Int = 1

// synthetic
given given_Int: Int = 1

I believe it would be helpful to extract compiler-generated trees that represent "missing identifier" (synthesized anonymous using params and invented given) to Synthetics because if there's not the given instance application synthetics in Scala3 (introduced by scala/scala3#13288) won't have complete information.

For example, when programmers write a code using an anonymous using param, there will be a synthetic tree (in SemanticDB) that represents an application whose argument is x$2, which is synthesized name by the compiler.

def foo(x: Int)(using Int) = ???
def m(using Int) = foo(0)

// synthetics
def foo(x: Int)(using Int) = ???
def m(using Int) = foo(0)(using x$2)

If there's no information where x$2 is defined, we can't understand where x$2 comes from. And that's why I think adding "missing identifier" information to Synthetic is useful.

Problem

So, the simplest way to represent missing identifier is by wrapping the symbol by Idtree like

Synthetic(
  <Int>,
  IdTree(<x$1>)
)

// for
def foo(using Int) = ???

scala/scala3@aef23bc

The problem is

  • We can't tell the symbol wrapped by IdTree is a definition or reference. Obviously, it's a definition in this case, but it will be a problem when other synthetics are introduced and there's a pattern that is represented by Idtree(symbol).
  • I think its unclear how to process the tree because usually a synthetic has a clear "hole" representing source code which the synthetic wraps, in this case though, the source code contains a hole and the IdTree is the plug - this is an inversion of the current mental model

Options

  • Go with Idtree(symbol)
  • Wrap the IdTree with a new tree kind something like ValDef? so we can tell it is a definition.
@tgodzik
Copy link
Collaborator

tgodzik commented Aug 30, 2021

I would just leave IdTree for now until we have specific usage for a more complex data structure.

It would probably be more correct to have something like DefTree, but not sure if we would ever use the information.

@olafurpg what do you think?

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

No branches or pull requests

2 participants