Skip to content

Commit

Permalink
improvement: In NIR codegen always use nir.Type.Unit for if return …
Browse files Browse the repository at this point in the history
…type if one of branches is unit type (#3644)
  • Loading branch information
WojciechMazur committed Jan 2, 2024
1 parent 115ed42 commit fff2bf0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,11 @@ trait NirGenExpr[G <: nsc.Global with Singleton] { self: NirGenPhase[G] =>

def genIf(tree: If): nir.Val = {
val If(cond, thenp, elsep) = tree
val retty = genType(tree.tpe)
def isUnitType(tpe: Type) =
defn.isUnitType(tpe) || tpe =:= defn.BoxedUnitTpe
val retty =
if (isUnitType(thenp.tpe) || isUnitType(elsep.tpe)) nir.Type.Unit
else genType(tree.tpe)
genIf(retty, cond, thenp, elsep)(tree.pos)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,11 @@ trait NirGenExpr(using Context) {
def genIf(tree: If): nir.Val = {
given nir.Position = tree.span
val If(cond, thenp, elsep) = tree
val retty = genType(tree.tpe)
def isUnitType(tpe: Type) =
tpe =:= defn.UnitType || defn.isBoxedUnitClass(tpe.sym)
val retty =
if (isUnitType(thenp.tpe) || isUnitType(elsep.tpe)) nir.Type.Unit
else genType(tree.tpe)
genIf(retty, cond, thenp, elsep)
}

Expand Down

0 comments on commit fff2bf0

Please sign in to comment.