Skip to content

Commit 715b9a6

Browse files
committed
Allow transparent classes
1 parent f19de96 commit 715b9a6

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")
@@ -609,5 +609,4 @@ object Flags {
609609
val SyntheticParam: FlagSet = Synthetic | Param
610610
val SyntheticTermParam: FlagSet = Synthetic | TermParam
611611
val SyntheticTypeParam: FlagSet = Synthetic | TypeParam
612-
val TransparentTrait: FlagSet = Trait | Transparent
613612
}

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

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

11541154
final def isTransparentTrait(using Context): Boolean =
1155-
isAllOf(TransparentTrait)
1155+
is(TransparentType)
11561156
|| defn.assumedTransparentTraits.contains(symbol)
11571157
|| isClass && hasAnnotation(defn.TransparentTraitAnnot)
11581158

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ object Checking {
495495
}
496496
if sym.is(Transparent) then
497497
if sym.isType then
498-
if !sym.is(Trait) then fail(em"`transparent` can only be used for traits".toMessage)
498+
if !sym.isExtensibleClass then fail(em"`transparent` can only be used for extensible classes and traits".toMessage)
499499
else
500500
if !sym.isInlineMethod then fail(em"`transparent` can only be used for inline methods".toMessage)
501501
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)