Skip to content

Commit fe525f3

Browse files
committed
Desugar using clauses for Singleton context bounds to be erased
Drop the previous convention that Singleton is an erased class under x.modularity. That does not work anynmore if we change to the Erased trait scheme.
1 parent 9b31664 commit fe525f3

File tree

5 files changed

+11
-17
lines changed

5 files changed

+11
-17
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ class Definitions {
543543
// needed as a synthetic class because Scala 2.x refers to it in classfiles
544544
// but does not define it as an explicit class.
545545
val cls = enterCompleteClassSymbol(
546-
ScalaPackageClass, tpnme.Singleton, PureInterfaceCreationFlags | Final | Erased,
546+
ScalaPackageClass, tpnme.Singleton, PureInterfaceCreationFlags | Final,
547547
List(AnyType))
548548
enterTypeField(cls, tpnme.Self, Deferred, cls.info.decls.openForMutations)
549549
cls

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ class TypeUtils:
2626
def isErasedClass(using Context): Boolean =
2727
val cls = self.underlyingClassRef(refinementOK = true).typeSymbol
2828
cls.is(Flags.Erased)
29-
&& (cls != defn.SingletonClass || Feature.enabled(Feature.modularity))
30-
// Singleton counts as an erased class only under x.modularity
3129

3230

3331
/** Is this type a checked exception? This is the case if the type

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,6 +1908,11 @@ class Namer { typer: Typer =>
19081908
case _ =>
19091909

19101910
val mbrTpe = paramFn(checkSimpleKinded(typedAheadType(mdef.tpt, tptProto)).tpe)
1911+
// Add an erased to the using clause generated from a `: Singleton` context bound
1912+
mdef.tpt match
1913+
case tpt: untpd.ContextBoundTypeTree if mbrTpe.typeSymbol == defn.SingletonClass =>
1914+
sym.setFlag(Erased)
1915+
case _ =>
19111916
if (ctx.explicitNulls && mdef.mods.is(JavaDefined))
19121917
JavaNullInterop.nullifyMember(sym, mbrTpe, mdef.mods.isAllOf(JavaEnumValue))
19131918
else mbrTpe

tests/new/test.scala

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
import java.io.IOException
21

3-
class CanThrow[-E <: Exception]
42

5-
def foo[E <: Exception](e: E)(using erased CanThrow[E]): Nothing = throw e
6-
7-
erased def magic[E]: E = magic // error
8-
inline def moreMagic[E]: E = moreMagic
9-
10-
def Test =
11-
foo(new IOException)(using magic)
12-
foo(new IOException)(using moreMagic) // should be error
3+
def foo[T: Singleton](x: T) = x

tests/pos/singleton-ctx-bound.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//> using options -language:experimental.modularity -source future
1+
//> using options -language:experimental.modularity -source future -language:experimental.erasedDefinitions
22
object Test:
33

44
class Wrap[T](x: T)
@@ -11,15 +11,15 @@ object Test:
1111
val x1 = f1(1)
1212
val _: Wrap[1] = x1
1313

14-
def f2[T](x: T)(using Singleton { type Self = T}): Wrap[T] = Wrap(x)
14+
def f2[T](x: T)(using erased Singleton { type Self = T}): Wrap[T] = Wrap(x)
1515
val x2 = f2(1)
1616
val _: Wrap[1] = x2
1717

1818
def f3[T: Singleton](x: T): Wrap[T] = Wrap(x)
1919
val x3 = f3(1)
2020
val _: Wrap[1] = x3
2121

22-
def f4[T](x: T)(using T is Singleton): Wrap[T] = Wrap(x)
22+
def f4[T](x: T)(using erased T is Singleton): Wrap[T] = Wrap(x)
2323
val x4 = f4(1)
2424
val _: Wrap[1] = x4
2525

@@ -33,7 +33,7 @@ object Test:
3333
val y1 = C1("hi")
3434
val _: "hi" = y1.fld
3535

36-
class C2[T](x: T)(using T is Singleton):
36+
class C2[T](x: T)(using erased T is Singleton):
3737
def fld: T = x
3838
val y2 = C2("hi")
3939
val _: "hi" = y2.fld

0 commit comments

Comments
 (0)