Skip to content

Commit 915b1eb

Browse files
committed
Warn if files containing the same toplevel def come from different directories.
1 parent 8ac2c66 commit 915b1eb

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

compiler/src/dotty/tools/dotc/core/SymDenotations.scala

+19-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import annotation.tailrec
1616
import util.SimpleIdentityMap
1717
import util.Stats
1818
import java.util.WeakHashMap
19+
import scala.util.control.NonFatal
1920
import config.Config
2021
import reporting.diagnostic.Message
2122
import 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,24 @@ 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
2179+
catch case NonFatal(ex) => true
2180+
if !ambiguityWarningIssued then
2181+
for conflicting <- assocFiles.find(!sameContainer(_)) do
2182+
ctx.warning(i"""${ambiguousFilesMsg(youngest.tail.head)}
2183+
|Keeping only the definition in $chosen""")
2184+
ambiguityWarningIssued = true
2185+
multi.filterWithPredicate(_.symbol.associatedFile == chosen)
21732186
end dropStale
21742187

21752188
if (symbol `eq` defn.ScalaPackageClass) {

0 commit comments

Comments
 (0)