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)

(cherry picked from commit fff2bf0)
  • Loading branch information
WojciechMazur committed Jan 17, 2024
1 parent 18a2118 commit c68a838
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 @@ -185,7 +185,11 @@ trait NirGenExpr[G <: nsc.Global with Singleton] { self: NirGenPhase[G] =>

def genIf(tree: If): 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 @@ -427,7 +427,11 @@ trait NirGenExpr(using Context) {
def genIf(tree: If): 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 c68a838

Please sign in to comment.