Skip to content

Fix #5455: Fix computation of companion object of opaque type #5480

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 2 commits into from
Nov 21, 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
14 changes: 11 additions & 3 deletions compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Symbols._, StdNames._, Trees._
import Decorators._, transform.SymUtils._
import NameKinds.{UniqueName, EvidenceParamName, DefaultGetterName}
import typer.FrontEnd
import util.Property
import collection.mutable.ListBuffer
import reporting.diagnostic.messages._
import reporting.trace
Expand All @@ -18,6 +19,11 @@ object desugar {
import untpd._
import DesugarEnums._

/** If a Select node carries this attachment, suppress the check
* that its type refers to an acessible symbol.
*/
val SuppressAccessCheck = new Property.Key[Unit]

/** Info of a variable in a pattern: The named tree and its type */
private type VarInfo = (NameTree, Tree)

Expand Down Expand Up @@ -727,9 +733,11 @@ object desugar {
fwd
}
val moduleName = tdef.name.toTermName
val aliasType = cpy.TypeDef(tdef)(
rhs = completeForwarder(Select(Ident(moduleName), tdef.name)))
val localType = tdef.withFlags(Synthetic | Opaque)
val localRef = Select(Ident(moduleName), tdef.name)
localRef.pushAttachment(SuppressAccessCheck, ())
val aliasType = cpy.TypeDef(tdef)(rhs = completeForwarder(localRef))
val localType = tdef.withMods(Modifiers(Synthetic | Opaque).withPrivateWithin(tdef.name))

val companions = moduleDef(ModuleDef(
moduleName, Template(emptyConstructor, Nil, EmptyValDef, localType :: Nil))
.withFlags(Synthetic | Opaque))
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/SymDenotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ object SymDenotations {
if (is(Module)) sourceModule
else if (isOpaqueAlias)
info match {
case TypeAlias(TypeRef(TermRef(prefix, _), _)) => prefix.termSymbol
case TypeAlias(TypeRef(prefix: TermRef, _)) => prefix.termSymbol
}
else registeredCompanion.sourceModule

Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ trait TypeAssigner {
qualType = errorType(em"$qualType takes type parameters", qual1.pos)
else if (!qualType.isInstanceOf[TermType]) qualType = errorType(em"$qualType is illegal as a selection prefix", qual1.pos)
val ownType = selectionType(qualType, tree.name, tree.pos)
ensureAccessible(ownType, qual1.isInstanceOf[Super], tree.pos)
if (tree.getAttachment(desugar.SuppressAccessCheck).isDefined) ownType
else ensureAccessible(ownType, qual1.isInstanceOf[Super], tree.pos)
}

/** Type assignment method. Each method takes as parameters
Expand Down
35 changes: 35 additions & 0 deletions tests/neg/i5455.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
object Library {

opaque type Nat = Int

object Nat {
def apply(n: Int): Nat = {
require(n >= 0)
n
}
def times(x: Nat, y: Nat): Nat = x * y
def toInt(n: Nat): Int = n

implicit class NatOps(val self: Nat) extends AnyVal {
def *(other: Nat): Nat = self * other
def toInt: Int = self.asInstanceOf
}
}
}

object User extends App {
import Library._

val x = Nat(3)
val y = Nat(4)

val a = x * y
val b = double1(x)
val c = double2(x)

def double0(n: Nat): Nat = n * Nat(2) // ok

def double1(n: Nat.Nat): Nat = n * Nat(2) // error

def double2(n: Nat.Nat): Nat.Nat = n * Nat(2) // error // error
}
36 changes: 36 additions & 0 deletions tests/run/i5455.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
object Library {

opaque type Nat = Int

object Nat {
def apply(n: Int): Nat = {
require(n >= 0)
n
}
def times(x: Nat, y: Nat): Nat = x * y
def toInt(n: Nat): Int = n

implicit class NatOps(val self: Nat) extends AnyVal {
def *(other: Nat): Nat = self * other
def toInt: Int = self.asInstanceOf
}
}
}

object Test extends App {
import Library._

val x = Nat(3)
val y = Nat(4)

val a = x * y
val b = double1(x)
val c = double2(x)

assert(a.toInt == 12)
assert(b.toInt == 6)
assert(c.toInt == 6)

def double1(n: Nat): Nat = n * Nat(2)
def double2(n: Nat): Nat = Nat.NatOps(n) * Nat(2)
}