D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6564 - enum with no initializer compiles
Summary: enum with no initializer compiles
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2011-08-27 16:41 UTC by Andrej Mitrovic
Modified: 2012-01-30 22:38 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrej Mitrovic 2011-08-27 16:41:33 UTC
enum Length;

void main()
{
    int[Length] a;
    auto b = a;
}

Notice I forgot to declare what Length is, I might have wanted to declare "enum Length = 5".

This creates a whole host of errors depending on how you use the code, e.g.:

void main()
{
    int[Length] a;
    auto b = a;
    
    writeln(b);
}

Error: enum test.Length is forward referenced when looking for 'stringof'

Or:

import std.stdio;
enum Length;
void main()
{
    int[Length] a;
    auto b = a;
    
    writeln(b.length);
}

Error: Assertion failure: 'impl' on line 4080 in file 'mtype.c'
abnormal program termination

Or:

import std.stdio;
enum Length;
void main()
{
    writeln(Length);
}

D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\stdio.d(1483): Error: forward reference of Length.init
D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\stdio.d(1495): Error: forward reference of Length.init
D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\stdio.d(1508): Error: forward reference of Length.init

Anyway, the first example should not compile.
Comment 1 yebblies 2012-01-30 21:59:16 UTC
enum Length; is a forward declaration to an enum type defined elsewhere.

eg.
--- x.d ---
enum Length
{
 ... members ...
}

--- x.di ---
enum Length;

Not sure this is the greatest thing to allow, but it is correct according to the spec.  Please open an enhancement request to disallow forward declarations of enums is you think this is desirable.
Comment 2 Andrej Mitrovic 2012-01-30 22:19:30 UTC
(In reply to comment #1)
> --- x.di ---
> enum Length;

That's not generated by the compiler (at least not 2.057, I don't know about 2.058 HEAD). But how does it make sense to hide enum members anyway? I've never seen D code that uses a forward enum declaration.
Comment 3 yebblies 2012-01-30 22:25:22 UTC
(In reply to comment #2)
> That's not generated by the compiler (at least not 2.057, I don't know about
> 2.058 HEAD).

It doesn't get generated by the compiler, but you can still do it.

> But how does it make sense to hide enum members anyway? 

An enum declaration creates its own type, so in the same kind of places you'd want to pass around an opaque struct pointer.

> I've never seen D code that uses a forward enum declaration.

Neither have I, but I've seen plenty of C/C++ source that does it. eg dmd
Comment 4 Andrej Mitrovic 2012-01-30 22:38:43 UTC
(In reply to comment #3)
> An enum declaration creates its own type, so in the same kind of places you'd
> want to pass around an opaque struct pointer.

Like "struct Foo;", I see. I've seen opaque structs before but never opaque enums. :)