Skip to content

Fix #5527: Fix companion module for hk opaque type aliases #5628

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

Merged
merged 1 commit into from
Dec 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions compiler/src/dotty/tools/dotc/core/SymDenotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -991,10 +991,15 @@ object SymDenotations {
*/
final def companionModule(implicit ctx: Context): Symbol =
if (is(Module)) sourceModule
else if (isOpaqueAlias)
info match {
case TypeAlias(TypeRef(prefix: TermRef, _)) => prefix.termSymbol
else if (isOpaqueAlias) {
def reference(tp: Type): TermRef = tp match {
case TypeRef(prefix: TermRef, _) => prefix
case tp: HKTypeLambda => reference(tp.resType)
case tp: AppliedType => reference(tp.tycon)
}
val TypeAlias(alias) = info
reference(alias).termSymbol
}
else registeredCompanion.sourceModule

private def companionType(implicit ctx: Context): Symbol =
Expand Down
35 changes: 35 additions & 0 deletions tests/run/i5527.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
trait Contravariant[F[_]] {
def contramap[A, B](fa: F[A])(f: B => A): F[B]
}

object Library {

opaque type Set[A] = A => Boolean

object Set {
def singleton[A](a: A): Set[A] =
_ == a

implicit class SetOps[A](private val set: Set[A]) extends AnyVal {
def contains(a: A): Boolean =
set(a)
}

implicit val setContravariant: Contravariant[Set] = new Contravariant[Set] {
def contramap[A, B](fa: Set[A])(f: B => A): Set[B] =
b => fa(f(b))
}
}
}

object Test extends App {
import Library._
//import Library.Set.setContravariant if this is imported the program will run correctly

val F = implicitly[Contravariant[Set]]

val one = Set.singleton(1)
val char = F.contramap(one)((s: String) => s.length)
assert(char.contains("a"))
assert(!char.contains("ab"))
}