Skip to content

Fix #9735: Tighten opaque types check #9938

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 3 commits into from
Oct 6, 2020
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
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -888,8 +888,8 @@ class Namer { typer: Typer =>
val unsafeInfo = if (isDerived) rhsBodyType else abstracted(rhsBodyType)

def opaqueToBounds(info: Type): Type =
if sym.isOpaqueAlias && tparamSyms.isEmpty && info.typeParams.nonEmpty then
report.error(em"opaque type alias must be fully applied", rhs.srcPos)
if sym.isOpaqueAlias && info.typeParams.nonEmpty && info.hkResult.typeParams.nonEmpty then
report.error(em"opaque type alias cannot have multiple type parameter lists", rhs.srcPos)
sym.opaqueToBounds(info, rhs1, tparamSyms)

if (isDerived) sym.info = unsafeInfo
Expand Down
14 changes: 14 additions & 0 deletions docs/docs/reference/other-new-features/opaques-details.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ object o {
def id(x: o.T): o.T = x
```

### Type Parameters of Opaque Types

Opaque type aliases can have a single type parameter list. The following aliases
are well-formed
```scala
opaque type F[T] = (T, T)
opaque type G = [T] =>> List[T]
```
but the following are not:
```scala
opaque type BadF[T] = [U] =>> (T, U)
opaque type BadG = [T] =>> [U] => (T, U)
```

### Translation of Equality

Comparing two values of opaque type with `==` or `!=` normally uses universal equality,
Expand Down
12 changes: 12 additions & 0 deletions tests/neg/i9735.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
trait Two[A, B]

opaque type U[A] = [B] =>> Two[A, B] // error: opaque type alias cannot have multiple type parameter lists // error: cannot instantiate
opaque type T = [A] =>> [B] =>> String // error: opaque type alias cannot have multiple type parameter lists
opaque type S = [B] =>> String // ok
opaque type IArray[+T] = Array[? <: T] // ok
opaque type S2[B] = String // ok

opaque type BadF[T] = [U] =>> (T, U) // error
opaque type BadG = [T] =>> [U] =>> (T, U) // error