www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 13471] New: CTFE glitch when executing

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

          Issue ID: 13471
           Summary: CTFE glitch when executing std.digest.crc.crc32Of()
                    and checking the result with enforce(). (keyword:
                    uninitialized variable)
           Product: D
           Version: D2
          Hardware: x86_64
                OS: All
            Status: NEW
          Keywords: CTFE
          Severity: normal
          Priority: P1
         Component: DMD
          Assignee: nobody puremagic.com
          Reporter: Marco.Leise gmx.de

This snipped shows some strange behavoir:

    import std.digest.crc; 
    import std.exception;

    enum buggy1 = foo("Hello World!");
    enum buggy2 = crc32Of("Hello World!");

    ubyte[4] foo(string str) 
    {
        ubyte[4] result = str.crc32Of();
        enforce (result != (ubyte[4]).init, "this should not be thrown");
        return result;
    }

When we look at `buggy2` the error message is hinting four times at some
uninitialized variable, although we don't know where exactly that is:

  main.d(5): Error: uninitialized variable 'value' cannot be returned from CTFE
  main.d(5): Error: uninitialized variable 'value' cannot be returned from CTFE
  main.d(5): Error: uninitialized variable 'value' cannot be returned from CTFE
  main.d(5): Error: uninitialized variable 'value' cannot be returned from CTFE

Now in the case of `buggy1` we do the same inside foo(), but check the result
in the next line. It shows that although CTFE knows it cannot complete the
crc32Of() call it continues on with a default initialized `result`:

  /opt/dmd-2.066/import/std/bitmanip.d(1796): Error: Integer constant
expression expected instead of void
  /opt/dmd-2.066/import/std/bitmanip.d(1796): Error: Integer constant
expression expected instead of void
  /opt/dmd-2.066/import/std/bitmanip.d(1796): Error: Integer constant
expression expected instead of void
  /opt/dmd-2.066/import/std/bitmanip.d(1796): Error: Integer constant
expression expected instead of void
  /opt/dmd-2.066/import/std/exception.d(374): Error: Uncaught CTFE exception
object.Exception("this should not be thrown")
  main.d(4):        called from here: foo("Hello World!")

[Reproducible at least on 2.065 and 2.066]

--
Sep 13 2014