Thread overview
[Issue 13575] Unreachable scope(failure) should be warned
Jan 17, 2020
Witold Baryluk
Jan 17, 2020
Witold Baryluk
Dec 17, 2022
Iain Buclaw
January 17, 2020
https://issues.dlang.org/show_bug.cgi?id=13575

Witold Baryluk <witold.baryluk+d@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |witold.baryluk+d@gmail.com

--- Comment #1 from Witold Baryluk <witold.baryluk+d@gmail.com> ---
Your example is pretty good, but not good enough as it introduces other issue, that prevents compiler from optimizing it properly.

The writeln("Failed in someFunc()") can fail with the exception (i.e. if the
stdout was a pipe into other process, and that process died, or it was
redirected to a file, and there was IO error or disk is full / out of quota,
just some examples)! And then the first scope(failure) which can in fact
execute!


Better example:


```
int i = 0;

size_t f(string x) nothrow {
  scope(failure) {
    i = 5;
    return 30;
  }
  scope(failure) {
    i = 10;
    return 42;
  }
  throw new Exception("a");
}

int main(string[] args) {
  return cast(int)f(args[0]) + i*i;
}
```

Notice that I also added `nothrow` and made scopes use code that can't throw on its own.

The compiler doesn't even recognize that the first `scope(failure)` is not dead code. I still see it in the disassembly of the binary.


When I run the code it does return 142, as expected.

If I change the code to:

```
int i = 0;

size_t f(string x) nothrow {
  scope(failure) {
    i = 5;
    return 30;
  }
  scope(failure) {
    i = 10;
    throw new Exception("b");
  }
  throw new Exception("a");
}

int main(string[] args) {
  return cast(int)f(args[0]) + i*i;
}
```

The code correctly returns 55.

--
January 17, 2020
https://issues.dlang.org/show_bug.cgi?id=13575

--- Comment #2 from Witold Baryluk <witold.baryluk+d@gmail.com> ---
Ah, obviously I did forgot to mention that compiler does not warn about my first example.

But it should.

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=13575

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P4

--
November 14, 2023
https://issues.dlang.org/show_bug.cgi?id=13575

Steven Schveighoffer <schveiguy@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |schveiguy@gmail.com
         Resolution|---                         |INVALID

--- Comment #3 from Steven Schveighoffer <schveiguy@gmail.com> ---
You can no longer return from scope(failure)

--