Skip to content

Commit d94d794

Browse files
committed
Handle exception with super calls
1 parent ee84531 commit d94d794

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

compiler/src/dotty/tools/dotc/transform/init/Semantic.scala

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import config.Printers.init as printer
1414
import reporting.trace as log
1515

1616
import Errors._
17-
import Util._
1817

1918
import scala.collection.mutable
2019

@@ -212,8 +211,7 @@ class Semantic {
212211
case thisRef: ThisRef =>
213212
val target =
214213
if superType.exists then
215-
// TODO: superType could be A & B when there is self-annotation
216-
resolveSuper(thisRef.klass, superType.classSymbol.asClass, meth)
214+
resolveSuper(thisRef.klass, superType, meth)
217215
else
218216
resolve(thisRef.klass, meth)
219217
if target.isOneOf(Flags.Method | Flags.Lazy) then
@@ -239,8 +237,7 @@ class Semantic {
239237
case warm: Warm =>
240238
val target =
241239
if superType.exists then
242-
// TODO: superType could be A & B when there is self-annotation
243-
resolveSuper(warm.klass, superType.classSymbol.asClass, meth)
240+
resolveSuper(warm.klass, superType, meth)
244241
else
245242
resolve(warm.klass, meth)
246243
if target.is(Flags.Param) then
@@ -706,7 +703,7 @@ class Semantic {
706703
// init param fields
707704
klass.paramAccessors.foreach { acc =>
708705
if (!acc.is(Flags.Method)) {
709-
traceIndented(acc.show + " initialized", printer)
706+
printer.println(acc.show + " initialized")
710707
thisV.updateField(acc, Hot)
711708
}
712709
}
@@ -843,4 +840,25 @@ object Semantic {
843840
Some((tref, newTree, fn.symbol, argss))
844841
case _ => None
845842
}
843+
844+
extension (symbol: Symbol) def hasSource(using Context): Boolean =
845+
!symbol.defTree.isEmpty
846+
847+
def resolve(cls: ClassSymbol, sym: Symbol)(using Context): Symbol =
848+
if (sym.isEffectivelyFinal || sym.isConstructor) sym
849+
else sym.matchingMember(cls.appliedRef)
850+
851+
def resolveSuper(cls: ClassSymbol, superType: Type, sym: Symbol)(using Context): Symbol = {
852+
import annotation.tailrec
853+
@tailrec def loop(bcs: List[ClassSymbol]): Symbol = bcs match {
854+
case bc :: bcs1 =>
855+
val cand = sym.matchingDecl(bcs.head, cls.thisType)
856+
.suchThat(alt => !alt.is(Flags.Deferred)).symbol
857+
if (cand.exists) cand else loop(bcs.tail)
858+
case _ =>
859+
NoSymbol
860+
}
861+
loop(cls.info.baseClasses.dropWhile(sym.owner != _))
862+
}
863+
846864
}

tests/init/neg/super.scala

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
trait A:
2+
def foo() = 1
3+
4+
trait B:
5+
def foo() = 1
6+
7+
trait C:
8+
def foo() = n
9+
def n: Int
10+
11+
class Foo extends A, B, C:
12+
super[A].foo()
13+
14+
override def foo() = n
15+
16+
val n = 10
17+
18+
class Bar extends A, B, C:
19+
super[C].foo()
20+
21+
override def foo() = n * n
22+
23+
val n = 10
24+
25+
class Qux extends A, B, C:
26+
super.foo()
27+
28+
override def foo() = n * n
29+
30+
val n = 10

0 commit comments

Comments
 (0)