Skip to content
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

Disallow private opaque type aliases #14666

Merged
merged 6 commits into from
Mar 14, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should point out that private[OuterScope] is allowed however.
Another thing that might not be very clear here is that one can define an abstract type and then override (or implement) it with an opaque type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that could be elsewhere, maybe in a tutorial. The doc here is a spec and conciseness is good. If we say however it can be private[X] the we'd have to go to all other places where we say private and include or exclude private X.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's consistent across the docs that private[Scope] is not private then it's OK


## 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