Skip to content

Commit a6097e3

Browse files
committed
Scala.js: Report an error on duplicate non-static getters
Forward port of scala-js/scala-js@65a1682
1 parent 019ec16 commit a6097e3

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

compiler/src/dotty/tools/backend/sjs/JSExportsGen.scala

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -350,14 +350,8 @@ final class JSExportsGen(jsCodeGen: JSCodeGen)(using Context) {
350350
val (getter, setters) = alts.partition(_.info.paramInfoss.head.isEmpty)
351351

352352
// We can have at most one getter
353-
if (getter.sizeIs > 1) {
354-
/* Member export of properties should be caught earlier, so if we get
355-
* here with a non-static export, something went horribly wrong.
356-
*/
357-
assert(static, s"Found more than one instance getter to export for name $jsName.")
358-
for (duplicate <- getter.tail)
359-
report.error(s"Duplicate static getter export with name '${jsName.displayName}'", duplicate)
360-
}
353+
if (getter.sizeIs > 1)
354+
reportCannotDisambiguateError(jsName, alts)
361355

362356
val getterBody = getter.headOption.map { getterSym =>
363357
genApplyForSingleExported(new FormalArgsRegistry(0, false), new ExportedSymbol(getterSym, static), static)
@@ -535,7 +529,7 @@ final class JSExportsGen(jsCodeGen: JSCodeGen)(using Context) {
535529
// 2. The optional argument count restriction has triggered
536530
// 3. We only have (more than once) repeated parameters left
537531
// Therefore, we should fail
538-
reportCannotDisambiguateError(jsName, alts)
532+
reportCannotDisambiguateError(jsName, alts.map(_.sym))
539533
js.Undefined()
540534
} else {
541535
val altsByTypeTest = groupByWithoutHashCode(alts) { exported =>
@@ -597,7 +591,7 @@ final class JSExportsGen(jsCodeGen: JSCodeGen)(using Context) {
597591
}
598592
}
599593

600-
private def reportCannotDisambiguateError(jsName: JSName, alts: List[Exported]): Unit = {
594+
private def reportCannotDisambiguateError(jsName: JSName, alts: List[Symbol]): Unit = {
601595
val currentClass = currentClassSym.get
602596

603597
/* Find a position that is in the current class for decent error reporting.
@@ -606,21 +600,26 @@ final class JSExportsGen(jsCodeGen: JSCodeGen)(using Context) {
606600
* same error in all compilers.
607601
*/
608602
val validPositions = alts.collect {
609-
case alt if alt.sym.owner == currentClass => alt.pos
603+
case alt if alt.owner == currentClass => alt.sourcePos
610604
}
611605
val pos: SourcePosition =
612606
if (validPositions.isEmpty) currentClass.sourcePos
613607
else validPositions.maxBy(_.point)
614608

615609
val kind =
616-
if (currentClass.isJSType) "method"
617-
else "exported method"
610+
if (alts.head.isJSGetter) "getter"
611+
else if (alts.head.isJSSetter) "setter"
612+
else "method"
613+
614+
val fullKind =
615+
if (currentClass.isJSType) kind
616+
else "exported " + kind
618617

619618
val displayName = jsName.displayName
620-
val altsTypesInfo = alts.map(_.typeInfo).mkString("\n ")
619+
val altsTypesInfo = alts.map(_.info.show).sorted.mkString("\n ")
621620

622621
report.error(
623-
s"Cannot disambiguate overloads for $kind $displayName with types\n $altsTypesInfo",
622+
s"Cannot disambiguate overloads for $fullKind $displayName with types\n $altsTypesInfo",
624623
pos)
625624
}
626625

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- Error: tests/neg-scalajs/js-non-native-members-conflicts.scala:7:6 --------------------------------------------------
2+
7 | def b: Unit = () // error
3+
| ^
4+
| Cannot disambiguate overloads for getter a with types
5+
| (): Unit
6+
| (): Unit
7+
-- Error: tests/neg-scalajs/js-non-native-members-conflicts.scala:10:2 -------------------------------------------------
8+
10 | object B1 // error
9+
| ^
10+
| Cannot disambiguate overloads for getter B1 with types
11+
| (): A.this.B1
12+
| (): Object
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import scala.scalajs.js
2+
import scala.scalajs.js.annotation.*
3+
4+
class A extends js.Object {
5+
def a: Unit = ()
6+
@JSName("a")
7+
def b: Unit = () // error
8+
9+
class B1 extends js.Object
10+
object B1 // error
11+
}

0 commit comments

Comments
 (0)