Skip to content

Commit 20b6c03

Browse files
committed
wip: undefOr2ops import, more complete erasure
1 parent e21623f commit 20b6c03

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import collection.mutable
1111
import Denotations.SingleDenotation
1212
import util.{SimpleIdentityMap, SourceFile, NoSource}
1313
import typer.ImportInfo.RootRef
14+
import ast.untpd
15+
import backend.sjs.JSDefinitions
1416

1517
import scala.annotation.tailrec
1618

@@ -1342,16 +1344,30 @@ class Definitions {
13421344
RootRef(() => ScalaPackageVal.termRef)
13431345

13441346
private val PredefImportFns: RootRef =
1345-
RootRef(() => ScalaPredefModule.termRef, isPredef=true)
1347+
RootRef(() => ScalaPredefModule.termRef,
1348+
selectors =
1349+
// do not import any2stringadd
1350+
untpd.ImportSelector(untpd.Ident(nme.any2stringadd), untpd.Ident(nme.WILDCARD)) ::
1351+
RootRef.defaultSelectors)
1352+
1353+
@tu private lazy val ScalaJsRootImportFn: RootRef =
1354+
RootRef(() => JSDefinitions.jsdefn.PseudoUnionModuleRef,
1355+
// import only undefOr2ops
1356+
selectors = List(untpd.ImportSelector(untpd.Ident(nme.undefOr2ops))))
13461357

13471358
@tu private lazy val JavaRootImportFns: List[RootRef] =
13481359
if ctx.settings.YnoImports.value then Nil
13491360
else JavaImportFns
13501361

13511362
@tu private lazy val ScalaRootImportFns: List[RootRef] =
1352-
if ctx.settings.YnoImports.value then Nil
1353-
else if ctx.settings.YnoPredef.value then ScalaImportFns
1354-
else ScalaImportFns :+ PredefImportFns
1363+
if ctx.settings.YnoImports.value then
1364+
Nil
1365+
else if ctx.settings.YnoPredef.value then
1366+
ScalaImportFns
1367+
else if !ctx.settings.scalajs.value then
1368+
ScalaImportFns :+ PredefImportFns
1369+
else
1370+
ScalaImportFns :+ PredefImportFns :+ ScalaJsRootImportFn
13551371

13561372
@tu private lazy val JavaRootImportTypes: List[TermRef] = JavaRootImportFns.map(_.refFn())
13571373
@tu private lazy val ScalaRootImportTypes: List[TermRef] = ScalaRootImportFns.map(_.refFn())

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,7 @@ object StdNames {
614614
val unapply: N = "unapply"
615615
val unapplySeq: N = "unapplySeq"
616616
val unbox: N = "unbox"
617+
val undefOr2ops: N = "undefOr2ops"
617618
val universe: N = "universe"
618619
val unsafeNulls: N = "unsafeNulls"
619620
val update: N = "update"

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,10 @@ class TypeErasure(sourceLanguage: SourceLanguage, semiEraseVCs: Boolean, isConst
593593
val tp1 =
594594
if sourceLanguage.isScala2 && ctx.settings.scalajs.value then
595595
val eraseAsPseudoUnion = new TypeMap:
596-
def apply(tp: Type) = tp match
596+
// TODO: This isn't good enough since an OrType might be
597+
// hidden behind a bound for example; we might have no
598+
// choice but to move this logic to the TypeErasure class.
599+
def apply(tp: Type) = tp.dealias match
597600
case tp: OrType =>
598601
JSDefinitions.jsdefn.PseudoUnionType
599602
case tp =>

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ import Decorators._
1717

1818
object ImportInfo {
1919

20-
case class RootRef(refFn: () => TermRef, isPredef: Boolean = false)
21-
22-
/** The import info for a root import */
23-
def rootImport(ref: RootRef)(using Context): ImportInfo =
24-
var selectors =
20+
case class RootRef(refFn: () => TermRef, selectors: Context ?=> List[untpd.ImportSelector] = Nil)
21+
object RootRef:
22+
def defaultSelectors(using Context): List[untpd.ImportSelector] =
2523
untpd.ImportSelector(untpd.Ident(nme.WILDCARD)) // import all normal members...
2624
:: untpd.ImportSelector(untpd.Ident(nme.EMPTY)) // ... and also all given members
2725
:: Nil
28-
if ref.isPredef then // do not import any2stringadd
29-
selectors = untpd.ImportSelector(untpd.Ident(nme.any2stringadd), untpd.Ident(nme.WILDCARD))
30-
:: selectors
26+
def apply(refFn: () => TermRef)(using Context): RootRef =
27+
RootRef(refFn, defaultSelectors)
3128

29+
/** The import info for a root import */
30+
def rootImport(ref: RootRef)(using Context): ImportInfo =
31+
val selectors = ref.selectors
3232
def sym(using Context) =
3333
val expr = tpd.Ident(ref.refFn()) // refFn must be called in the context of ImportInfo.sym
3434
tpd.Import(expr, selectors).symbol

0 commit comments

Comments
 (0)