Skip to content

Commit fc39b6c

Browse files
committed
Fix #5455: Fix computation of companion object of opaque type
1 parent 3a3d5a3 commit fc39b6c

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

compiler/src/dotty/tools/dotc/core/SymDenotations.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ object SymDenotations {
985985
if (is(Module)) sourceModule
986986
else if (isOpaqueAlias)
987987
info match {
988-
case TypeAlias(TypeRef(TermRef(prefix, _), _)) => prefix.termSymbol
988+
case TypeAlias(TypeRef(prefix: TermRef, _)) => prefix.termSymbol
989989
}
990990
else registeredCompanion.sourceModule
991991

tests/pos/i5455.scala

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
object Library {
2+
3+
opaque type Nat = Int
4+
5+
object Nat {
6+
def apply(n: Int): Nat = {
7+
require(n >= 0)
8+
n
9+
}
10+
def times(x: Nat, y: Nat): Nat = x * y
11+
def toInt(n: Nat): Int = n
12+
13+
implicit class NatOps(val self: Nat) extends AnyVal {
14+
def *(other: Nat): Nat = self * other
15+
def toInt: Int = self.asInstanceOf
16+
}
17+
}
18+
}
19+
20+
object User extends App {
21+
import Library._
22+
23+
val x = Nat(3)
24+
val y = Nat(4)
25+
26+
val a = x * y // inferred type is Library.Nat.Nat
27+
val b = double1(x) // inferred type is Library.Nat
28+
val c = double2(x) // inferred type is Library.Nat.Nat
29+
30+
assert(a.toInt == 12) // works
31+
//assert(b.toInt == 6) // error: toInt is not a member of Library.Nat
32+
assert(c.toInt == 6) // works
33+
34+
def double0(n: Nat): Nat = n * Nat(2) // ok
35+
36+
def double3(n: Nat): Nat = Nat.NatOps(n) * Nat(2) // ok
37+
38+
39+
def double1(n: Nat.Nat): Nat = n * Nat(2) // output type is incorrect
40+
41+
def double2(n: Nat.Nat): Nat.Nat =n * Nat(2) // works
42+
}

0 commit comments

Comments
 (0)