[Issue 13575] Unreachable scope(failure) should be warned

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Jan 17 16:28:46 UTC 2020


https://issues.dlang.org/show_bug.cgi?id=13575

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

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

--- Comment #1 from Witold Baryluk <witold.baryluk+d at 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.

--


More information about the Digitalmars-d-bugs mailing list