Skip to content

Commit

Permalink
Fix compilation failures when resolving intrinsic sizeOf for Nat digi…
Browse files Browse the repository at this point in the history
…ts in Scala 3. (#3245)

When using intrinsic version of sizeOf / alignmentOf for types using unsafe.Nat types in the projects in which they're defined (nativelib) it might happend that they're not yet available. In such case use fallback method of parsing digits based on class name
  • Loading branch information
WojciechMazur committed Apr 4, 2023
1 parent 9864f50 commit 825f33d
Showing 1 changed file with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import core.Types._
import core.Symbols._
import core.StdNames._
import core.TypeErasure._
import core.TypeError
import dotty.tools.dotc.report
import dotty.tools.dotc.typer.TyperPhase
import dotty.tools.dotc.transform.SymUtils._
Expand Down Expand Up @@ -237,11 +238,22 @@ trait NirGenType(using Context) {
}

private def genFixedSizeArray(st: SimpleType): nir.Type = {
def parseDigit(st: SimpleType): Int = {
try defnNir.NatBaseClasses.indexOf(st.sym)
catch {
case e: TypeError =>
// Can happen when Nat class is not yet availble, etc. usages withing nativelib
st.sym.name.toSimpleName.toString match
case s"Nat$$_${digit}" if digit.length == 1 =>
digit.toIntOption.getOrElse(throw e)
case _ => throw e
}
}
def natClassToInt(st: SimpleType): Int =
if (st.targs.isEmpty) defnNir.NatBaseClasses.indexOf(st.sym)
if (st.targs.isEmpty) parseDigit(st)
else
st.targs.foldLeft(0) {
case (acc, st) => acc * 10 + defnNir.NatBaseClasses.indexOf(st.sym)
case (acc, st) => acc * 10 + parseDigit(st)
}

val SimpleType(_, Seq(elemType, size)) = st
Expand Down

0 comments on commit 825f33d

Please sign in to comment.