@@ -16,6 +16,7 @@ import annotation.tailrec
1616import util .SimpleIdentityMap
1717import util .Stats
1818import java .util .WeakHashMap
19+ import scala .util .control .NonFatal
1920import config .Config
2021import reporting .diagnostic .Message
2122import reporting .diagnostic .messages .BadSymbolicReference
@@ -2081,6 +2082,7 @@ object SymDenotations {
20812082
20822083 private var packageObjsCache : List [ClassDenotation ] = _
20832084 private var packageObjsRunId : RunId = NoRunId
2085+ private var ambiguityWarningIssued : Boolean = false
20842086
20852087 /** The package objects in this class */
20862088 def packageObjs (implicit ctx : Context ): List [ClassDenotation ] = {
@@ -2163,13 +2165,28 @@ object SymDenotations {
21632165 // pick the variant(s) from the youngest class file
21642166 val lastModDate = assocFiles.map(_.lastModified).max
21652167 val youngest = assocFiles.filter(_.lastModified == lastModDate)
2168+ val chosen = youngest.head
2169+ def ambiguousFilesMsg (f : AbstractFile ) =
2170+ em """ Toplevel definition $name is defined in
2171+ | $chosen
2172+ |and also in
2173+ | $f"""
21662174 if youngest.size > 1 then
2167- throw TypeError (em """ Toplevel definition $name is defined in
2168- | ${youngest.head}
2169- |and also in
2170- | ${youngest.tail.head}
2171- |One of these files should be removed from the classpath. """ )
2172- multi.filterWithPredicate(_.symbol.associatedFile == youngest.head)
2175+ throw TypeError (i """ ${ambiguousFilesMsg(youngest.tail.head)}
2176+ |One of these files should be removed from the classpath. """ )
2177+ def sameContainer (f : AbstractFile ): Boolean =
2178+ try f.container == chosen.container catch case NonFatal (ex) => true
2179+
2180+ // Warn if one of the older files comes from a different container.
2181+ // In that case picking the youngest file is not necessarily what we want,
2182+ // since the older file might have been loaded from a jar earlier in the
2183+ // classpath.
2184+ if ! ambiguityWarningIssued then
2185+ for conflicting <- assocFiles.find(! sameContainer(_)) do
2186+ ctx.warning(i """ ${ambiguousFilesMsg(youngest.tail.head)}
2187+ |Keeping only the definition in $chosen""" )
2188+ ambiguityWarningIssued = true
2189+ multi.filterWithPredicate(_.symbol.associatedFile == chosen)
21732190 end dropStale
21742191
21752192 if (symbol `eq` defn.ScalaPackageClass ) {
0 commit comments