Skip to content

Commit 069a9eb

Browse files
committed
Allow transparent classes
1 parent d8ca95c commit 069a9eb

File tree

5 files changed

+21
-5
lines changed

5 files changed

+21
-5
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ object Flags {
357357
val (_, DefaultMethod @ _, _) = newFlags(38, "<defaultmethod>")
358358

359359
/** Symbol is a transparent inline method or trait */
360-
val (Transparent @ _, _, _) = newFlags(39, "transparent")
360+
val (Transparent @ _, _, TransparentType @ _) = newFlags(39, "transparent")
361361

362362
/** Symbol is an enum class or enum case (if used with case) */
363363
val (Enum @ _, EnumVal @ _, _) = newFlags(40, "enum")
@@ -606,5 +606,4 @@ object Flags {
606606
val SyntheticParam: FlagSet = Synthetic | Param
607607
val SyntheticTermParam: FlagSet = Synthetic | TermParam
608608
val SyntheticTypeParam: FlagSet = Synthetic | TypeParam
609-
val TransparentTrait: FlagSet = Trait | Transparent
610609
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1145,7 +1145,7 @@ object SymDenotations {
11451145
isOneOf(FinalOrSealed) || isClass && !isOneOf(EffectivelyOpenFlags)
11461146

11471147
final def isTransparentTrait(using Context): Boolean =
1148-
isAllOf(TransparentTrait)
1148+
is(TransparentType)
11491149
|| defn.assumedTransparentTraits.contains(symbol)
11501150
|| isClass && hasAnnotation(defn.TransparentTraitAnnot)
11511151

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ object Checking {
494494
}
495495
if sym.is(Transparent) then
496496
if sym.isType then
497-
if !sym.is(Trait) then fail(em"`transparent` can only be used for traits")
497+
if !sym.isExtensibleClass then fail(em"`transparent` can only be used for extensible classes and traits")
498498
else
499499
if !sym.isInlineMethod then fail(em"`transparent` can only be used for inline methods")
500500
if (!sym.isClass && sym.is(Abstract))

tests/neg/transparent.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
transparent def foo = 1 // error
22
transparent inline def bar = 2 // ok
33
transparent inline val x = 2 // error
4-
transparent class c // error
4+
transparent class c // ok
5+
transparent final class d // error
56
transparent object y // error
67
transparent trait t // ok
78
transparent type T = c // error

tests/pos/unions.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,20 @@ object Test:
44
val x = if ??? then "" else 1
55
val _: String | Int = x
66

7+
object Test2:
8+
transparent class A
9+
class B extends A
10+
class C extends A
11+
val x = if ??? then B() else C()
12+
val _: B | C = x
13+
14+
object Test3:
15+
class A
16+
class B extends A
17+
class C extends A
18+
val x = if ??? then B() else C()
19+
val _: A = x
20+
21+
22+
723

0 commit comments

Comments
 (0)