Bug 9665 – Structure constant members can not be initialized if have opAssign

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-03-08T02:49:18Z
Last change time
2018-04-02T21:48:04Z
Keywords
pull, rejects-valid
Assigned to
Kenji Hara
Creator
Maksim Zholudev
Blocks
7737, 10357, 11186, 11246
See also
https://issues.dlang.org/show_bug.cgi?id=18708

Comments

Comment #0 by maximzms — 2013-03-08T02:49:18Z
Normally constant members of a structure can be initialized in constructor. However this is not possible if they have overloading of assignment operator. This restricts usage of complex numbers from std.complex since they are structures with opAssign. ------------------- struct Foo1 // opAssign is a function { int value; void opAssign(int src) { value = src; } } struct Foo2 // opAssign is a template { int value; void opAssign()(int src) { value = src; } } struct Boo { const Foo1 f1; const Foo2 f2; this(int src) { f1 = src; // Error! f2 = src; // Error! } } void main() {} ------------------- test.d(20): Error: mutable method test.Foo1.opAssign is not callable using a const object test.d(21): Error: template test.Foo2.opAssign does not match any function template declaration. Candidates are: test.d(10): test.Foo2.opAssign()(int src) test.d(21): Error: template test.Foo2.opAssign()(int src) cannot deduce template function from argument types !()(int) -------------------
Comment #1 by k.hara.pg — 2013-03-09T08:01:49Z
This is unfixable problem, if I'm not mistaken. Currently, compiler always considers opAssign operator overloading for all field "assignment" in constructor. struct S { T field; this(...) { field = xxx; } // If T has opAssign, it is called. } But for non-mutable field, opAssign invocation is not legal, because it may break const correctness. T* p; struct T { void opAssign(int n) { ...; p = &this; } } struct S { immutable T field; this(...) { field = 1; // invoke T.opAssign (currently not allowed) /* now global p holds mutable pointer to immutable T object! */ } } I have no answer for this issue... So, assigned to Andrei.
Comment #2 by andrej.mitrovich — 2013-03-09T09:07:15Z
(In reply to comment #1) > This is unfixable problem, if I'm not mistaken. What if opAssign is const/inout?
Comment #3 by k.hara.pg — 2013-03-09T09:14:18Z
(In reply to comment #2) > (In reply to comment #1) > > This is unfixable problem, if I'm not mistaken. > > What if opAssign is const/inout? It would be invoked, but you cannot do any meaningful operation in it.
Comment #4 by andrej.mitrovich — 2013-03-09T09:16:40Z
(In reply to comment #3) > (In reply to comment #2) > > (In reply to comment #1) > > > This is unfixable problem, if I'm not mistaken. > > > > What if opAssign is const/inout? > > It would be invoked, but you cannot do any meaningful operation in it. I see. Is there any use for a const opAssign?
Comment #5 by rswhite4 — 2013-03-09T09:32:56Z
(In reply to comment #3) > (In reply to comment #2) > > (In reply to comment #1) > > > This is unfixable problem, if I'm not mistaken. > > > > What if opAssign is const/inout? > > It would be invoked, but you cannot do any meaningful operation in it. You can: import std.stdio; import std.c.string : memcpy; struct Foo1 // opAssign is a function { int value; void opAssign(int src) const { int* ptr = cast(int*) &this.value; *ptr = src; } } struct Boo { const Foo1 f1; this(int src) { f1 = src; // Error! } } void main() { Boo b = Boo(42); writeln(b.f1.value); // prints 42 }
Comment #6 by maximzms — 2013-03-09T09:47:58Z
(In reply to comment #1) > But for non-mutable field, opAssign invocation is not legal, because it may > break const correctness. > > T* p; > struct T { > void opAssign(int n) { ...; p = &this; } > } > struct S { > immutable T field; > this(...) { field = 1; // invoke T.opAssign (currently not allowed) > /* now global p holds mutable pointer to immutable T object! */ > } > } Is there any way to break const correctness if opAssign is pure? If not, then changes should be allowed for pure opAssign (e.g. with implicit cast field to mutable).
Comment #7 by k.hara.pg — 2013-03-09T10:29:11Z
(In reply to comment #5) [snip] To enforce breaking type system by language users is not good at all. When talking about language semantics, unsafe operation like cast must not appear. Language user can break type system explicitly, but compiler must not do it. (In reply to comment #6) > Is there any way to break const correctness if opAssign is pure? 'pure' attribute does not affect constancy. > If not, then changes should be allowed for pure opAssign (e.g. with implicit > cast field to mutable). opAssign should modify its 'this' object for the meaningful operation, but mutable method cannot call on non-const object, even inside constructor. So this problem is unfixable.
Comment #8 by maxim — 2013-03-09T11:02:57Z
(In reply to comment #0) > Normally constant members of a structure can be initialized in constructor. > However this is not possible if they have overloading of assignment operator. > > This restricts usage of complex numbers from std.complex since they are > structures with opAssign. > > <skipped> You can do: f2.value = src; which doesn't require opAssign as well as doesn't scale as structure grows. This does not work either when structure has private members.
Comment #9 by andrei — 2013-03-09T17:42:11Z
This is an insufficiency in D's design. I think we should approach this the same way as super() invocation and construction of qualified objects: 1. When a constructor is entered, the object is in a "raw" state. 2. While in this raw state, the object is not considered initialized so it can't be passed to functions etc. Its const/immutable members (be they by their own qualifier or propagated from the object) are also raw and can't be passed out. 3. This raw state lasts until super() has been called EXACTLY ONCE and each qualified field has been assigned to EXACTLY ONCE. 4. At that point the object has become "cooked" and all restrictions are lifted. Kenji, I recall we discussed this design in the context of const and immutable constructors, and you were favorable to it. How about we extend the same design for initializing qualified members in all contexts? We can reuse the same primitive flow analysis that's now used for super().
Comment #10 by andrei — 2013-03-09T17:43:08Z
Assigned to Kenji :o).
Comment #11 by k.hara.pg — 2013-03-09T18:27:27Z
(In reply to comment #9) Thanks for your nice reply! I have recalled description written in TDPL. > Kenji, I recall we discussed this design in the context of const and immutable > constructors, and you were favorable to it. How about we extend the same design > for initializing qualified members in all contexts? I think it is possible. Now I completely understood. We should need some control flow analysis that is similar to calling super() for the process of initializing object field. (In reply to comment #10) > Assigned to Kenji :o). I have received your request!
Comment #12 by andrei — 2013-03-09T18:32:32Z
@Kenji: that's exactly right. Just in case it isn't obvious, that only assignment for qualified fields while in raw state is not an opAssign call, it's a constructor call.
Comment #13 by maxim — 2013-03-10T01:53:08Z
(In reply to comment #9) > This is an insufficiency in D's design. I think we should approach this the > same way as super() invocation and construction of qualified objects This is close to const vs postblit problem. The problem appeared due to unthoughtful decision to allow const objects mutation during construction. But it is too late now. > 3. This raw state lasts until super() has been called EXACTLY ONCE and each > qualified field has been assigned to EXACTLY ONCE. I am afraid such limitation would hurt programming in cases when raw object is touched more than once. By the way, what is about example in comment 1 when non-const opAssign stores mutable pointer to immutable data? > 4. At that point the object has become "cooked" and all restrictions are > lifted. > > Kenji, I recall we discussed this design in the context of const and immutable > constructors, and you were favorable to it. How about we extend the same design > for initializing qualified members in all contexts? > > We can reuse the same primitive flow analysis that's now used for super(). I think it would be better to have special qualifier for such situation. Anyway we implicitly have it. Problem with flow analysis is that callee may have no idea who is caller due to separate compilation.
Comment #14 by andrei — 2013-03-10T12:33:50Z
(In reply to comment #13) > (In reply to comment #9) > > This is an insufficiency in D's design. I think we should approach this the > > same way as super() invocation and construction of qualified objects > > This is close to const vs postblit problem. The problem appeared due to > unthoughtful decision to allow const objects mutation during construction. But > it is too late now. I disagree it's too late, seeing as I actually propose a solution. Also, this is probably not the best forum to air criticisms about the competence and state of mind of the language designers. > > 3. This raw state lasts until super() has been called EXACTLY ONCE and each > > qualified field has been assigned to EXACTLY ONCE. > > I am afraid such limitation would hurt programming in cases when raw object is > touched more than once. I agree it would disallow some correct code, but it's not a strong limitation to ask for exactly one initialization. > By the way, what is about example in comment 1 when non-const opAssign stores > mutable pointer to immutable data? I'm not sure what you mean. What about it? This can't occur in the proposed raw/cooked design. > > 4. At that point the object has become "cooked" and all restrictions are > > lifted. > > > > Kenji, I recall we discussed this design in the context of const and immutable > > constructors, and you were favorable to it. How about we extend the same design > > for initializing qualified members in all contexts? > > > > We can reuse the same primitive flow analysis that's now used for super(). > > I think it would be better to have special qualifier for such situation. Anyway > we implicitly have it. Problem with flow analysis is that callee may have no > idea who is caller due to separate compilation. I disagree. A new qualifier would complicate the language much more than a typechecking rule for constructors. BTW the raw/cooked design is proven - it's been already used in a number of papers and languages.
Comment #15 by maxim — 2013-03-10T13:16:15Z
(In reply to comment #14) > (In reply to comment #13) > > (In reply to comment #9) > > > This is an insufficiency in D's design. I think we should approach this the > > > same way as super() invocation and construction of qualified objects > > > > This is close to const vs postblit problem. The problem appeared due to > > unthoughtful decision to allow const objects mutation during construction. But > > it is too late now. > > I disagree it's too late, seeing as I actually propose a solution. Also, this > is probably not the best forum to air criticisms about the competence and state > of mind of the language designers. The purpose was not to insult language designers but to point that affecting one feature may break its interaction with other features. > > By the way, what is about example in comment 1 when non-const opAssign stores > > mutable pointer to immutable data? > > I'm not sure what you mean. What about it? This can't occur in the proposed > raw/cooked design. > From what I understood, the proposal is to relax(remove temporarily) constness of members during ctor invocation which allows code like below: T* p; struct T { void opAssign(int n) { ...; p = &this; } } struct S { immutable T field; this(...) { field = 1; // invoke T.opAssign (currently not allowed) /* now global p holds mutable pointer to immutable T object! */ } } Or I misunderstood completely and you really proposing to call something like super().
Comment #16 by k.hara.pg — 2013-03-10T20:40:34Z
(In reply to comment #15) > From what I understood, the proposal is to relax(remove temporarily) constness > of members during ctor invocation which allows code like below: > > T* p; > struct T { > void opAssign(int n) { ...; p = &this; } > } > struct S { > immutable T field; > this(...) { field = 1; // invoke T.opAssign (currently not allowed) > /* now global p holds mutable pointer to immutable T object! */ > } > } > > Or I misunderstood completely and you really proposing to call something like > super(). In the raw/cooked design, you cannot call mutable T.opAssign from immutable field, even inside constructor. Instead, you should use whole object "assignment" for the field. struct S { immutable T field; this(...) { field = immutable(T)(...); // T's literal or constructor call // This _looks like_ "assignment" but in practice // it would be treated as initializing. So opAssign is not invoked. } }
Comment #17 by andrei — 2013-05-05T19:00:14Z
So Kenji, did you fix this bug in trunk?
Comment #18 by k.hara.pg — 2013-05-07T04:25:59Z
(In reply to comment #17) > So Kenji, did you fix this bug in trunk? In local I've tried to fix this, but it had broke Phobos compilation by the lack of proper postblit feature. Sorry...
Comment #19 by k.hara.pg — 2013-10-10T05:14:26Z
*** Issue 11204 has been marked as a duplicate of this issue. ***
Comment #20 by samukha — 2013-10-10T07:07:59Z
I see there was a discussion in bug 9665. Kenji, if you are fixing this, please don't forget about postblit, which is a constructor and should have similar semantics. That is: struct S { @disable this(); this(int x) { } static int postblit; this(this) { postblit = true; } static int assigned; void opAssign(S s) { assigned = true; } } class A { S s; this() { auto s1 = S(1); s = s1; assert(!S.assigned); assert(S.postblit); } } void main() { auto a = new A; } Copy-construction should be performed, not assignment.
Comment #21 by samukha — 2013-10-10T07:10:42Z
(In reply to comment #20) "I see there was a discussion in bug 9665." - ignore this sentence.
Comment #22 by k.hara.pg — 2013-10-14T05:58:29Z
Comment #23 by github-bugzilla — 2013-10-14T15:19:11Z
Commits pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/644e11ea46ac671b4a1a34a31ef5e72a6f89a2d2 fix Issue 9665 - Structure constant members can not be initialized if have opAssign https://github.com/D-Programming-Language/phobos/commit/5f725db1141d98c67ee3515f4edb7b43692173d7 Merge pull request #1637 from 9rnsr/fix9665 Supplemental fix for issue 9665 - Structure constant members can not be initialized if have opAssign
Comment #24 by github-bugzilla — 2013-10-14T15:20:11Z
Commit pushed to 2.064 at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/f5606620c5f106694a6c1ec082587d8ffba1a6c7 Merge pull request #1637 from 9rnsr/fix9665 Supplemental fix for issue 9665 - Structure constant members can not be initialized if have opAssign
Comment #25 by github-bugzilla — 2013-10-14T20:51:16Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/f8386c7eeb2518779e42e753507538188c1e3be5 fix Issue 9665 - Structure constant members can not be initialized if have opAssign - Change the first field assignment inside constructor to true initialization. - Disable multiple initialization of non-mutable field, if it is once initialized. https://github.com/D-Programming-Language/dmd/commit/84ee9522f470cdb90031c4a8542c32e753d1462c Merge pull request #2665 from 9rnsr/fix9665 Issue 9665 - Structure constant members can not be initialized if have opAssign
Comment #26 by github-bugzilla — 2013-10-14T20:51:57Z
Commit pushed to 2.064 at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/44d568a7f83a43d6bbd31cbcfcf194da39a3beb7 Merge pull request #2665 from 9rnsr/fix9665 Issue 9665 - Structure constant members can not be initialized if have opAssign
Comment #27 by k.hara.pg — 2013-10-20T02:32:33Z
*** Issue 9732 has been marked as a duplicate of this issue. ***
Comment #28 by github-bugzilla — 2013-11-03T20:47:08Z
Commits pushed to master at https://github.com/D-Programming-Language/dlang.org https://github.com/D-Programming-Language/dlang.org/commit/76bf0a581313aa6f2702f23a577f28d32a5d385f fix Issue 9665 - Structure constant members can not be initialized if have opAssign Describe about field initialization behavior. https://github.com/D-Programming-Language/dlang.org/commit/0af070ef719ef65ddd325dfb5d68b5fd987ce6aa Merge pull request #404 from 9rnsr/fix9665 Issue 9665 - Structure constant members can not be initialized if have opAssign
Comment #29 by github-bugzilla — 2013-11-03T20:49:18Z
Commit pushed to 2.064 at https://github.com/D-Programming-Language/dlang.org https://github.com/D-Programming-Language/dlang.org/commit/101d3a7a229b45bd97bc922f7c4df465f72d2432 Merge pull request #404 from 9rnsr/fix9665 Issue 9665 - Structure constant members can not be initialized if have opAssign
Comment #30 by k.hara.pg — 2013-11-28T20:39:34Z
*** Issue 8118 has been marked as a duplicate of this issue. ***
Comment #31 by k.hara.pg — 2014-05-05T18:43:59Z
*** Issue 11952 has been marked as a duplicate of this issue. ***
Comment #32 by k.hara.pg — 2014-09-22T05:26:17Z
*** Issue 9372 has been marked as a duplicate of this issue. ***