Bug 8766 – unexpected compile-time error when switching a struct definition to a class
Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-10-05T20:01:00Z
Last change time
2013-11-24T04:37:34Z
Assigned to
nobody
Creator
blooh_
Comments
Comment #0 by blooh_ — 2012-10-05T20:01:04Z
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?
Comment #1 by maxim — 2012-10-06T02:33:37Z
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.
Comment #2 by bearophile_hugs — 2012-10-06T03:20:29Z
(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
Comment #3 by maxim — 2012-10-06T04:02:43Z
(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.
Comment #4 by bugzilla — 2012-10-06T05:08:43Z
Please paste source code reproducing a bug inline here, or attach as a file to the bugzilla issue.
Comment #5 by maxim — 2012-10-06T05:47:03Z
(In reply to comment #4)
> Please paste source code reproducing a bug inline here, or attach as a file to
> the bugzilla issue.
OK