# Unreachable code after return statement compiles!

**URL:** <https://discuss.kotlinlang.org/t/unreachable-code-after-return-statement-compiles/5490>\
**Category:** Language Design\
**Created:** [November 20, 2017, 3:05am UTC](https://discuss.kotlinlang.org/t/unreachable-code-after-return-statement-compiles/5490 "2017-11-20T03:05:23Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![determinant](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@determinant](https://discuss.kotlinlang.org/u/determinant)\
**Post date:** [November 20, 2017, 3:05am UTC](https://discuss.kotlinlang.org/t/unreachable-code-after-return-statement-compiles/5490/1 "2017-11-20T03:05:23Z")

</div>

I was surprised to find that the following snippet compiles:

```
fun main(args: Array<String>) {
    test {
        println("hey!")
        return
        println("Strangely this compiles even though it's right after the return statement")
    }
    println("The compiler should complain about this too")
}

inline fun test(body: () -> Unit) {
    body()
}

```

Having a statement after a return fails to compile in Java so I would have expected the Kotlin compiler to abort as well. Additionally, no warnings were presented (but this really should be a compilation error).

This seems pretty severe since it will cause surprises (especially if it’s not as obvious as performing another statement right after returning).

I’m using IntelliJ 2017.2.2 with Kotlin plugin 1.1.60

---

<div class="post-metadata">

**Author:** ![nanodeath](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nanodeath/32/1803_2.png) [@nanodeath](https://discuss.kotlinlang.org/u/nanodeath)\
**Post date:** [November 20, 2017, 5:07am UTC](https://discuss.kotlinlang.org/t/unreachable-code-after-return-statement-compiles/5490/2 "2017-11-20T05:07:45Z")

</div>

FWIW, IntelliJ does complain pretty obviously – everything after your return will get an obvious ugly yellow background.

---

<div class="post-metadata">

**Author:** ![nutjob2](https://avatars.discourse-cdn.com/v4/letter/n/ccd318/32.png) [@nutjob2](https://discuss.kotlinlang.org/u/nutjob2)\
**Post date:** [November 20, 2017, 10:54am UTC](https://discuss.kotlinlang.org/t/unreachable-code-after-return-statement-compiles/5490/3 "2017-11-20T10:54:30Z")

</div>

Most compilers issue a warning about unreachable code rather than stop compiling. There are valid reasons for having unreachable code, such as disabling a section of code that may be re-enabled. It’s hardly a critical error.

---

<div class="post-metadata">

**Author:** ![determinant](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@determinant](https://discuss.kotlinlang.org/u/determinant)\
**Post date:** [November 20, 2017, 12:58pm UTC](https://discuss.kotlinlang.org/t/unreachable-code-after-return-statement-compiles/5490/4 "2017-11-20T12:58:46Z")

</div>

The above code did not generate any warnings at all from both the compiler and also from IntelliJ (there was nothing in yellow)

---

<div class="post-metadata">

**Author:** ![determinant](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@determinant](https://discuss.kotlinlang.org/u/determinant)\
**Post date:** [November 20, 2017, 1:24pm UTC](https://discuss.kotlinlang.org/t/unreachable-code-after-return-statement-compiles/5490/5 "2017-11-20T13:24:05Z")

</div>

I can see why such a decision would be made for languages that have text based macros however this doesn’t apply to Java or Kotlin. This enables Java & Kotlin to provide additional robustness guarantees that wouldn’t normally be possible with text based macros.

I can also see the value in allowing you to control if some code should be executed inside a condition which is controlled by a boolean constant. Something like this could be used to enable a testing mode so that additional tests are performed when its enabled.

However, any statement directly after a return in Kotlin is likely a human error so having the compiler complain about it would reduce potential errors. If you want to disable a section of code, you could easily comment it out.

Given that many of the Kotlin users are coming from Java, and that Java prevents compiling such code, I would suggest that fixing this would make Kotlin a better language.

---

<div class="post-metadata">

**Author:** ![nanodeath](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nanodeath/32/1803_2.png) [@nanodeath](https://discuss.kotlinlang.org/u/nanodeath)\
**Post date:** [November 20, 2017, 3:50pm UTC](https://discuss.kotlinlang.org/t/unreachable-code-after-return-statement-compiles/5490/6 "2017-11-20T15:50:44Z")

</div>

I personally like it – when I’m debugging a method and I want to manually return a value early (instead of calculating the value as usual) I can just plop `return foo` in the middle somewhere and it still works without having to comment out a bunch of code. Plus I doubt they can change it now – some code surely is relying on this behavior, though perhaps they could change it in a new major version. e.g. 1.2.

As for the code itself, well, here’s what I get, which seems painfully obvious to me:

 ![image](https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/d/d5a311d93f6d36978b1cbe4f7f227b28b3596a21.png)  
 ![image](https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/4/428f228313823662837df7d711b3737308902326.jpg)

---

<div class="post-metadata">

**Author:** ![determinant](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@determinant](https://discuss.kotlinlang.org/u/determinant)\
**Post date:** [November 21, 2017, 3:27am UTC](https://discuss.kotlinlang.org/t/unreachable-code-after-return-statement-compiles/5490/7 "2017-11-21T03:27:25Z")

</div>

Here’s a screenshot with no warning:  
 ![image](https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/d/d7d9d41571b6535cd592c7286a7e31f80edfaee7.png)

So this is closer to the structure of my original code. This occurred because I went back and added an early return inside the lambda without reconsidering the follow-on code (and IntelliJ didn’t present any warning so I didn’t think much about it).

Realizing the problem afterwards, I went back to see how broken the system was by adding another statement immediately after the return so I didn’t look out for warnings again. So you are correct that a warning is presented if you have a statement right after the return within the same block (which is fairly easy to spot) but unfortunately my scenario isn’t handled correctly.

Although I would prefer an error, the compiler should at the very least issue a warning about the unreachable code after the lambda.

---

<div class="post-metadata">

**Author:** ![nanodeath](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nanodeath/32/1803_2.png) [@nanodeath](https://discuss.kotlinlang.org/u/nanodeath)\
**Post date:** [November 21, 2017, 5:49am UTC](https://discuss.kotlinlang.org/t/unreachable-code-after-return-statement-compiles/5490/8 "2017-11-21T05:49:58Z")

</div>

Hm…you’re right, technically the code following the `return` is unreachable. Perhaps it’s too complicated for the compiler to do that level of unreachable code analysis – for instance, if the `body()` call was wrapped in any sort of conditional, it’d be impossible to infer if the second `println()` was unreachable or not. Might be interesting if the compiler did some of these checks after inlining the method call, but I have no idea if that’s practical.

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [November 22, 2017, 2:02am UTC](https://discuss.kotlinlang.org/t/unreachable-code-after-return-statement-compiles/5490/9 "2017-11-22T02:02:15Z")

</div>

You do not get unreachable code warning on `println("This doesn't get executed")` because the compiler cannot infer that it’s unreachable. When analyzing the body of `main` function, the compiler doesn’t know anything about how the `test` function is implemented and whether it calls `body` function at all.

There’s a plan to provide the way to declare that `test` will definitely call `body` once invoked, so that the code after such `test` invocation as in your example will be marked as unreachable.

---

<div class="post-metadata">

**Author:** ![determinant](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@determinant](https://discuss.kotlinlang.org/u/determinant)\
**Post date:** [November 22, 2017, 8:09pm UTC](https://discuss.kotlinlang.org/t/unreachable-code-after-return-statement-compiles/5490/11 "2017-11-22T20:09:29Z")

</div>

Since the compiler warns about unreachable code for a statement after a return within the same block, it seems like this problem can be fixed if the unreachable-code detection logic is performed after the inlining step (instead of before).

The compiler already complains if I try to add a return statement in the lambda when the “test” function is not declared as inline so it seems like it’s already half-way there.

Has a bug already been filed for this?

Thanks

---

<div class="post-metadata">

**Author:** ![nutjob2](https://avatars.discourse-cdn.com/v4/letter/n/ccd318/32.png) [@nutjob2](https://discuss.kotlinlang.org/u/nutjob2)\
**Post date:** [November 22, 2017, 10:09pm UTC](https://discuss.kotlinlang.org/t/unreachable-code-after-return-statement-compiles/5490/12 "2017-11-22T22:09:57Z")

</div>

What are you basing your “half-way there” estimate on, becuase in general it’s impossible to write a compiler that can detect all unreachable code. Even if you don’t write for the general case, the compiler would likely be very slow if it tried so find most cases of unreachable code, and would take many person-years of effort to write.

---

<div class="post-metadata">

**Author:** ![determinant](https://avatars.discourse-cdn.com/v4/letter/d/bbce88/32.png) [@determinant](https://discuss.kotlinlang.org/u/determinant)\
**Post date:** [November 23, 2017, 2:37pm UTC](https://discuss.kotlinlang.org/t/unreachable-code-after-return-statement-compiles/5490/14 "2017-11-23T14:37:27Z")

</div>

The estimated difficulty is based on the fact that the compiler already has the smarts to detect this kind of problem if the detection phase was run after the inlining phase (because the inlining phase would transform the code into a format which matches a pattern that the compiler already detects).

So fixing this problem might be as simple as just changing the order of the steps so that the inlining step happens before the unreachable-code detection step.

---

<div class="post-metadata">

**Author:** ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)\
**Post date:** [November 23, 2017, 9:24pm UTC](https://discuss.kotlinlang.org/t/unreachable-code-after-return-statement-compiles/5490/15 "2017-11-23T21:24:00Z")

</div>

> [@determinant](#):
>
> So fixing this problem might be as simple as just changing the order of the steps so that the inlining step happens before the unreachable-code detection step.

Given that inlining operates on the bytecode level, it’s not as simple as it seems.  
Even if it was possible to reverse these steps, the cost of doing inlining and then analyzing the resulting code would be prohibitively high, especially in IDE where you need to analyze the code many times during editing.
