@@ -16,6 +16,7 @@ import annotation.tailrec
16
16
import util .SimpleIdentityMap
17
17
import util .Stats
18
18
import java .util .WeakHashMap
19
+ import scala .util .control .NonFatal
19
20
import config .Config
20
21
import reporting .diagnostic .Message
21
22
import reporting .diagnostic .messages .BadSymbolicReference
@@ -2081,6 +2082,7 @@ object SymDenotations {
2081
2082
2082
2083
private var packageObjsCache : List [ClassDenotation ] = _
2083
2084
private var packageObjsRunId : RunId = NoRunId
2085
+ private var ambiguityWarningIssued : Boolean = false
2084
2086
2085
2087
/** The package objects in this class */
2086
2088
def packageObjs (implicit ctx : Context ): List [ClassDenotation ] = {
@@ -2163,13 +2165,28 @@ object SymDenotations {
2163
2165
// pick the variant(s) from the youngest class file
2164
2166
val lastModDate = assocFiles.map(_.lastModified).max
2165
2167
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"""
2166
2174
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)
2173
2190
end dropStale
2174
2191
2175
2192
if (symbol `eq` defn.ScalaPackageClass ) {
0 commit comments