-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 TypeOf (first step towards transparent methods) #4671
Conversation
Disable stability check on SingletonTypeTree for now. Introduce TypeOf only to suspend type checking of top-level TypeApply, Apply, If and Match AST nodes.
The reason why we need to ba a proper subtype of AnnotatedType and not simply instanciate it is because we a custom equality. For instance we want to disregard the differance between Ident and TypedTrees since the later replaces the former during typing.
AnnotatedType uses precise equivalance of Trees, whereas TypeOf is only concerned with certain top level trees.
Also, restore the invariant that the type of the TypeOf tree is the TypeOf type itself. Here is an example showing what would go wrong if we didn't do that. Suppose we have def f(x: Int) = x def g(x: Int) = 2 * x def h(f: Int => Int) = f(1) h(g): { 2 * 1 } Given a type map substituting f for g in f(1), the underlying type should be substituted to the result type of g(1), that is, 2 * 1.
We now effectively -Xprint-types for trees within TypeOf. Instead, we show the underlying type for the top-level tree. Also added toString in TypeOf.
…ght be out of date)
In the new scheme, we never touch a TypeOf tree's type and ensure this by assigning that tree NoType. This currently induces additional tree copies which could be removed at a later point. In the process we also fixed bugs in TreeCopier, which would sometimes not go through TypeAssigner when it should have.
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.
.
test performance please |
I added a comment to #4616 which tries to explore the solution space and some of the tradeoffs. |
@odersky I will try to address some of your concerns about
These two points vanish as soon as we stop caring about term and only focus on types. Indeed, once trees are lifted to types there is no need to "apply type maps to terms" or keep trees and types in sync, types are the source of truth on we are being normalized.
This point is already addressed by the type Foo = {{
case class Bar(i: Int)
println(Bar(1))
1 + 2
}} But since we do not lift case class definition and side-effecting method calls in blocks, the above type is equal (and indistinguishable, from the point of view of all the infrastructure in typer) to the following: type Foo = { 1 + 2 }
The current implementation does not have any quadratic behavior and properly reuses the term level trees in the types. Everything is type-checked exactly once and lifted into a if (c1) (if (c2) t2 else e2) else e1 Type checking this term bottom up, typer will first assign the types of if (c1) (if (c2: Boolean2) t2: B else e2: C) else e1 Then it will lift the if (c1) (if (c2: Boolean2) t2: B else e2: C): inner-if-type else e1 Where inner-if-type = TypeOf(underlying = Lub(B, C), tree = If(c2, t2, e2)) Where the Type-checking then continues with {
if (c1: Boolean2) (if (c2: Boolean2) t2: B else e2: C): inner-if-type else e1: A
}: outer-if-type Where outer-if-type = TypeOf(underlying = Lub(inner-if, A), tree = If(c1, inner-if, e1)) |
So then
should get the usual type through avoidance and produce |
@Blaisorblade I didn't try it, but this is what I would like it to be. The point I was trying to make here is that even if some things do not make at the type-level, such as mutations and other side effects, the idea is that TypeOf types are able to capture everything, but will only make sense of a subset of the terms, namely if, pattern matching and function calls. Everything else is just "typed as usual", and that inferred type used as is on the type level. This is maybe better illustrated with var: var i = 0
transparent def foo(j: Int) = {
i += 1
println(i)
i + j
}
|
It would be good to try to do the test cases in #4616: typelevel.scala, typelevel1.scala in this PR. |
Here's a use case that seems hard for transparent def f(c: Boolean): Nat = {
def g[X <: Nat](x: X): X = x
g(if (c) Z else S(Z))
}
val f1 = f(true)
val f2 = f(false) The problem is that we need to prevent the type argument of |
Based only on the formalization that I @gsps and @OlivierBlanvillain started sketching, I'd want |
@Blaisorblade Yes, it makes sense, in this case the type argument of g should be inferred to be dependent. Currently dotc refuses to infer singleton types unless specified by the prototype. For instance: def foo[X](x: X): X = x
foo(1)
// With -Xprint:front -Xprint-types:
// <<<Foo.foo:([X](x: X): X)>[Int]:((x: Int): Int)>(<1:Int(1)>):Int>
// ^ X = Int This means that we need to adapt type inference to instantiate type parameters in transparent context to be as precise as possible. |
It does if you write |
Also see discussion at scala/bug#10838 |
Subsumed by #4806. |
We ended up working around several limitations of the
AnnotatedType
infrastructure:First and foremost, we discovered that a custom kind of equality is required to (type-) compare
TypeOf
s: After typing some trees may have been transformed, e.g. certainIdent
s intoTypeTree
s, which in turn breaks the "naive" tree equality employed by AnnotatedType. Instead, we defined equality ofTypeOf
based on the top-level tree node: E.g., twoTypeOf
s withif
as their top-level tree are equal if the types of theircond
s,thenb
s andelseb
s are.Similarly, we discovered that special handling is required for
TypeOf
inTypeMap
s. One crucial aspect is thatAnnotatedType
would map over the underlying type and the tree separately, potentially causing the two to go out of sync. As an example, consider the following:In other words, we can't rely on the lower path, so we instead propose to always derive the underlying type from the
TypeOf
's mapped-over tree (the top path).TreeAccumulator
would actually ignore annotations. For instance, the dependency status ofTermLambda
s wouldn't take aTypeOf
's tree into account, thus skipping some of the necessary substitutions. This can be fixed, of course, by making TreeAccumulator (or all of its relevant implementations) aware of annotations. Either way, makingAnnotatedType
work out of the box would require some additional investigation.