# Problem with smart cast?

**URL:** <https://discuss.kotlinlang.org/t/problem-with-smart-cast/16295>\
**Category:** Support\
**Created:** [February 11, 2020, 3:54pm UTC](https://discuss.kotlinlang.org/t/problem-with-smart-cast/16295 "2020-02-11T15:54:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mathijs](https://avatars.discourse-cdn.com/v4/letter/m/4da419/32.png) [@mathijs](https://discuss.kotlinlang.org/u/mathijs)\
**Post date:** [February 11, 2020, 3:54pm UTC](https://discuss.kotlinlang.org/t/problem-with-smart-cast/16295/1 "2020-02-11T15:54:47Z")

</div>

I ran into an issue today with smart cast of two same-named variables in different scopes.  
A minimal reproduction example is like this:

```auto
fun main() {
    run {
        var modifiedVariable: Double? = null
        for (i in 1..3) {
            if (modifiedVariable != null) {
                println(i + modifiedVariable)
            }
            modifiedVariable = i + 0.1
        }
    }

    run {
        var modifiedVariable: Double? = null
        for (i in 1..3) {
            if (modifiedVariable != null) {
                println(i + modifiedVariable)
            }
            modifiedVariable = i + 0.1
        }
    }
}

```

The second block has a compile error “Smart cast to ‘Double’ is impossible, because ‘modifiedVariable’ is a local variable that is captured by a changing closure”.  
That changing closure seems to be the first run{} block, except that that one never can change the variable local to the second block.  
Indeed commenting out the first block fixes the compile error in the second, or renaming the variable to `modifiedVariable2` in one of the blocks also fixes the error.  
Is this a bug in the compiler or am I missing something?

---

<div class="post-metadata">

**Author:** ![arocnies](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/arocnies/32/5143_2.png) [@arocnies](https://discuss.kotlinlang.org/u/arocnies)\
**Post date:** [February 12, 2020, 3:38pm UTC](https://discuss.kotlinlang.org/t/problem-with-smart-cast/16295/2 "2020-02-12T15:38:50Z")

</div>

Odd. Maybe it’s a bug related to inlining.

Here’s a runnable example for others to play around with and edit without leaving the forum:

```run-kotlin
fun main() {
    run {
        var modifiedVariable: Double? = null
        for (i in 1..3) {
            if (modifiedVariable != null) {
                println(i + modifiedVariable)
            }
            modifiedVariable = i + 0.1
        }
    }

    run {
        var modifiedVariable: Double? = null
        for (i in 1..3) {
            if (modifiedVariable != null) {
                println(i + modifiedVariable)
            }
            modifiedVariable = i + 0.1
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![mathijs](https://avatars.discourse-cdn.com/v4/letter/m/4da419/32.png) [@mathijs](https://discuss.kotlinlang.org/u/mathijs)\
**Post date:** [February 13, 2020, 2:34pm UTC](https://discuss.kotlinlang.org/t/problem-with-smart-cast/16295/3 "2020-02-13T14:34:20Z")

</div>

Thanks for the runnable example! I’ve reported it as an issue here: [https://youtrack.jetbrains.com/issue/KT-36681](https://youtrack.jetbrains.com/issue/KT-36681)
