Skip to content

Commit c76b81b

Browse files
Run (a part of) PostTyper on java sources
1 parent 853b0bf commit c76b81b

File tree

3 files changed

+69
-42
lines changed

3 files changed

+69
-42
lines changed

compiler/src/dotty/tools/dotc/transform/Pickler.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,12 @@ class Pickler extends Phase {
8787
}
8888
}
8989

90+
protected def discardBeforePickler(unit: CompilationUnit)(using Context): Boolean =
91+
unit.isJava // stop here for java files
92+
9093
override def runOn(units: List[CompilationUnit])(using Context): List[CompilationUnit] = {
91-
val result = super.runOn(units)
92-
if (ctx.settings.YtestPickler.value)
94+
val result = super.runOn(units.filterNot(discardBeforePickler))
95+
if ctx.settings.YtestPickler.value
9396
testUnpickler(
9497
using ctx.fresh
9598
.setPeriod(Period(ctx.runId + 1, FirstPhaseId))

compiler/src/dotty/tools/dotc/transform/PostTyper.scala

Lines changed: 61 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
7171
override def transformPhase(using Context): Phase = thisPhase.next
7272

7373
protected def newTransformer(using Context): Transformer =
74-
new PostTyperTransformer
74+
if ctx.compilationUnit.isJava then LimitedChecksTransformer()
75+
else PostTyperTransformer()
7576

7677
val superAcc: SuperAccessors = new SuperAccessors(thisPhase)
7778
val synthMbr: SyntheticMembers = new SyntheticMembers(thisPhase)
@@ -85,6 +86,65 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
8586
// TODO fill in
8687
}
8788

89+
private def normalizeTypeArgs(tree: TypeApply)(using Context): TypeApply = tree.tpe match
90+
case pt: PolyType => // wait for more arguments coming
91+
tree
92+
case _ =>
93+
def decompose(tree: TypeApply): (Tree, List[Tree]) = tree.fun match
94+
case fun: TypeApply =>
95+
val (tycon, args) = decompose(fun)
96+
(tycon, args ++ tree.args)
97+
case _ =>
98+
(tree.fun, tree.args)
99+
end decompose
100+
def reorderArgs(pnames: List[Name], namedArgs: List[NamedArg], otherArgs: List[Tree]): List[Tree] = pnames match
101+
case pname :: pnames1 =>
102+
namedArgs.partition(_.name == pname) match
103+
case (NamedArg(_, arg) :: _, namedArgs1) =>
104+
arg :: reorderArgs(pnames1, namedArgs1, otherArgs)
105+
case _ =>
106+
val otherArg :: otherArgs1 = otherArgs
107+
otherArg :: reorderArgs(pnames1, namedArgs, otherArgs1)
108+
case nil =>
109+
assert(namedArgs.isEmpty && otherArgs.isEmpty)
110+
Nil
111+
end reorderArgs
112+
113+
val (tycon, args) = decompose(tree)
114+
tycon.tpe.widen match
115+
case tp: PolyType if args.exists(isNamedArg) =>
116+
val (namedArgs, otherArgs) = args.partition(isNamedArg)
117+
val args1 = reorderArgs(tp.paramNames, namedArgs.asInstanceOf[List[NamedArg]], otherArgs)
118+
TypeApply(tycon, args1).withSpan(tree.span).withType(tree.tpe)
119+
case _ =>
120+
tree
121+
122+
class LimitedChecksTransformer extends Transformer {
123+
private def checkAppliedTypes(tree: Tree)(using Context): Unit = tree match
124+
case tpt: TypeTree =>
125+
Checking.checkAppliedTypesIn(tpt)
126+
case app: AppliedTypeTree =>
127+
Checking.checkAppliedType(app, EmptyTree)
128+
case _ =>
129+
130+
override def transform(tree: Tree)(using Context): Tree =
131+
ctx.debuglog(s"LimitedChecksTransformer.transform(${tree.show})")
132+
ctx.debuglog(s" -- $tree")
133+
tree match
134+
case Select(qual, name) if name.isTypeName =>
135+
Checking.checkRealizable(qual.tpe, qual.posd)
136+
if name.isTypeName then
137+
super.transform(tree)(using ctx.addMode(Mode.Type))
138+
case tree: TypeApply =>
139+
val TypeApply(fn, args) = normalizeTypeArgs(tree)
140+
args.foreach(checkAppliedTypes)
141+
case tree: ValOrDefDef =>
142+
checkAppliedTypes(tree.tpt)
143+
case _ =>
144+
super.transform(tree)
145+
tree
146+
}
147+
88148
class PostTyperTransformer extends Transformer {
89149

90150
private var inJavaAnnot: Boolean = false
@@ -180,41 +240,6 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
180240
}
181241
}
182242

183-
private def normalizeTypeArgs(tree: TypeApply)(using Context): TypeApply = tree.tpe match {
184-
case pt: PolyType => // wait for more arguments coming
185-
tree
186-
case _ =>
187-
def decompose(tree: TypeApply): (Tree, List[Tree]) = tree.fun match {
188-
case fun: TypeApply =>
189-
val (tycon, args) = decompose(fun)
190-
(tycon, args ++ tree.args)
191-
case _ =>
192-
(tree.fun, tree.args)
193-
}
194-
def reorderArgs(pnames: List[Name], namedArgs: List[NamedArg], otherArgs: List[Tree]): List[Tree] = pnames match {
195-
case pname :: pnames1 =>
196-
namedArgs.partition(_.name == pname) match {
197-
case (NamedArg(_, arg) :: _, namedArgs1) =>
198-
arg :: reorderArgs(pnames1, namedArgs1, otherArgs)
199-
case _ =>
200-
val otherArg :: otherArgs1 = otherArgs
201-
otherArg :: reorderArgs(pnames1, namedArgs, otherArgs1)
202-
}
203-
case nil =>
204-
assert(namedArgs.isEmpty && otherArgs.isEmpty)
205-
Nil
206-
}
207-
val (tycon, args) = decompose(tree)
208-
tycon.tpe.widen match {
209-
case tp: PolyType if args.exists(isNamedArg) =>
210-
val (namedArgs, otherArgs) = args.partition(isNamedArg)
211-
val args1 = reorderArgs(tp.paramNames, namedArgs.asInstanceOf[List[NamedArg]], otherArgs)
212-
TypeApply(tycon, args1).withSpan(tree.span).withType(tree.tpe)
213-
case _ =>
214-
tree
215-
}
216-
}
217-
218243
private object dropInlines extends TreeMap {
219244
override def transform(tree: Tree)(using Context): Tree = tree match {
220245
case Inlined(call, _, expansion) =>

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ class FrontEnd extends Phase {
9292
}
9393

9494
protected def discardAfterTyper(unit: CompilationUnit)(using Context): Boolean =
95-
unit.isJava || unit.suspended
95+
unit.suspended
9696

97-
override def runOn(units: List[CompilationUnit])(using Context): List[CompilationUnit] = {
97+
override def runOn(units: List[CompilationUnit])(using Context): List[CompilationUnit] =
9898
val unitContexts =
9999
for unit <- units yield
100100
ctx.inform(s"compiling ${unit.source}")
@@ -106,7 +106,7 @@ class FrontEnd extends Phase {
106106
enterSyms(using remaining.head)
107107
remaining = remaining.tail
108108

109-
if (firstXmlPos.exists && !defn.ScalaXmlPackageClass.exists)
109+
if firstXmlPos.exists && !defn.ScalaXmlPackageClass.exists then
110110
ctx.error("""To support XML literals, your project must depend on scala-xml.
111111
|See https://github.com/scala/scala-xml for more information.""".stripMargin,
112112
firstXmlPos)
@@ -130,7 +130,6 @@ class FrontEnd extends Phase {
130130
|
131131
|To fix this, place macros in one set of files and their callers in another.$enableXprintSuspensionHint""")
132132
newUnits
133-
}
134133

135134
def run(using Context): Unit = unsupported("run")
136135
}

0 commit comments

Comments
 (0)