Strange null-check behavior

package test

fun something() : String? = null

fun main(args : Array<String>) : Unit {
  var line : String?
  do {
  line = something()
  if (line != null) {
           val x : String = line
  } else {
           break
  }
  } while(true);
}

At this code compiler fails with message that x can’t be assigned from line because it String?. Why?

But if I move declaration line like this

``

package test

fun something() : String? = null

fun main(args : Array<String>) : Unit {
  do {
  val line: String? = something()
  if (line != null) {
           val x : String = line
  } else {
           break
  }
  } while(true);
}


then everything will be OK…

Whether it’s bug or I missed something?

Kotlin compiler monitors nullability only for val variables. So it's enought to declare 'test' variable with val in your first example to make code compile without errors.

I see.. you are right