Skip to content

Commit cb53940

Browse files
committed
Make compiler sources invariant under rewriting
1 parent 1e3a8d9 commit cb53940

File tree

188 files changed

+1928
-1992
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

188 files changed

+1928
-1992
lines changed

compiler/src/dotty/tools/dotc/Driver.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,15 @@ class Driver {
5959
MacroClassLoader.init(ctx)
6060
Positioned.updateDebugPos(ctx)
6161

62-
if (!ctx.settings.YdropComments.value(ctx) || ctx.mode.is(Mode.ReadComments)) {
62+
if (!ctx.settings.YdropComments.value(ctx) || ctx.mode.is(Mode.ReadComments))
6363
ctx.setProperty(ContextDoc, new ContextDocstrings)
64-
}
6564

6665
val fileNames = CompilerCommand.checkUsage(summary, sourcesRequired)(ctx)
6766
fromTastySetup(fileNames, ctx)
6867
}
6968

7069
/** Setup extra classpath and figure out class names for tasty file inputs */
71-
protected def fromTastySetup(fileNames0: List[String], ctx0: Context): (List[String], Context) = {
70+
protected def fromTastySetup(fileNames0: List[String], ctx0: Context): (List[String], Context) =
7271
if (ctx0.settings.fromTasty.value(ctx0)) {
7372
// Resolve classpath and class names of tasty files
7473
val (classPaths, classNames) = fileNames0.flatMap { name =>
@@ -98,8 +97,8 @@ class Driver {
9897
val fullClassPath = (classPaths1 :+ ctx1.settings.classpath.value(ctx1)).mkString(java.io.File.pathSeparator)
9998
ctx1.setSetting(ctx1.settings.classpath, fullClassPath)
10099
(classNames, ctx1)
101-
} else (fileNames0, ctx0)
102-
}
100+
}
101+
else (fileNames0, ctx0)
103102

104103
/** Entry point to the compiler that can be conveniently used with Java reflection.
105104
*

compiler/src/dotty/tools/dotc/Run.scala

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
102102
def compile(fileNames: List[String]): Unit = try {
103103
val sources = fileNames.map(ctx.getSource(_))
104104
compileSources(sources)
105-
} catch {
105+
}
106+
catch {
106107
case NonFatal(ex) =>
107108
ctx.echo(i"exception occurred while compiling $units%, %")
108109
throw ex
@@ -114,11 +115,12 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
114115
* or we need to assemble phases on each run, and take -Yskip, -Ystop into
115116
* account. I think the latter would be preferable.
116117
*/
117-
def compileSources(sources: List[SourceFile]): Unit =
118+
def compileSources(sources: List[SourceFile]): Unit = {
118119
if (sources forall (_.exists)) {
119120
units = sources.map(CompilationUnit(_))
120121
compileUnits()
121122
}
123+
}
122124

123125
def compileUnits(us: List[CompilationUnit]): Unit = {
124126
units = us
@@ -150,24 +152,23 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
150152
var lastPrintedTree: PrintedTree = NoPrintedTree
151153
val profiler = ctx.profiler
152154

153-
for (phase <- ctx.base.allPhases)
155+
for (phase <- ctx.base.allPhases) {
154156
if (phase.isRunnable)
155157
Stats.trackTime(s"$phase ms ") {
156158
val start = System.currentTimeMillis
157159
val profileBefore = profiler.beforePhase(phase)
158160
units = phase.runOn(units)
159161
profiler.afterPhase(phase, profileBefore)
160-
if (ctx.settings.Xprint.value.containsPhase(phase)) {
161-
for (unit <- units) {
162+
if (ctx.settings.Xprint.value.containsPhase(phase))
163+
for (unit <- units)
162164
lastPrintedTree =
163165
printTree(lastPrintedTree)(ctx.fresh.setPhase(phase.next).setCompilationUnit(unit))
164-
}
165-
}
166166
ctx.informTime(s"$phase ", start)
167167
Stats.record(s"total trees at end of $phase", ast.Trees.ntrees)
168168
for (unit <- units)
169169
Stats.record(s"retained typed trees at end of $phase", unit.tpdTree.treeSize)
170170
}
171+
}
171172

172173
profiler.finished()
173174
}
@@ -189,7 +190,7 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
189190
* If `typeCheck = true`, also run typer on the compilation unit, and set
190191
* `rootTreeOrProvider`.
191192
*/
192-
def lateCompile(file: AbstractFile, typeCheck: Boolean)(implicit ctx: Context): Unit =
193+
def lateCompile(file: AbstractFile, typeCheck: Boolean)(implicit ctx: Context): Unit = {
193194
if (!files.contains(file) && !lateFiles.contains(file)) {
194195
lateFiles += file
195196
val unit = CompilationUnit(ctx.getSource(file.path))
@@ -208,6 +209,7 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
208209
}
209210
process()(runContext.fresh.setCompilationUnit(unit))
210211
}
212+
}
211213

212214
private sealed trait PrintedTree
213215
private /*final*/ case class SomePrintedTree(phase: String, tree: String) extends PrintedTree

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -308,12 +308,12 @@ object desugar {
308308
* case '{ @patternBindHole def `$a`(...) = ...; ... `$a`() ... } => a
309309
* ```
310310
*/
311-
def transformQuotedPatternName(tree: ValOrDefDef)(implicit ctx: Context): ValOrDefDef = {
311+
def transformQuotedPatternName(tree: ValOrDefDef)(implicit ctx: Context): ValOrDefDef =
312312
if (ctx.mode.is(Mode.QuotedPattern) && !isBackquoted(tree) && tree.name != nme.ANON_FUN && tree.name.startsWith("$")) {
313313
val mods = tree.mods.withAddedAnnotation(New(ref(defn.InternalQuoted_patternBindHoleAnnot.typeRef)).withSpan(tree.span))
314314
tree.withMods(mods)
315-
} else tree
316-
}
315+
}
316+
else tree
317317

318318
/** Add an explicit ascription to the `expectedTpt` to every tail splice.
319319
*
@@ -926,7 +926,8 @@ object desugar {
926926
val patternBindHoleAnnot = New(ref(defn.InternalQuoted_patternBindHoleAnnot.typeRef)).withSpan(tree.span)
927927
val mods = tree.mods.withAddedAnnotation(patternBindHoleAnnot)
928928
tree.withMods(mods)
929-
} else tree
929+
}
930+
else tree
930931
}
931932

932933
/** The normalized name of `mdef`. This means
@@ -953,7 +954,8 @@ object desugar {
953954
impl.body.find {
954955
case dd: DefDef if dd.mods.is(Extension) => true
955956
case _ => false
956-
} match {
957+
}
958+
match {
957959
case Some(DefDef(name, _, (vparam :: _) :: _, _, _)) =>
958960
s"${name}_of_${inventTypeName(vparam.tpt)}"
959961
case _ =>
@@ -1007,7 +1009,7 @@ object desugar {
10071009
case id: Ident =>
10081010
expandSimpleEnumCase(id.name.asTermName, mods,
10091011
Span(id.span.start, id.span.end, id.span.start))
1010-
}
1012+
}
10111013
else {
10121014
val pats1 = if (tpt.isEmpty) pats else pats map (Typed(_, tpt))
10131015
pats1 map (makePatDef(pdef, mods, _, rhs))
@@ -1068,7 +1070,7 @@ object desugar {
10681070
if (tupleOptimizable) // include `_`
10691071
pat match {
10701072
case Tuple(pats) =>
1071-
pats.map { case id: Ident => id -> TypeTree() }
1073+
pats.map { case id: Ident => id -> TypeTree() }
10721074
}
10731075
else getVariables(pat) // no `_`
10741076

@@ -1127,11 +1129,12 @@ object desugar {
11271129
case tree: MemberDef =>
11281130
var tested: MemberDef = tree
11291131
def fail(msg: String) = ctx.error(msg, tree.sourcePos)
1130-
def checkApplicable(flag: Flag, test: MemberDefTest): Unit =
1132+
def checkApplicable(flag: Flag, test: MemberDefTest): Unit = {
11311133
if (tested.mods.is(flag) && !test.applyOrElse(tree, (md: MemberDef) => false)) {
11321134
fail(i"modifier `${flag.flagsString}` is not allowed for this definition")
11331135
tested = tested.withMods(tested.mods.withoutFlags(flag))
11341136
}
1137+
}
11351138
checkApplicable(Opaque, legalOpaque)
11361139
tested
11371140
case _ =>
@@ -1296,7 +1299,7 @@ object desugar {
12961299
if (isGenericTuple) Apply(Select(refOfDef(param), nme.apply), Literal(Constant(n)))
12971300
else Select(refOfDef(param), nme.selectorName(n))
12981301
val vdefs =
1299-
params.zipWithIndex.map{
1302+
params.zipWithIndex.map {
13001303
case (param, idx) =>
13011304
DefDef(param.name, Nil, Nil, TypeTree(), selector(idx)).withSpan(param.span)
13021305
}
@@ -1338,7 +1341,7 @@ object desugar {
13381341
.withSpan(original.span.withPoint(named.span.start))
13391342
val mayNeedSetter = valDef(vdef)
13401343
mayNeedSetter
1341-
}
1344+
}
13421345

13431346
private def derivedDefDef(original: Tree, named: NameTree, tpt: Tree, rhs: Tree, mods: Modifiers)(implicit src: SourceFile) =
13441347
DefDef(named.name.asTermName, Nil, Nil, tpt, rhs)
@@ -1551,7 +1554,8 @@ object desugar {
15511554
RefinedTypeTree(polyFunctionTpt, List(
15521555
DefDef(nme.apply, applyTParams, List(applyVParams), res, EmptyTree)
15531556
))
1554-
} else {
1557+
}
1558+
else {
15551559
// Desugar [T_1, ..., T_M] -> (x_1: P_1, ..., x_N: P_N) => body
15561560
// Into new scala.PolyFunction { def apply[T_1, ..., T_M](x_1: P_1, ..., x_N: P_N) = body }
15571561

@@ -1606,7 +1610,8 @@ object desugar {
16061610
Annotated(
16071611
AppliedTypeTree(ref(seqType), t),
16081612
New(ref(defn.RepeatedAnnot.typeRef), Nil :: Nil))
1609-
} else {
1613+
}
1614+
else {
16101615
assert(ctx.mode.isExpr || ctx.reporter.errorsReported || ctx.mode.is(Mode.Interactive), ctx.mode)
16111616
Select(t, op.name)
16121617
}
@@ -1692,8 +1697,9 @@ object desugar {
16921697
private def getVariables(tree: Tree)(implicit ctx: Context): List[VarInfo] = {
16931698
val buf = ListBuffer[VarInfo]()
16941699
def seenName(name: Name) = buf exists (_._1.name == name)
1695-
def add(named: NameTree, t: Tree): Unit =
1700+
def add(named: NameTree, t: Tree): Unit = {
16961701
if (!seenName(named.name) && named.name.isTermName) buf += ((named, t))
1702+
}
16971703
def collect(tree: Tree): Unit = tree match {
16981704
case Bind(nme.WILDCARD, tree1) =>
16991705
collect(tree1)

compiler/src/dotty/tools/dotc/ast/NavigateAST.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ object NavigateAST {
9797
case _ =>
9898
}
9999
childPath(p.productIterator, p :: path)
100-
} else path
100+
}
101+
else path
101102
singlePath(from, Nil)
102103
}
103104
}

compiler/src/dotty/tools/dotc/ast/Positioned.scala

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ abstract class Positioned(implicit @constructorOnly src: SourceFile) extends Pro
3838
/** The span part of the item's position */
3939
def span: Span = mySpan
4040

41-
def span_=(span: Span): Unit = {
41+
def span_=(span: Span): Unit =
4242
mySpan = span
43-
}
4443

4544
uniqueId = src.nextId
4645
span = envelope(src)
@@ -58,9 +57,8 @@ abstract class Positioned(implicit @constructorOnly src: SourceFile) extends Pro
5857
else {
5958
val newpd: this.type =
6059
if (mySpan.isSynthetic) {
61-
if (!mySpan.exists && span.exists) {
60+
if (!mySpan.exists && span.exists)
6261
envelope(source, span.startPos) // fill in children spans
63-
}
6462
this
6563
}
6664
else cloneIn(source)
@@ -109,12 +107,11 @@ abstract class Positioned(implicit @constructorOnly src: SourceFile) extends Pro
109107
else if (span1.start == MaxOffset)
110108
// No positioned child was found
111109
NoSpan
112-
else {
110+
else
113111
///println(s"revisit $uniqueId with $span1")
114112
// We have some children left whose span could not be assigned.
115113
// Go through it again with the known start position.
116114
includeChildren(span1.startPos, 0)
117-
}
118115
span2.toSynthetic
119116
}
120117

@@ -145,7 +142,7 @@ abstract class Positioned(implicit @constructorOnly src: SourceFile) extends Pro
145142
found = isParent(productElement(n))
146143
}
147144
found
148-
}
145+
}
149146
}
150147

151148
/** A hook that can be overridden if overlap checking in `checkPos` should be
@@ -232,7 +229,8 @@ abstract class Positioned(implicit @constructorOnly src: SourceFile) extends Pro
232229
n += 1
233230
}
234231
}
235-
} catch {
232+
}
233+
catch {
236234
case ex: AssertionError =>
237235
println(i"error while checking $this")
238236
throw ex
@@ -242,7 +240,6 @@ abstract class Positioned(implicit @constructorOnly src: SourceFile) extends Pro
242240
object Positioned {
243241
@sharable private[Positioned] var debugId = Int.MinValue
244242

245-
def updateDebugPos(implicit ctx: Context): Unit = {
243+
def updateDebugPos(implicit ctx: Context): Unit =
246244
debugId = ctx.settings.YdebugTreeWithId.value
247-
}
248245
}

compiler/src/dotty/tools/dotc/ast/TreeInfo.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
401401
|| fn.symbol.isPrimaryConstructor && fn.symbol.owner.isNoInitsClass) // TODO: include in isStable?
402402
minOf(exprPurity(fn), args.map(exprPurity)) `min` Pure
403403
else if (fn.symbol.is(Erased)) Pure
404-
else if (fn.symbol.isStableMember /* && fn.symbol.is(Lazy) */)
404+
else if (fn.symbol.isStableMember) /* && fn.symbol.is(Lazy) */
405405
minOf(exprPurity(fn), args.map(exprPurity)) `min` Idempotent
406406
else Impure
407407
case Typed(expr, _) =>

compiler/src/dotty/tools/dotc/ast/TreeMapWithImplicits.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class TreeMapWithImplicits extends tpd.TreeMap {
2828

2929
@tailrec def traverse(curStats: List[Tree])(implicit ctx: Context): List[Tree] = {
3030

31-
def recur(stats: List[Tree], changed: Tree, rest: List[Tree])(implicit ctx: Context): List[Tree] = {
31+
def recur(stats: List[Tree], changed: Tree, rest: List[Tree])(implicit ctx: Context): List[Tree] =
3232
if (stats eq curStats) {
3333
val rest1 = transformStats(rest, exprOwner)
3434
changed match {
@@ -37,7 +37,6 @@ class TreeMapWithImplicits extends tpd.TreeMap {
3737
}
3838
}
3939
else stats.head :: recur(stats.tail, changed, rest)
40-
}
4140

4241
curStats match {
4342
case stat :: rest =>
@@ -123,5 +122,5 @@ class TreeMapWithImplicits extends tpd.TreeMap {
123122
tree
124123
}
125124
}
126-
127125
}
126+

compiler/src/dotty/tools/dotc/ast/Trees.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ object Trees {
118118
* - the child tree is an identifier, or
119119
* - errors were reported
120120
*/
121-
private def checkChildrenTyped(it: Iterator[Any])(implicit ctx: Context): Unit =
121+
private def checkChildrenTyped(it: Iterator[Any])(implicit ctx: Context): Unit = {
122122
if (!this.isInstanceOf[Import[_]])
123123
while (it.hasNext)
124124
it.next() match {
@@ -129,6 +129,7 @@ object Trees {
129129
case xs: List[_] => checkChildrenTyped(xs.iterator)
130130
case _ =>
131131
}
132+
}
132133

133134
def withTypeUnchecked(tpe: Type): ThisTree[Type] = {
134135
val tree =
@@ -374,7 +375,7 @@ object Trees {
374375
if (idx >= 0) idx
375376
else point // use `point` anyway. This is important if no source exists so scanning fails
376377
}
377-
Span(point, point + realName.length, point)
378+
Span(point, point + realName.length, point)
378379
}
379380
}
380381
else span
@@ -421,14 +422,13 @@ object Trees {
421422
extends DenotingTree[T] with TermTree[T] {
422423
type ThisTree[-T >: Untyped] = This[T]
423424
// Denotation of a This tree is always the underlying class; needs correction for modules.
424-
override def denot(implicit ctx: Context): Denotation = {
425+
override def denot(implicit ctx: Context): Denotation =
425426
typeOpt match {
426427
case tpe @ TermRef(pre, _) if tpe.symbol.is(Module) =>
427428
tpe.symbol.moduleClass.denot.asSeenFrom(pre)
428429
case _ =>
429430
super.denot
430431
}
431-
}
432432
}
433433

434434
/** C.super[mix], where qual = C.this */

0 commit comments

Comments
 (0)