Skip to content

Commit e2f5ea1

Browse files
authoredDec 6, 2024··
Merge pull request sbt#1507 from retronym/anon-dep
Exclude anon classes from dependency registration
2 parents 5ca7ed7 + c752dae commit e2f5ea1

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed
 

‎internal/compiler-bridge/src/main/scala/xsbt/Dependency.scala

+25-1
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,27 @@ final class Dependency(val global: CallbackGlobal) extends LocateClassFile with
189189
* but when it does we must ensure the incremental compiler tries its best no to lose
190190
* any dependency. Therefore, we do a last-time effort to get the origin of the symbol
191191
* by inspecting the classpath manually.
192+
*
193+
* UPDATE: This can also happen without compiler bugs if the symbol is simply uninitialized.
194+
* Example, `class Client { def foo = Server.foo }`. When compiling client, the type `Foo` returned
195+
* by `Server.foo` does not need to be initialized as we do not select from it or check its
196+
* conformance to another type.
197+
*
198+
* Initializing `targetSymbol` before calling `assosicatedFile` would work but is problematic
199+
* see zinc/zinc#949
200+
*
201+
* Perhaps consider this?
202+
* val file = targetSymbol.associatedFile match {
203+
* case NoAbstractFile => sym.rawInfo match {
204+
* case cfl: global.loaders.ClassfileLoader =>
205+
* val f = cfl.associatedFile(sym) // Gets the file from the loader
206+
* if (f.exists) f else NoAbstractFile
207+
* case f => f
208+
* }
209+
* }
210+
*
211+
* Or the status quo might just be perfectly fine -- if compilation doesn't need to force `Foo`,
212+
* then there isn't a real dependency.
192213
*/
193214
val fqn = fullName(targetSymbol, '.', targetSymbol.moduleSuffix, false)
194215
global.findAssociatedFile(fqn) match {
@@ -291,9 +312,12 @@ final class Dependency(val global: CallbackGlobal) extends LocateClassFile with
291312
assert(fromClass.isClass, Feedback.expectedClassSymbol(fromClass))
292313
val depClass = enclOrModuleClass(dep)
293314
val dependency = ClassDependency(fromClass, depClass)
315+
// An anonymous class be the enclosing class of an existential type symbol inferred from refinements,
316+
// prior to https://github.com/scala/scala/pull/10940. Allowing this here leads to a dependency on class name
317+
// that does not exist. Guard against it here to avoid the issue with legacy compiler versions.
294318
if (
295319
!cache.contains(dependency) &&
296-
!depClass.isRefinementClass
320+
!depClass.isAnonOrRefinementClass
297321
) {
298322
process(dependency)
299323
cache.add(dependency)

0 commit comments

Comments
 (0)
Please sign in to comment.