Skip to content

Treat all incomplete ifs as statements #14960

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

Merged
merged 2 commits into from
Apr 19, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ class SemanticdbInputStream private (buffer: Array[Byte], input: InputStream) {
throw new IllegalStateException(
s"refillBuffer() called when $n bytes were already available in buffer")
}
if (totalBytesRetired + bufferPos + n > currentLimit) false
else if (input != null) {
if totalBytesRetired + bufferPos + n <= currentLimit && input != null then
val pos: Int = bufferPos
if (pos > 0) {
if (bufferSize > pos) {
Expand All @@ -166,7 +165,6 @@ class SemanticdbInputStream private (buffer: Array[Byte], input: InputStream) {
recomputeBufferSizeAfterLimit()
return ((bufferSize >= n) || tryRefillBuffer(n))
}
}
false
}

Expand Down
13 changes: 10 additions & 3 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1128,15 +1128,22 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
if tree.isInline then checkInInlineContext("inline if", tree.srcPos)
val cond1 = typed(tree.cond, defn.BooleanType)

def isIncomplete(tree: untpd.If): Boolean = tree.elsep match
case EmptyTree => true
case elsep: untpd.If => isIncomplete(elsep)
case _ => false

val branchPt = if isIncomplete(tree) then defn.UnitType else pt.dropIfProto

val result =
if tree.elsep.isEmpty then
val thenp1 = typed(tree.thenp, defn.UnitType)(using cond1.nullableContextIf(true))
val thenp1 = typed(tree.thenp, branchPt)(using cond1.nullableContextIf(true))
val elsep1 = tpd.unitLiteral.withSpan(tree.span.endPos)
cpy.If(tree)(cond1, thenp1, elsep1).withType(defn.UnitType)
else
val thenp1 :: elsep1 :: Nil = harmonic(harmonize, pt) {
val thenp0 = typed(tree.thenp, pt.dropIfProto)(using cond1.nullableContextIf(true))
val elsep0 = typed(tree.elsep, pt.dropIfProto)(using cond1.nullableContextIf(false))
val thenp0 = typed(tree.thenp, branchPt)(using cond1.nullableContextIf(true))
val elsep0 = typed(tree.elsep, branchPt)(using cond1.nullableContextIf(false))
thenp0 :: elsep0 :: Nil
}
assignType(cpy.If(tree)(cond1, thenp1, elsep1), thenp1, elsep1)
Expand Down
8 changes: 8 additions & 0 deletions tests/pos/i14914.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

def Test(b: Boolean) =
val a =
if b then
1
else if !b then
2
val _: Unit = a