www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 15302] New: DMD -O optimizing out meaningful code


          Issue ID: 15302
           Summary: DMD -O optimizing out meaningful code
           Product: D
           Version: D2
          Hardware: x86
                OS: All
            Status: NEW
          Severity: major
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: ketmar ketmar.no-ip.org

compiling the following code with "dmd -O" leading to failed assert. seems that
dmd optimized out "lineNumber++;", failing to see that it is used in "catch"


class XException : Exception {
  this (string msg, size_t lineNumber, string file=__FILE__, size_t
line=__LINE__, Throwable next=null) pure nothrow  safe {
    super(msg, file, line, next);
    _lineNumber = lineNumber;
   nogc  safe size_t lineNumber() const nothrow {
        return _lineNumber;
  size_t _lineNumber;

void foo (string[] lines) {
  size_t lineNumber = 0;
  try {
    foreach (line; lines) {
      if (line == "Line3") throw new Exception("!");
  } catch (Exception e) {
    throw new XException(e.msg, lineNumber, e.file, e.line, e.next);

void main () {
  try {
    foo(["Line", "AnotherLine", "Line3"]);
  } catch (XException e) {
    assert(e.lineNumber == 3);

Nov 08 2015