Skip to content

Commit

Permalink
add global lock around SafeType subtype check
Browse files Browse the repository at this point in the history
  • Loading branch information
neko-kai committed May 30, 2019
1 parent 6379c89 commit 245efd7
Showing 1 changed file with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.github.pshirshov.izumi.fundamentals.reflection

import com.github.pshirshov.izumi.fundamentals.reflection.ReflectionUtil._
import com.github.pshirshov.izumi.fundamentals.reflection.SafeType0.globalSubtypeCheckLock

import scala.reflect.runtime.{universe => ru}

// TODO: hotspots, hashcode on keys is inefficient
class SafeType0[U <: SingletonUniverse](
private val u: U // Needed just for the corner case in TagText."work with odd type prefixes" ._.
private val u: U // Needed just for the corner case in TagTest."work with odd type prefixes" ._.
, val tpe: U#Type) {

private final val dealiased: U#Type = {
Expand Down Expand Up @@ -73,14 +74,22 @@ class SafeType0[U <: SingletonUniverse](
}

final def <:<(that: SafeType0[U]): Boolean =
dealiased <:< that.dealiased || freeTermPrefixTypeSuffixHeuristicEq(_ <:< _, dealiased, that.dealiased)
globalSubtypeCheckLock.synchronized {
dealiased <:< that.dealiased || freeTermPrefixTypeSuffixHeuristicEq(_ <:< _, dealiased, that.dealiased)
}

final def weak_<:<(that: SafeType0[U]): Boolean =
(dealiased weak_<:< that.dealiased) || freeTermPrefixTypeSuffixHeuristicEq(_ weak_<:< _, dealiased, that.dealiased)
globalSubtypeCheckLock.synchronized {
(dealiased weak_<:< that.dealiased) || freeTermPrefixTypeSuffixHeuristicEq(_ weak_<:< _, dealiased, that.dealiased)
}
}

object SafeType0 {

// <:< is not thread-safe and upstream refuses to fix that.
// /~https://github.com/scala/bug/issues/10766
private[SafeType0] object globalSubtypeCheckLock

def apply[U <: SingletonUniverse](u: U, tpe: U#Type): SafeType0[U] = new SafeType0[U](u, tpe)

def apply(tpe: ru.Type): SafeType0[ru.type] = new SafeType0[ru.type](ru, tpe)
Expand Down

0 comments on commit 245efd7

Please sign in to comment.