Skip to content

Commit 9a545e5

Browse files
committed
Adapt Namer.checkFlags to type defdefs
Converted flags to termflags before.
1 parent 96c1d22 commit 9a545e5

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

compiler/src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,14 @@ class Namer { typer: Typer =>
284284
def checkFlags(flags: FlagSet) =
285285
if (flags.isEmpty) flags
286286
else {
287-
val (ok, adapted, kind) = tree match {
288-
case tree: TypeDef => (flags.isTypeFlags, flags.toTypeFlags, "type")
289-
case _ => (flags.isTermFlags, flags.toTermFlags, "value")
287+
val isType = tree match {
288+
case tree: TypeDef => true
289+
case tree: DefDef => tree.name.isTypeName
290+
case _ => false
290291
}
292+
val (ok, adapted, kind) =
293+
if (isType) (flags.isTypeFlags, flags.toTypeFlags, "type")
294+
else (flags.isTermFlags, flags.toTermFlags, "value")
291295
if (!ok)
292296
ctx.error(i"modifier(s) `$flags' incompatible with $kind definition", tree.pos)
293297
adapted

tests/neg/typemethods.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
trait Nat {
2+
def toInt: Int
3+
}
4+
5+
case object Z extends Nat {
6+
transparent def toInt = 0
7+
}
8+
9+
case class S[N <: Nat](n: N) extends Nat {
10+
transparent def toInt = n.toInt + 1
11+
}
12+
13+
object Test extends App {
14+
15+
type ToNat(n: Int) <: Nat =
16+
if n == 0 then Z.type
17+
else S[ToNat(n - 1)]
18+
19+
type ToNat = Int // error: double definition
20+
}

tests/neg/typemethods2.scala

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
trait Nat {
2+
def toInt: Int
3+
}
4+
5+
case object Z extends Nat {
6+
transparent def toInt = 0
7+
}
8+
9+
case class S[N <: Nat](n: N) extends Nat {
10+
transparent def toInt = n.toInt + 1
11+
}
12+
13+
class C {
14+
type ToNat = Int
15+
16+
type ToNat2(n: Int) <: Nat =
17+
if n == 0 then Z.type
18+
else S[ToNat2(n - 1)]}
19+
20+
object Test extends C {
21+
22+
override type ToNat(n: Int) <: Nat = // error: illegal override
23+
if n == 0 then Z.type
24+
else S[ToNat(n - 1)]
25+
26+
override type ToNat2[X] = X // error: illegal override
27+
28+
}

0 commit comments

Comments
 (0)