D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3488 - Segfault(expression.c): enum declared with struct static initializer
Summary: Segfault(expression.c): enum declared with struct static initializer
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: ice-on-valid-code, patch
Depends on:
Blocks:
 
Reported: 2009-11-08 08:30 UTC by Don
Modified: 2015-06-09 01:27 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Don 2009-11-08 08:30:45 UTC
Reported by g in d.learn.
Segfaults in expression.c. D2 only.
--
struct Move{
	int Dx;
}
template genMove(){
    enum Move genMove = { Dx:4 };
}
enum Move b = genMove!();
Comment 1 g 2009-11-08 10:51:30 UTC
also to note that this also don't works:
--

struct A{
    int n;
}
template genA(){
    enum A genA = { n:4 };
}
immutable A b = genA!();

--

but this works:

--

struct A{
    int n;
}
template genA(){
    //works with immutable
    immutable  A genA = { n:4 };
}
immutable A b = genA!();

void main(){
    //ok
    assert(b.n == 4);
}
--
Comment 2 Don 2009-11-08 11:32:12 UTC
Further reduced test case shows it doesn't need a template.  It's just an enum static initializer problem.
---
struct Move{
   int Dx;
}
enum Move genMove = { Dx:4 };
immutable Move b = genMove;
---
Comment 3 Don 2009-11-09 03:24:21 UTC
It's happening because static struct initializers with names are not evaluated at compile time. And this is because init.c, StructInitializer::toExpression() doesn't deal with it. BTW -- now that we have struct literals, I'm not sure that we need struct initializers any more. They're a bit annoying, implementation-wise.

--- TEST CASE ---
struct Move{ int Dx; }
immutable Move genMove = { Dx:4};
static assert(genMove.Dx == 4); // not evaluatable at compile time.
Comment 4 Don 2009-11-23 00:23:22 UTC
PATCH: With the demise of struct initializers, it's not worth fixing properly.
But DsymbolExp::semantic() should check for a null value anyway (it checks in other places in expression.c). This would prevent the segfault.

expression.c line 2306 (svn 267):

	if ((v->storage_class & STCmanifest) && v->init)
	{
	    e = v->init->toExpression();
+  	    if (!e)
+	    {   error("cannot make expression out of initializer for %s", v->toChars());
+		e = new ErrorExp();
+	    }
	    e->semantic(sc);
	    return e;
	}
Comment 5 Walter Bright 2010-01-23 01:19:02 UTC
Changeset 349
Comment 6 Walter Bright 2010-01-30 22:44:36 UTC
fixed dmd 2.040