Skip to content

Commit

Permalink
Merge pull request #14666 from dotty-staging/fix-14660
Browse files Browse the repository at this point in the history
Disallow private opaque type aliases
  • Loading branch information
odersky committed Mar 14, 2022
2 parents b505b08 + fe6c299 commit 83fb072
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 4 deletions.
9 changes: 6 additions & 3 deletions compiler/src/dotty/tools/dotc/core/Denotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ object Denotations {
}
case denot1: SingleDenotation =>
if (denot1 eq denot2) denot1
else if (denot1.matches(denot2)) mergeSingleDenot(denot1, denot2)
else if denot1.matches(denot2) then mergeSingleDenot(denot1, denot2)
else NoDenotation
}

Expand Down Expand Up @@ -438,8 +438,11 @@ object Denotations {
else defn.RootClass)

def isHidden(sym: Symbol) = sym.exists && !sym.isAccessibleFrom(pre)
val hidden1 = isHidden(sym1)
val hidden2 = isHidden(sym2)
// In typer phase filter out denotations with symbols that are not
// accessible. After typer, this is not possible since we cannot guarantee
// that the current owner is set correctly. See pos/14660.scala.
val hidden1 = isHidden(sym1) && ctx.isTyper
val hidden2 = isHidden(sym2) && ctx.isTyper
if hidden1 && !hidden2 then denot2
else if hidden2 && !hidden1 then denot1
else
Expand Down
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ object Checking {
checkCombination(Private, Protected)
checkCombination(Abstract, Override)
checkCombination(Private, Override)
if sym.isType && !sym.isClass then checkCombination(Private, Opaque)
checkCombination(Lazy, Inline)
// The issue with `erased inline` is that the erased semantics get lost
// as the code is inlined and the reference is removed before the erased usage check.
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/scala/quoted/runtime/impl/QuoteMatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ object QuoteMatcher {
}

/** Result of matching a part of an expression */
private opaque type Matching = Option[Tuple]
private type Matching = Option[Tuple]

private object Matching {

Expand Down
2 changes: 2 additions & 0 deletions docs/_docs/reference/other-new-features/opaques-details.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ object o:
def id(x: o.T): o.T = x
```

Opaque type aliases cannot be `private` and cannot be overridden in subclasses.

## Type Parameters of Opaque Types

Opaque type aliases can have a single type parameter list. The following aliases
Expand Down
8 changes: 8 additions & 0 deletions tests/neg/i14660.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Bar:
private opaque type Baz = Int // error

private object Foo:
opaque type O = Int // OK

val x: Baz = 1

6 changes: 6 additions & 0 deletions tests/pos/i14660.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
trait Foo:
class Bar:
private[Foo] opaque type Baz = Int

def foo: Bar#Baz

0 comments on commit 83fb072

Please sign in to comment.