Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deterministically adding additional interfaces #20593

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions compiler/src/dotty/tools/backend/jvm/BTypesFromSymbols.scala
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,12 @@ class BTypesFromSymbols[I <: DottyBackendInterface](val int: I, val frontendAcce
val directlyInheritedTraits = sym.directlyInheritedTraits
val directlyInheritedTraitsSet = directlyInheritedTraits.toSet
val allBaseClasses = directlyInheritedTraits.iterator.flatMap(_.asClass.baseClasses.drop(1)).toSet
val superCalls = superCallsMap.getOrElse(sym, Set.empty)
val additional = (superCalls -- directlyInheritedTraitsSet).filter(_.is(Trait))
val superCalls = superCallsMap.getOrElse(sym, List.empty)
val superCallsSet = superCalls.toSet
val additional = superCalls.filter(t => !directlyInheritedTraitsSet(t) && t.is(Trait))
// if (additional.nonEmpty)
// println(s"$fullName: adding supertraits $additional")
directlyInheritedTraits.filter(t => !allBaseClasses(t) || superCalls(t)) ++ additional
directlyInheritedTraits.filter(t => !allBaseClasses(t) || superCallsSet(t)) ++ additional
}

val interfaces = classSym.superInterfaces.map(classBTypeFromSymbol)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import StdNames.nme
import NameKinds.{LazyBitMapName, LazyLocalName}
import Names.Name

class DottyBackendInterface(val superCallsMap: ReadOnlyMap[Symbol, Set[ClassSymbol]])(using val ctx: Context) {
class DottyBackendInterface(val superCallsMap: ReadOnlyMap[Symbol, List[ClassSymbol]])(using val ctx: Context) {

private val desugared = new java.util.IdentityHashMap[Type, tpd.Select]

Expand Down
7 changes: 4 additions & 3 deletions compiler/src/dotty/tools/backend/jvm/GenBCode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ class GenBCode extends Phase { self =>

override def isRunnable(using Context) = super.isRunnable && !ctx.usedBestEffortTasty

private val superCallsMap = new MutableSymbolMap[Set[ClassSymbol]]
private val superCallsMap = new MutableSymbolMap[List[ClassSymbol]]
def registerSuperCall(sym: Symbol, calls: ClassSymbol): Unit = {
val old = superCallsMap.getOrElse(sym, Set.empty)
superCallsMap.update(sym, old + calls)
val old = superCallsMap.getOrElse(sym, List.empty)
if (!old.contains(calls))
superCallsMap.update(sym, old :+ calls)
}

private val entryPoints = new mutable.HashSet[String]()
Expand Down
24 changes: 24 additions & 0 deletions compiler/test/dotty/tools/backend/jvm/DottyBytecodeTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1963,6 +1963,30 @@ class DottyBytecodeTests extends DottyBytecodeTest {
assertSameCode(instructions, expected)
}
}

/**
* Test 'additional' imports are generated in deterministic order
* https://github.com/scala/scala3/issues/20496
*/
@Test def deterministicAdditionalImports = {
val source =
"""trait Actor:
| def receive() = ()
|trait Timers:
| def timers() = ()
|abstract class ShardCoordinator extends Actor with Timers
|class PersistentShardCoordinator extends ShardCoordinator:
| def foo =
| super.receive()
| super.timers()""".stripMargin
checkBCode(source) { dir =>
val clsIn = dir.lookupName("PersistentShardCoordinator.class", directory = false).input
val clsNode = loadClassNode(clsIn)

val expected = List("Actor", "Timers")
assertEquals(expected, clsNode.interfaces.asScala)
}
}
}

object invocationReceiversTestCode {
Expand Down
Loading