1262 – Local variable of struct type initialized by literal resets when compared to .init

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 1262 - Local variable of struct type initialized by literal resets when compared to .init
Summary: Local variable of struct type initialized by literal resets when compared to ...
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: x86 Linux
: P2 normal
Assignee: Walter Bright
URL:
Keywords: wrong-code
Depends on:
Blocks:
Reported: 2007年06月08日 07:03 UTC by Frits van Bommel
Modified: 2014年02月16日 15:23 UTC (History)
0 users

See Also:


Attachments
Add an attachment (proposed patch, testcase, etc.)

Note You need to log in before you can comment on or make changes to this issue.
Description Frits van Bommel 2007年06月08日 07:03:32 UTC
(First seen in a message posted by "HATA" to d.D.bugs, http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.bugs&article_id=11355)
---
import std.stdio;
struct A { int v; }
void main() {
 A a = A(10);
 
 writefln("Before test 1: ", a.v);
 if (a == a.init) writefln(a.v,"(a==a.init)");
 else writefln(a.v,"(a!=a.init)");
 
 a.v = 100;
 writefln("Before test 2: ", a.v);
 if (a == a.init) writefln(a.v,"(a==a.init)");
 else writefln(a.v,"(a!=a.init)");
 
 a = A(1000);
 writefln("Before test 3: ", a.v);
 if (a == a.init) writefln(a.v,"(a==a.init)");
 else writefln(a.v,"(a!=a.init)");
}
---
Even though the value of a.v is not 10 before the last two if statements, it gets set to 10 right before the test.
This doesn't happen if 'a' is a global variable, or if a static opCall(int) with traditional implementation is added. So this is a workaround:
---
struct A {
 int v;
 static A opCall(int x) {
	A result;
	result.v = x;
	return result;
 }
}
---
Comment 1 david 2007年06月12日 11:10:30 UTC
umm, struct literals should not be l-value
Comment 2 Frits van Bommel 2007年06月12日 12:13:51 UTC
(In reply to comment #1)
> umm, struct literals should not be l-value
And your point is?
At no point in the code is a struct literal used as an l-value. There's a struct variable initialized _by_ a struct literal, and a struct literal being assigned to a variable, but those are the only struct literals used. And those operations should only copy the value of the struct literal to the variable being initialized, which should be perfectly fine.
Structs are value types, not reference types.
Comment 3 david 2007年06月12日 20:55:55 UTC
heh, you are right. Literal things screw up in my mind
Comment 4 Walter Bright 2007年06月27日 20:58:49 UTC
Fixed dmd 2.001, 1.017
Comment 6 github-bugzilla 2013年10月15日 13:03:43 UTC
Commit pushed to master at https://github.com/D-Programming-Language/phobos
https://github.com/D-Programming-Language/phobos/commit/8f00475ef27cd6382500b8296ae7f23ed3c8fe16
too tight constraint that has no use in private function
Fixes issue 1262 


AltStyle によって変換されたページ (->オリジナル) /