-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #3965: Correct equality for higher-kinded types #3970
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
8 commits
Select commit
Hold shift + click to select a range
fb3aeda
Move hashing to separate object
odersky 7f97453
Suppress automatic printing of TyperState#hashesStr
odersky c275373
Structural hashes for dependent types
odersky d8ee7bf
Structural equality for dependent types
odersky aa04a38
Add test cases for #3965
odersky 8c4d873
Hash-cons all lambda types
odersky f913c43
Merge common methods of AndType and OrType
odersky 0423cf1
Make Hashing use only monomorphic dispatch
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 was deleted.
Oops, something went wrong.
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,128 @@ | ||
package dotty.tools.dotc | ||
package core | ||
|
||
import Types._ | ||
import scala.util.hashing.{ MurmurHash3 => hashing } | ||
|
||
class Hashing(binders: Array[BindingType]) { | ||
import Hashing._ | ||
|
||
private def avoidSpecialHashes(h: Int) = | ||
if (h == NotCached) NotCachedAlt | ||
else if (h == HashUnknown) HashUnknownAlt | ||
else h | ||
|
||
private def finishHash(hashCode: Int, arity: Int): Int = | ||
avoidSpecialHashes(hashing.finalizeHash(hashCode, arity)) | ||
|
||
private def typeHash(tp: Type) = | ||
if (binders == null) tp.hash else tp.computeHash(this) | ||
|
||
def identityHash(tp: Type): Int = { | ||
if (binders != null) { | ||
var idx = 0 | ||
while (idx < binders.length) { | ||
if (binders(idx) `eq` tp) return avoidSpecialHashes(idx * 31) | ||
idx += 1 | ||
} | ||
} | ||
avoidSpecialHashes(System.identityHashCode(tp)) | ||
} | ||
|
||
private def finishHash(seed: Int, arity: Int, tp: Type): Int = { | ||
val elemHash = typeHash(tp) | ||
if (elemHash == NotCached) return NotCached | ||
finishHash(hashing.mix(seed, elemHash), arity + 1) | ||
} | ||
|
||
private def finishHash(seed: Int, arity: Int, tp1: Type, tp2: Type): Int = { | ||
val elemHash = typeHash(tp1) | ||
if (elemHash == NotCached) return NotCached | ||
finishHash(hashing.mix(seed, elemHash), arity + 1, tp2) | ||
} | ||
|
||
private def finishHash(seed: Int, arity: Int, tps: List[Type]): Int = { | ||
var h = seed | ||
var xs = tps | ||
var len = arity | ||
while (xs.nonEmpty) { | ||
val elemHash = typeHash(xs.head) | ||
if (elemHash == NotCached) return NotCached | ||
h = hashing.mix(h, elemHash) | ||
xs = xs.tail | ||
len += 1 | ||
} | ||
finishHash(h, len) | ||
} | ||
|
||
private def finishHash(seed: Int, arity: Int, tp: Type, tps: List[Type]): Int = { | ||
val elemHash = typeHash(tp) | ||
if (elemHash == NotCached) return NotCached | ||
finishHash(hashing.mix(seed, elemHash), arity + 1, tps) | ||
} | ||
|
||
final def doHash(clazz: Class[_], x: Any): Int = | ||
finishHash(hashing.mix(clazz.hashCode, x.hashCode), 1) | ||
|
||
final def doHash(clazz: Class[_], tp: Type): Int = | ||
finishHash(clazz.hashCode, 0, tp) | ||
|
||
final def doHash(clazz: Class[_], x1: Any, tp2: Type): Int = | ||
finishHash(hashing.mix(clazz.hashCode, x1.hashCode), 1, tp2) | ||
|
||
final def doHash(clazz: Class[_], tp1: Type, tp2: Type): Int = | ||
finishHash(clazz.hashCode, 0, tp1, tp2) | ||
|
||
final def doHash(clazz: Class[_], x1: Any, tp2: Type, tp3: Type): Int = | ||
finishHash(hashing.mix(clazz.hashCode, x1.hashCode), 1, tp2, tp3) | ||
|
||
final def doHash(clazz: Class[_], tp1: Type, tps2: List[Type]): Int = | ||
finishHash(clazz.hashCode, 0, tp1, tps2) | ||
|
||
final def doHash(clazz: Class[_], x1: Any, tp2: Type, tps3: List[Type]): Int = | ||
finishHash(hashing.mix(clazz.hashCode, x1.hashCode), 1, tp2, tps3) | ||
|
||
final def doHash(clazz: Class[_], x1: Int, x2: Int): Int = | ||
finishHash(hashing.mix(hashing.mix(clazz.hashCode, x1), x2), 1) | ||
|
||
final def addDelta(elemHash: Int, delta: Int) = | ||
if (elemHash == NotCached) NotCached | ||
else avoidSpecialHashes(elemHash + delta) | ||
|
||
final def withBinder(binder: BindingType): Hashing = { | ||
new Hashing( | ||
if (binders == null) { | ||
val bs = new Array[BindingType](1) | ||
bs(0) = binder | ||
bs | ||
} | ||
else { | ||
val bs = new Array[BindingType](binders.length + 1) | ||
Array.copy(binders, 0, bs, 0, binders.length) | ||
bs(binders.length) = binder | ||
bs | ||
}) | ||
} | ||
} | ||
|
||
object Hashing extends Hashing(null) { | ||
|
||
/** A hash value indicating that the underlying type is not | ||
* cached in uniques. | ||
*/ | ||
final val NotCached = 0 | ||
|
||
/** An alternative value returned from `hash` if the | ||
* computed hashCode would be `NotCached`. | ||
*/ | ||
private[core] final val NotCachedAlt = Int.MinValue | ||
|
||
/** A value that indicates that the hash code is unknown | ||
*/ | ||
private[core] final val HashUnknown = 1234 | ||
|
||
/** An alternative value if computeHash would otherwise yield HashUnknown | ||
*/ | ||
private[core] final val HashUnknownAlt = 4321 | ||
} | ||
|
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,36 @@ | ||
package dotty.tools.dotc | ||
package core | ||
|
||
import annotation.tailrec | ||
import Types._ | ||
|
||
class StructEquality(binders1: Array[BindingType], binders2: Array[BindingType]) { | ||
|
||
def equals(tp1: Type, tp2: Any): Boolean = (tp1 `eq` tp2.asInstanceOf[AnyRef]) || tp1.iso(tp2, this) | ||
|
||
@tailrec final def equals(tps1: List[Type], tps2: List[Type]): Boolean = | ||
(tps1 `eq` tps2) || { | ||
if (tps1.isEmpty) tps2.isEmpty | ||
else tps2.nonEmpty && equals(tps1.head, tps2.head) && equals(tps1.tail, tps2.tail) | ||
} | ||
|
||
final def equalBinders(tp1: BindingType, tp2: BindingType): Boolean = | ||
(tp1 `eq` tp2) || { | ||
var idx = 0 | ||
while (idx < binders1.length && (tp1 `ne` binders1(idx))) | ||
idx += 1 | ||
idx < binders2.length && (tp2 `eq` binders2(idx)) | ||
} | ||
|
||
final def withBinders(binder1: BindingType, binder2: BindingType): StructEquality = { | ||
val newBinders1 = new Array[BindingType](binders1.length + 1) | ||
val newBinders2 = new Array[BindingType](binders2.length + 1) | ||
Array.copy(binders1, 0, newBinders1, 0, binders1.length) | ||
Array.copy(binders2, 0, newBinders2, 0, binders2.length) | ||
newBinders1(binders1.length) = binder1 | ||
newBinders2(binders1.length) = binder2 | ||
new StructEquality(newBinders1, newBinders2) | ||
} | ||
} | ||
|
||
object StructEquality extends StructEquality(Array(), Array()) |
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
Oops, something went wrong.
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.
This method is unused
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.
No, there are two calls in
Types
.