Skip to content

Commit 881f1a8

Browse files
committed
Fix latest unit tests from Zinc
1 parent 5247afa commit 881f1a8

File tree

3 files changed

+56
-70
lines changed

3 files changed

+56
-70
lines changed

compiler/src/dotty/tools/dotc/sbt/ExtractAPI.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,20 @@ class ExtractAPI extends Phase {
6262
ctx.sbtCallback.startSource(sourceFile.file)
6363

6464
val apiTraverser = new ExtractAPICollector
65-
val sources = apiTraverser.apiSource(unit.tpdTree)
65+
val classes = apiTraverser.apiSource(unit.tpdTree)
6666
val mainClasses = apiTraverser.mainClasses
6767

6868
if (dumpInc) {
6969
// Append to existing file that should have been created by ExtractDependencies
7070
val pw = new PrintWriter(File(sourceFile.jpath).changeExtension("inc").toFile
7171
.bufferedWriter(append = true), true)
7272
try {
73-
sources.foreach(source => pw.println(DefaultShowAPI(source)))
73+
classes.foreach(source => pw.println(DefaultShowAPI(source)))
7474
} finally pw.close()
7575
}
7676

7777
if (ctx.sbtCallback != null) {
78-
sources.foreach(ctx.sbtCallback.api(sourceFile.file, _))
78+
classes.foreach(ctx.sbtCallback.api(sourceFile.file, _))
7979
mainClasses.foreach(ctx.sbtCallback.mainClass(sourceFile.file, _))
8080
}
8181
}

compiler/src/dotty/tools/dotc/sbt/ExtractDependencies.scala

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ private class ExtractDependenciesCollector extends tpd.TreeTraverser { thisTreeT
278278

279279
/** Mangle a JVM symbol name in a format better suited for internal uses by sbt. */
280280
private def mangledName(sym: Symbol)(implicit ctx: Context): Name = {
281-
def constructorName = sym.enclosingClass.fullName ++ ";init;"
281+
def constructorName = sym.owner.fullName ++ ";init;"
282282

283283
if (sym.isConstructor) constructorName
284284
else sym.name.stripModuleClassSuffix
@@ -297,7 +297,7 @@ private class ExtractDependenciesCollector extends tpd.TreeTraverser { thisTreeT
297297
}
298298
}
299299

300-
private def addInheritanceDependency(tree: Template)(implicit ctx: Context): Unit =
300+
private def addInheritanceDependencies(tree: Template)(implicit ctx: Context): Unit =
301301
if (tree.parents.nonEmpty) {
302302
val depContext =
303303
if (tree.symbol.owner.isLocal) LocalDependencyByInheritance
@@ -310,9 +310,10 @@ private class ExtractDependenciesCollector extends tpd.TreeTraverser { thisTreeT
310310

311311
private def ignoreDependency(sym: Symbol)(implicit ctx: Context) =
312312
!sym.exists ||
313+
sym.is(PackageClass) ||
314+
sym.isEffectiveRoot ||
313315
sym.isAnonymousFunction ||
314-
sym.isAnonymousClass ||
315-
sym.is(PackageClass)
316+
sym.isAnonymousClass
316317

317318
/** Traverse the tree of a source file and record the dependencies and used names which
318319
* can be retrieved using `dependencies` and`usedNames`.
@@ -328,7 +329,7 @@ private class ExtractDependenciesCollector extends tpd.TreeTraverser { thisTreeT
328329
addMemberRefDependency(lookupImported(name.toTermName))
329330
addMemberRefDependency(lookupImported(name.toTypeName))
330331
}
331-
selectors foreach {
332+
selectors.foreach {
332333
case Ident(name) =>
333334
addImported(name)
334335
case Thicket(Ident(name) :: Ident(rename) :: Nil) =>
@@ -338,20 +339,31 @@ private class ExtractDependenciesCollector extends tpd.TreeTraverser { thisTreeT
338339
}
339340
case _ =>
340341
}
341-
case Inlined(call, _, _) =>
342-
// The inlined call is normally ignored by TreeTraverser but we need to
343-
// record it as a dependency
344-
traverse(call)
345342
case t: TypeTree =>
346343
addTypeDependency(t.tpe)
347344
case ref: RefTree =>
348345
addMemberRefDependency(ref.symbol)
349346
addTypeDependency(ref.tpe)
350347
case t: Template =>
351-
addInheritanceDependency(t)
348+
addInheritanceDependencies(t)
349+
case _ =>
350+
}
351+
352+
tree match {
353+
case Inlined(call, _, _) =>
354+
// The inlined call is normally ignored by TreeTraverser but we need to
355+
// record it as a dependency
356+
traverse(call)
357+
case vd: ValDef if vd.symbol.is(ModuleVal) =>
358+
// Don't visit module val
359+
case t: Template if t.symbol.owner.is(ModuleClass) =>
360+
// Don't visit self type of module class
361+
traverse(t.constr)
362+
t.parents.foreach(traverse)
363+
t.body.foreach(traverse)
352364
case _ =>
365+
traverseChildren(tree)
353366
}
354-
traverseChildren(tree)
355367
}
356368

357369
/** Traverse a used type and record all the dependencies we need to keep track

sbt-bridge/test/xsbt/ExtractUsedNamesSpecification.scala

Lines changed: 30 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class ExtractUsedNamesSpecification {
4343
|}""".stripMargin
4444
val compilerForTesting = new ScalaCompilerForUnitTesting
4545
val usedNames = compilerForTesting.extractUsedNamesFromSrc(srcA, srcB)
46-
val expectedNames = standardNames ++ Set("a", "c", "A", "B", "C", "D", "b", "X", "BB")
46+
val expectedNames = standardNames ++ Set("a", "c", "A", "B", "C", "D", "b", "BB")
4747
assertEquals(usedNames("b.X"), expectedNames)
4848
}
4949

@@ -58,7 +58,7 @@ class ExtractUsedNamesSpecification {
5858
|}""".stripMargin
5959
val compilerForTesting = new ScalaCompilerForUnitTesting
6060
val usedNames = compilerForTesting.extractUsedNamesFromSrc(srcA, srcB)
61-
val expectedNames = standardNames ++ Set("A", "a", "B", "=", "Int")
61+
val expectedNames = standardNames ++ Set("A", "a", "=", "Int")
6262
assertEquals(usedNames("B"), expectedNames)
6363
}
6464

@@ -78,15 +78,11 @@ class ExtractUsedNamesSpecification {
7878
val compilerForTesting = new ScalaCompilerForUnitTesting
7979
val usedNames = compilerForTesting.extractUsedNamesFromSrc(srcA, srcB, srcC, srcD)
8080
val scalaVersion = scala.util.Properties.versionNumberString
81-
// TODO: Find out what's making these types appear in 2.10
82-
// They don't come from type dependency traverser, but from `addSymbol`
83-
val versionDependentNames =
84-
if (scalaVersion.contains("2.10")) Set("Nothing", "Any") else Set()
85-
val namesA = standardNames ++ Set("A") ++ versionDependentNames
86-
val namesAX = standardNames ++ Set("X", "x", "T", "A")
87-
val namesB = Set("B", "A", "Int", "A;init;", "scala")
88-
val namesC = Set("B;init;", "C", "B")
89-
val namesD = standardNames ++ Set("D", "C", "X", "foo", "Int", "T")
81+
val namesA = standardNames ++ Set("Nothing", "Any")
82+
val namesAX = standardNames ++ Set("x", "T", "A", "Nothing", "Any")
83+
val namesB = Set("A", "Int", "A;init;", "Unit")
84+
val namesC = Set("B;init;", "B", "Unit")
85+
val namesD = standardNames ++ Set("C", "X", "foo", "Int", "T")
9086
assertEquals(usedNames("A"), namesA)
9187
assertEquals(usedNames("A.X"), namesAX)
9288
assertEquals(usedNames("B"), namesB)
@@ -134,23 +130,18 @@ class ExtractUsedNamesSpecification {
134130
|""".stripMargin
135131
val compilerForTesting = new ScalaCompilerForUnitTesting
136132
val usedNames = compilerForTesting.extractUsedNamesFromSrc(src1, src2)
137-
val expectedNames_lista = standardNames ++ Set("Test_lista", "x", "B", "lista", "List", "A")
138-
val expectedNames_at = standardNames ++ Set("Test_at", "x", "B", "at", "A", "T", "X0", "X1")
139-
val expectedNames_as = standardNames ++ Set("Test_as", "x", "B", "as", "S", "Y")
140-
val expectedNames_foo = standardNames ++ Set("Test_foo",
141-
"x",
142-
"B",
133+
val expectedNames_lista = standardNames ++ Set("B", "lista", "List", "A")
134+
val expectedNames_at = standardNames ++ Set("B", "at", "A", "T", "X0", "X1")
135+
val expectedNames_as = standardNames ++ Set("B", "as", "S", "Y")
136+
val expectedNames_foo = standardNames ++ Set("B",
143137
"foo",
144138
"M",
145139
"N",
146140
"Predef",
147141
"???",
148142
"Nothing")
149-
val expectedNames_bar = standardNames ++ Set("Test_bar",
150-
"x",
151-
"B",
143+
val expectedNames_bar = standardNames ++ Set("B",
152144
"bar",
153-
"Param",
154145
"P1",
155146
"P0",
156147
"Predef",
@@ -163,37 +154,20 @@ class ExtractUsedNamesSpecification {
163154
assertEquals(usedNames("Test_bar"), expectedNames_bar)
164155
}
165156

166-
@Test
167-
def extractUsedNamesFromExistential = {
168-
val srcFoo =
169-
"""import scala.language.existentials
170-
|class Foo {
171-
| val foo: T forSome { type T <: Double } = ???
172-
|}
173-
""".stripMargin
174-
val compilerForTesting = new ScalaCompilerForUnitTesting
175-
val usedNames = compilerForTesting.extractUsedNamesFromSrc(srcFoo)
176-
val expectedNames = standardNames ++ Seq("Double",
177-
"Foo",
178-
"T",
179-
"foo",
180-
"scala",
181-
"language",
182-
"existentials",
183-
"Nothing",
184-
"???",
185-
"Predef")
186-
assertEquals(usedNames("Foo"), expectedNames)
187-
}
188-
189157
@Test
190158
def extractUsedNamesFromRefinement = {
191-
val srcFoo =
192-
"object Outer {\n class Inner { type Xyz }\n\n type TypeInner = Inner { type Xyz = Int }\n}"
193-
val srcBar = "object Bar {\n def bar: Outer.TypeInner = null\n}"
159+
val srcFoo = """|object Outer {
160+
| class Inner { type Xyz }
161+
| type TypeInner = Inner { type Xyz = Int }
162+
|}
163+
|""".stripMargin
164+
val srcBar = """|object Bar {
165+
| def bar: Outer.TypeInner = null
166+
|}
167+
|""".stripMargin
194168
val compilerForTesting = new ScalaCompilerForUnitTesting
195169
val usedNames = compilerForTesting.extractUsedNamesFromSrc(srcFoo, srcBar)
196-
val expectedNames = standardNames ++ Set("Bar", "Outer", "TypeInner", "Inner", "Xyz", "Int")
170+
val expectedNames = standardNames ++ Set("Outer", "TypeInner", "Inner", "Int")
197171
assertEquals(usedNames("Bar"), expectedNames)
198172
}
199173

@@ -219,7 +193,7 @@ class ExtractUsedNamesSpecification {
219193

220194
// test for https://github.com/gkossakowski/sbt/issues/4
221195
// TODO: we should fix it by having special treatment of `selectDynamic` and `applyDynamic` calls
222-
@Test
196+
@Ignore("Call to Dynamic is desugared in type checker so Select nodes is turned into string literal.")
223197
def extractNamesFromMethodCallOnDynamic = {
224198
val srcA = """|import scala.language.dynamics
225199
|class A extends Dynamic {
@@ -228,7 +202,7 @@ class ExtractUsedNamesSpecification {
228202
val srcB = "class B { def foo(a: A): Int = a.bla }"
229203
val compilerForTesting = new ScalaCompilerForUnitTesting
230204
val usedNames = compilerForTesting.extractUsedNamesFromSrc(srcA, srcB)
231-
val expectedNames = standardNames ++ Set("B", "A", "a", "Int", "selectDynamic", "bla")
205+
val expectedNames = standardNames ++ Set("A", "a", "Int", "selectDynamic", "bla")
232206
assertEquals(usedNames("B"), expectedNames)
233207
}
234208

@@ -306,11 +280,11 @@ class ExtractUsedNamesSpecification {
306280
* definition.
307281
*/
308282
private val standardNames = Set(
309-
"scala",
310-
// The default parent of a class is "AnyRef" which is an alias for "Object"
311-
"AnyRef",
283+
// All classes extend Object
312284
"Object",
313-
"java.lang.Object;init;"
285+
// All classes have a default constructor called <init>
286+
"java.lang.Object;init;",
287+
// the return type of the default constructor is Unit
288+
"Unit"
314289
)
315-
316-
}
290+
}

0 commit comments

Comments
 (0)