www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Why does this work?

reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
int foo;
enum bar = foo+2;

void main()
{
	foo = 7;
	assert(bar == 9);
}

-- 
Best regards,
  Vladimir                            mailto:vladimir thecybershadow.net
Feb 17 2011
next sibling parent reply Jason House <jason.james.house gmail.com> writes:
Vladimir Panteleev Wrote:

 int foo;
 enum bar = foo+2;
 
 void main()
 {
 	foo = 7;
 	assert(bar == 9);
 }
 
 -- 
 Best regards,
   Vladimir                            mailto:vladimir thecybershadow.net
I would have expected bar to equal 2 since foo would be default initialized to 2. I'm going to guess that the there's some kind of optimization that only assigns to foo once and isn't noticing that bar depends on it. You should probably post that in bugzilla.
Feb 17 2011
next sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Thursday 17 February 2011 21:06:26 Jason House wrote:
 Vladimir Panteleev Wrote:
 int foo;
 enum bar = foo+2;
 
 void main()
 {
 
 	foo = 7;
 	assert(bar == 9);
 
 }
I would have expected bar to equal 2 since foo would be default initialized to 2. I'm going to guess that the there's some kind of optimization that only assigns to foo once and isn't noticing that bar depends on it. You should probably post that in bugzilla.
I believe that the bug here is that bar is allowed to use foo in its initialization. foo isn't immutable or an enum, so that shouldn't work. bar probably gets replaced with foo + 2, which would be perfectly legitimate were foo actually immutable. And the foo + 2 probably doesn't get optimized out like it should be, because foo isn't actually immutable like it would have to be (per the spec) to be used in bar's initialization. This is _definitely_ a bug. - Jonathan M Davis
Feb 17 2011
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 2/17/11 11:22 PM, Jonathan M Davis wrote:
 On Thursday 17 February 2011 21:06:26 Jason House wrote:
 Vladimir Panteleev Wrote:
 int foo;
 enum bar = foo+2;

 void main()
 {

 	foo = 7;
 	assert(bar == 9);

 }
I would have expected bar to equal 2 since foo would be default initialized to 2. I'm going to guess that the there's some kind of optimization that only assigns to foo once and isn't noticing that bar depends on it. You should probably post that in bugzilla.
I believe that the bug here is that bar is allowed to use foo in its initialization. foo isn't immutable or an enum, so that shouldn't work. bar probably gets replaced with foo + 2, which would be perfectly legitimate were foo actually immutable. And the foo + 2 probably doesn't get optimized out like it should be, because foo isn't actually immutable like it would have to be (per the spec) to be used in bar's initialization. This is _definitely_ a bug.
Posted, search bugzilla for "yum". Andrei
Feb 17 2011
prev sibling parent reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Fri, 18 Feb 2011 07:06:26 +0200, Jason House  
<jason.james.house gmail.com> wrote:

 I would have expected bar to equal 2 since foo would be default  
 initialized to 2. I'm going to guess that the there's some kind of  
 optimization that only assigns to foo once and isn't noticing that bar  
 depends on it.
Nope: int foo; enum bar = foo+2; void main() { foo = 7; assert(bar == 9); foo++; assert(bar == 10); }
 You should probably post that in bugzilla.
And complain about basically having expression macros?! -- Best regards, Vladimir mailto:vladimir thecybershadow.net
Feb 17 2011
parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Thursday 17 February 2011 21:23:26 Vladimir Panteleev wrote:
 On Fri, 18 Feb 2011 07:06:26 +0200, Jason House
 
 <jason.james.house gmail.com> wrote:
 I would have expected bar to equal 2 since foo would be default
 initialized to 2. I'm going to guess that the there's some kind of
 optimization that only assigns to foo once and isn't noticing that bar
 depends on it.
Nope: int foo; enum bar = foo+2; void main() { foo = 7; assert(bar == 9); foo++; assert(bar == 10); }
 You should probably post that in bugzilla.
And complain about basically having expression macros?!
It's a bug. No question. - Jonathan M Davis
Feb 17 2011
prev sibling parent Don <nospam nospam.com> writes:
Vladimir Panteleev wrote:
 int foo;
 enum bar = foo+2;
 
 void main()
 {
     foo = 7;
     assert(bar == 9);
 }
 
It's bug 2414 "enum is dynamically evaluated, yum"
Feb 17 2011