Skip to content

Commit 147f08d

Browse files
committed
Fix #4119: Warn when modules have the same name as a class modulo case
1 parent e85473d commit 147f08d

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed

compiler/src/dotty/tools/backend/jvm/GenBCode.scala

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -190,19 +190,25 @@ class GenBCodePipeline(val entryPoints: List[Symbol], val int: DottyBackendInter
190190
val claszSymbol = cd.symbol
191191

192192
// GenASM checks this before classfiles are emitted, https://github.com/scala/scala/commit/e4d1d930693ac75d8eb64c2c3c69f2fc22bec739
193-
val lowercaseJavaClassName = claszSymbol.name.toString.toLowerCase
194-
caseInsensitively.get(lowercaseJavaClassName) match {
195-
case None =>
196-
caseInsensitively.put(lowercaseJavaClassName, claszSymbol)
197-
case Some(dupClassSym) =>
198-
// Order is not deterministic so we enforce lexicographic order between the duplicates for error-reporting
199-
if (claszSymbol.name.toString < dupClassSym.name.toString)
200-
ctx.warning(s"Class ${claszSymbol.name} differs only in case from ${dupClassSym.name}. " +
201-
"Such classes will overwrite one another on case-insensitive filesystems.", claszSymbol.pos)
202-
else
203-
ctx.warning(s"Class ${dupClassSym.name} differs only in case from ${claszSymbol.name}. " +
204-
"Such classes will overwrite one another on case-insensitive filesystems.", dupClassSym.pos)
193+
def checkName(claszSymbol: Symbol): Unit = {
194+
val lowercaseJavaClassName = claszSymbol.effectiveName.toString.toLowerCase
195+
caseInsensitively.get(lowercaseJavaClassName) match {
196+
case None =>
197+
caseInsensitively.put(lowercaseJavaClassName, claszSymbol)
198+
case Some(dupClassSym) =>
199+
if (claszSymbol.effectiveName.toString != dupClassSym.effectiveName.toString) {
200+
// Order is not deterministic so we enforce lexicographic order between the duplicates for error-reporting
201+
val (cl1, cl2) =
202+
if (claszSymbol.effectiveName.toString < dupClassSym.effectiveName.toString) (claszSymbol, dupClassSym)
203+
else (dupClassSym, claszSymbol)
204+
ctx.warning(s"Class ${cl1.effectiveName} differs only in case from ${cl2.effectiveName}. " +
205+
"Such classes will overwrite one another on case-insensitive filesystems.", cl1.pos)
206+
}
207+
}
205208
}
209+
checkName(claszSymbol)
210+
if (int.symHelper(claszSymbol).isModuleClass)
211+
checkName(claszSymbol.companionModule)
206212

207213
// -------------- mirror class, if needed --------------
208214
val mirrorC =

tests/neg-custom-args/fatal-warnings/i2673.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,14 @@ package Foos
22

33
class Foo // error
44
class foo
5+
6+
class Bar // error
7+
object bar
8+
9+
class baz
10+
object Baz // error
11+
12+
object Outer {
13+
class X // error
14+
object x
15+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package Foos
2+
3+
class Bar // error
4+
object bar
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package Foos
2+
3+
object Outer {
4+
class X // error
5+
object x
6+
}

0 commit comments

Comments
 (0)