Skip to content

Commit 444ec0b

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 16c24c9 commit 444ec0b

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
@@ -1906,6 +1906,11 @@ class Namer { typer: Typer =>
19061906
case _ =>
19071907

19081908
val mbrTpe = paramFn(checkSimpleKinded(typedAheadType(mdef.tpt, tptProto)).tpe)
1909+
// Add an erased to the using clause generated from a `: Singleton` context bound
1910+
mdef.tpt match
1911+
case tpt: untpd.ContextBoundTypeTree if mbrTpe.typeSymbol == defn.SingletonClass =>
1912+
sym.setFlag(Erased)
1913+
case _ =>
19091914
if (ctx.explicitNulls && mdef.mods.is(JavaDefined))
19101915
JavaNullInterop.nullifyMember(sym, mbrTpe, mdef.mods.isAllOf(JavaEnumValue))
19111916
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)