-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[WIP] Changes to Interpolation #4065
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
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5ec0731
Allow member operation to constrain
odersky 6de750e
Don't set ephemeral flag when testing whether a TypeVar is instantiated
odersky 3455656
Retract out-of-sync change
odersky 11bbb9c
More flexible tracing
odersky 9fc455a
Avoid creation of an unused closure value in each trace op
odersky 60d8b92
Allow to suppress "inlined from" parts when printing trees
odersky 6b3b5a0
Add SimpleIdentitySet data structure
odersky 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
97 changes: 97 additions & 0 deletions
97
compiler/src/dotty/tools/dotc/util/SimpleIdentitySet.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,97 @@ | ||
package dotty.tools.dotc.util | ||
|
||
import collection.mutable.ListBuffer | ||
|
||
/** A simple linked set with `eq` as the comparison, optimized for small sets. | ||
* It has linear complexity for `contains`, `+`, and `-`. | ||
*/ | ||
abstract class SimpleIdentitySet[+Elem <: AnyRef] { | ||
def size: Int | ||
def + [E >: Elem <: AnyRef](x: E): SimpleIdentitySet[E] | ||
def - [E >: Elem <: AnyRef](x: E): SimpleIdentitySet[Elem] | ||
def contains[E >: Elem <: AnyRef](x: E): Boolean | ||
def foreach(f: Elem => Unit): Unit | ||
def toList: List[Elem] = { | ||
val buf = new ListBuffer[Elem] | ||
foreach(buf += _) | ||
buf.toList | ||
} | ||
def ++ [E >: Elem <: AnyRef](that: SimpleIdentitySet[E]) = | ||
((this: SimpleIdentitySet[E]) /: that.toList)(_ + _) | ||
def -- [E >: Elem <: AnyRef](that: SimpleIdentitySet[E]) = | ||
(this /: that.toList)(_ - _) | ||
override def toString = toList.mkString("(", ", ", ")") | ||
} | ||
|
||
object SimpleIdentitySet { | ||
object empty extends SimpleIdentitySet[Nothing] { | ||
def size = 0 | ||
def + [E <: AnyRef](x: E): SimpleIdentitySet[E] = | ||
new Set1[E](x) | ||
def - [E <: AnyRef](x: E): SimpleIdentitySet[Nothing] = | ||
this | ||
def contains[E <: AnyRef](x: E): Boolean = false | ||
def foreach(f: Nothing => Unit): Unit = () | ||
} | ||
|
||
private class Set1[+Elem <: AnyRef](x0: AnyRef) extends SimpleIdentitySet[Elem] { | ||
def size = 1 | ||
def + [E >: Elem <: AnyRef](x: E): SimpleIdentitySet[E] = | ||
new Set2[E](x0, x) | ||
def - [E >: Elem <: AnyRef](x: E): SimpleIdentitySet[Elem] = | ||
if (x `eq` x0) empty else this | ||
def contains[E >: Elem <: AnyRef](x: E): Boolean = x `eq` x0 | ||
def foreach(f: Elem => Unit): Unit = f(x0.asInstanceOf[Elem]) | ||
} | ||
|
||
private class Set2[+Elem <: AnyRef](x0: AnyRef, x1: AnyRef) extends SimpleIdentitySet[Elem] { | ||
def size = 2 | ||
def + [E >: Elem <: AnyRef](x: E): SimpleIdentitySet[E] = { | ||
val xs = new Array[AnyRef](3) | ||
xs(0) = x0 | ||
xs(1) = x1 | ||
xs(2) = x | ||
new SetN[E](xs) | ||
} | ||
def - [E >: Elem <: AnyRef](x: E): SimpleIdentitySet[Elem] = | ||
if (x `eq` x0) new Set1(x1) | ||
else if (x `eq` x1) new Set1(x0) | ||
else this | ||
def contains[E >: Elem <: AnyRef](x: E): Boolean = (x `eq` x0) || (x `eq` x1) | ||
def foreach(f: Elem => Unit): Unit = { f(x0.asInstanceOf[Elem]); f(x1.asInstanceOf[Elem]) } | ||
} | ||
|
||
private class SetN[+Elem <: AnyRef](xs: Array[AnyRef]) extends SimpleIdentitySet[Elem] { | ||
def size = xs.length | ||
def + [E >: Elem <: AnyRef](x: E): SimpleIdentitySet[E] = { | ||
val xs1 = new Array[AnyRef](size + 1) | ||
Array.copy(xs, 0, xs1, 0, size) | ||
xs1(size) = x | ||
new SetN[E](xs1) | ||
} | ||
def - [E >: Elem <: AnyRef](x: E): SimpleIdentitySet[Elem] = { | ||
var i = 0 | ||
while (i < size && (xs(i) `ne` x)) i += 1 | ||
if (i == size) this | ||
else if (size == 3) | ||
if (i == 0) new Set2(xs(1), xs(2)) | ||
else if (i == 1) new Set2(xs(0), xs(2)) | ||
else new Set2(xs(0), xs(1)) | ||
else { | ||
val xs1 = new Array[AnyRef](size - 1) | ||
Array.copy(xs, 0, xs1, 0, i) | ||
Array.copy(xs, i + 1, xs1, i, size - (i + 1)) | ||
new SetN(xs1) | ||
} | ||
} | ||
def contains[E >: Elem <: AnyRef](x: E): Boolean = { | ||
var i = 0 | ||
while (i < size && (xs(i) `ne` x)) i += 1 | ||
i < size | ||
} | ||
def foreach(f: Elem => Unit): Unit = { | ||
var i = 0 | ||
while (i < size) { f(xs(i).asInstanceOf[Elem]); i += 1 } | ||
} | ||
} | ||
} |
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.
We discussed this with Martin on a whiteboard, so a memo (and possible comment).
If
X >: List[T]
andx: X
and we typecheckx.filter(p)
, how shouldX
be instantiated? Here settingX
to its lower bound works, but might violate other constraints — ideally we'd just add some constraintX <: { def filter... }
, but since we don't have those, looking upList.filter
's definition class (sayTraversable[T]
) and adding boundX <: Traversable[T]
is a more general solution.This solution is not yet complete because some other ancestor of
List[T]
which doesn't extendTraversable[T]
might have another overload offilter
. There might even be a realistic example where this happens, but I can't think of it.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.
Yes, I think it makes sense to add this. Unfortunately, it is not enough. We still need to interpolate TypeVar results in some way when selecting because otherwise we might miss an implicit conversion. E.g.
The map operation with current stdlib leaves us with result type
That
whereBut to typecheck
deep
we need to find an implicit conversion from the result toArrayOps
. There is none fromThat
, but there is one fromArray[Int]
.