Thread overview
[Issue 8766] New: unexpected compile-time error when switching a struct definition to a class
Oct 06, 2012
Maxim Fomin
Oct 06, 2012
Maxim Fomin
Oct 06, 2012
Walter Bright
Oct 06, 2012
Maxim Fomin
October 06, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8766

           Summary: unexpected compile-time error when switching a struct
                    definition to a class
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: blooh_@hotmail.com


--- Comment #0 from Christopher Crouzet <blooh_@hotmail.com> 2012-10-05 20:01:04 PDT ---
Please consider the code below:

// ------------------------------
enum Storage : int
{
  dynamic = 0
}

enum StorageOrder : int
{
  columnMajor = 0,
  rowMajor = 1
}
alias StorageOrder.columnMajor defaultStorageOrder;


class Array( T_scalar, T_args ... )
{
  alias ArrayTraits!( T_scalar, T_args ) traits;
}


struct ArrayTraits( T_scalar, T_args ... )
{
  static if ( hasFlag( Flags.storageOrder ) == true )
    alias T_args[0 .. $ - 1] shapeTuple;
  else
    alias T_args shapeTuple;

  static immutable StorageOrder storageOrder = hasFlag( Flags.storageOrder ) ?
      T_args[$ - 1] : defaultStorageOrder;


  static int getFlags()
  {
    int flags = 0;
    if ( is( typeof( T_args[$ - 1] ) == StorageOrder ) )
      flags |= Flags.storageOrder;

    return flags;
  }

  static bool hasFlag( Flags flag )
  {
    return (getFlags() & flag) != 0;
  }


  enum Flags : int
  {
    dynamic = 1 << 0,
    storageOrder = 1 << 1
  }
}


void main()
{
  auto array1d = new Array!( float, 3 );
}
// ------------------------------

It all compiles fine as it is and produce the expected results.

Now, if you declare ArrayTraits as a class rather than a struct, then the compilation fails saying that the variable 'flag' cannot be read at compile time. I might be wrong, but when comparing the specs of classes and structs from the doc below, I don't see anything that would explain this error happenning only in one case but not the other? http://dlang.org/struct.html

Also, if you keep it declared as a class but this time define the hasFlag()
arguments at conpile-time, such as
static bool hasFlag( Flags flag )()
and adequately replace the calls to this method, then it compiles fine. I'm
getting confused here and am not sure anymore if how I initially declared it is
valid or not?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 06, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8766


Maxim Fomin <maxim@maxim-fomin.ru> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |maxim@maxim-fomin.ru


--- Comment #1 from Maxim Fomin <maxim@maxim-fomin.ru> 2012-10-06 02:33:37 PDT ---
Reduced http://dpaste.dzfl.pl/348de450

If integer constant 1 is replaced by any of StorageOrder enumeration, the bug goes away. Also if empty static if statement is cut off completely, the bug also goes away.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 06, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8766


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs@eml.cc


--- Comment #2 from bearophile_hugs@eml.cc 2012-10-06 03:20:29 PDT ---
(In reply to comment #1)
> Reduced http://dpaste.dzfl.pl/348de450

Note that enums in D don't need a final semicolon. And it's better to paste or attach the test cases in Bugzilla itself, instead of linking an external paste site.

-----------------------

A little reduced:


enum Foo { X }
class Bar {
    static if (spam(Foo.X)) {}
    static Foo a = spam(Foo.X) ? 1 : Foo.X;
    static bool spam(Foo flag) {
        return flag != 0;
    }
}
void main() {}

DMD 2.061alpha gives:

temp.d(6): Error: variable flag cannot be read at compile time
temp.d(4):        called from here: spam(cast(Foo)0)

Replacing class with struct the error goes away.

-----------------------

Also this makes the error go away, showing it's related to a forward reference error:

enum Foo { X }
class Bar {
    static bool spam(Foo flag) {
        return flag != 0;
    }
    static if (spam(Foo.X)) {}
    static Foo a = spam(Foo.X) ? 1 : Foo.X;
}
void main() {}

-----------------------

While this:

enum Foo { X }
class Bar {
    static if (spam(Foo.X)) {}
    static Foo a = spam(Foo.X) ? 1 : Foo.X;
    static bool spam(Foo flag) {
        return true;
    }
}
void main() {}


Gives:

temp.d(4): Error: cannot implicitly convert expression (1) of type int to Foo

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 06, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8766



--- Comment #3 from Maxim Fomin <maxim@maxim-fomin.ru> 2012-10-06 04:02:43 PDT ---
(In reply to comment #2)
> (In reply to comment #1)
> > Reduced http://dpaste.dzfl.pl/348de450
> 
> And it's better to paste or
> attach the test cases in Bugzilla itself, instead of linking an external paste
> site.

Dpaste is more convenient and it is supposed to be "D community" resource rather some external paste site. The only problem I can see is that it can be shut down when it's content is required. Is dpaste considered as very unreliable or does often experience problems? I faced it only once.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 06, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8766


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@digitalmars.com


--- Comment #4 from Walter Bright <bugzilla@digitalmars.com> 2012-10-06 05:08:43 PDT ---
Please paste source code reproducing a bug inline here, or attach as a file to the bugzilla issue.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 06, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8766



--- Comment #5 from Maxim Fomin <maxim@maxim-fomin.ru> 2012-10-06 05:47:03 PDT ---
(In reply to comment #4)
> Please paste source code reproducing a bug inline here, or attach as a file to the bugzilla issue.

OK

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------