Skip to content

Commit 350864f

Browse files
committed
Fix #5336: Disallow overloads in refinements
1 parent 8bd7bee commit 350864f

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ public enum ErrorMessageID {
139139
PureExpressionInStatementPositionID,
140140
TraitCompanionWithMutableStaticID,
141141
LazyStaticFieldID,
142-
StaticOverridingNonStaticMembersID
142+
StaticOverridingNonStaticMembersID,
143+
OverloadInRefinementID
143144
;
144145

145146
public int errorNumber() {

compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2170,4 +2170,13 @@ object messages {
21702170
override def kind: String = "Syntax"
21712171
override def explanation: String = ""
21722172
}
2173+
2174+
case class OverloadInRefinement(rsym: Symbol)(implicit val ctx: Context)
2175+
extends Message(OverloadInRefinementID) {
2176+
override def msg: String = "Refinements cannot introduce overloaded definitions"
2177+
override def kind: String = "Overload"
2178+
override def explanation: String =
2179+
hl"""The refinement `$rsym` introduces an overloaded definition.
2180+
|Refinements cannot contain overloaded definitions.""".stripMargin
2181+
}
21732182
}

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,12 @@ class Typer extends Namer
12351235
val rsym = refinement.symbol
12361236
if (rsym.info.isInstanceOf[PolyType] && rsym.allOverriddenSymbols.isEmpty)
12371237
ctx.error(PolymorphicMethodMissingTypeInParent(rsym, tpt1.symbol), refinement.pos)
1238+
1239+
// For some reason, `rsym.isOverloaded` always return false here.
1240+
val member = refineCls.info.member(rsym.name)
1241+
if (member.isOverloaded) {
1242+
ctx.error(OverloadInRefinement(rsym), refinement.pos)
1243+
}
12381244
}
12391245
assignType(cpy.RefinedTypeTree(tree)(tpt1, refinements1), tpt1, refinements1, refineCls)
12401246
}

tests/neg/structural.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ object Test3 {
88

99
def h(x: { def f[T](a: T): Int }) = x.f[Int](4) // error: polymorphic refinement method ... no longer allowed
1010

11+
type A = { def foo(x: Int): Unit; def foo(x: String): Unit } // error: overloaded definition // error: overloaded definition
12+
type B = { val foo: Int; def foo: Int } // error: duplicate foo
1113
}

0 commit comments

Comments
 (0)