[Bug c++/48483] Construct from yourself w/o warning
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=48483 Paolo Carlini paolo.carlini at oracle dot com changed: What|Removed |Added CC||lundberj at gmail dot com --- Comment #21 from Paolo Carlini paolo.carlini at oracle dot com --- *** Bug 57758 has been marked as a duplicate of this bug. ***
[Bug c++/48483] Construct from yourself w/o warning
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48483 --- Comment #17 from Manuel López-Ibáñez manu at gcc dot gnu.org 2012-02-09 11:35:27 UTC --- Clang++ 3.0 warns with -Wuninitialized /tmp/webcompile/_15338_0.cc:7:5: warning: variable 'a' is uninitialized when used within its own initialization [-Wuninitialized] A a(a.i); ~ ^ 1 warning generated. but it doesn't warn for the testcase in comment #2.
[Bug c++/48483] Construct from yourself w/o warning
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48483 --- Comment #18 from Lisp2D lisp2d at lisp2d dot net 2012-02-09 16:59:19 UTC --- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48483#c2 shows that function calling of object before constructor is bad idea and must be strongly forbidden.
[Bug c++/48483] Construct from yourself w/o warning
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48483 --- Comment #19 from Jonathan Wakely redi at gcc dot gnu.org 2012-02-09 17:04:06 UTC --- Everyone knows it's a bad idea, and everyone agrees there should be a warning. Stop going on about it.
[Bug c++/48483] Construct from yourself w/o warning
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48483 --- Comment #20 from Manuel López-Ibáñez manu at gcc dot gnu.org 2012-02-09 21:37:38 UTC --- (In reply to comment #18) http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48483#c2 shows that function calling of object before constructor is bad idea and must be strongly forbidden. Lisp2D, I think you could start with the testcase in comment 16, and try to figure out what would be needed to detect this case, as a firt step. All the code you need to look is in the C++ FE. Use gdb and follow the code. See also http://gcc.gnu.org/contribute.html and http://gcc.gnu.org/wiki/GettingStarted But if you expect that someone else is going to fix this for you, I think you may wait a long time. There are many bugs to fix.
[Bug c++/48483] Construct from yourself w/o warning
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48483 --- Comment #8 from Dmitry Gorbachev d.g.gorbachev at gmail dot com 2011-04-07 06:20:24 UTC --- There is a call to a non-static member function of the object before initialization by a constructor. That's undefined behavior. Not a compiler bug.
[Bug c++/48483] Construct from yourself w/o warning
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48483 Jonathan Wakely redi at gcc dot gnu.org changed: What|Removed |Added Keywords||diagnostic --- Comment #9 from Jonathan Wakely redi at gcc dot gnu.org 2011-04-07 09:41:55 UTC --- (In reply to comment #7) The example http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48483#c2 shows a compiler bug. No it doesn't. TYPE VARIABLE [ARGUMENT-TO-CONSTRUCT] The compiler must doing like this: 1. Compile ARGUMENT-TO-CONSTRUCT 2. Checking of TYPE 3. VARIABLE declaration No, the variable is in scope after its identifier, so it can be used in the initializer expression, e.g. int i = sizeof(i); // ok int i = i+1; // not ok The BIG mistake is declaration of VARIABLE from wrong position. No, C++ does not work the way you think it does. It's not a bug. It would be nice to get a warning, but it's not easy to do that.
[Bug c++/48483] Construct from yourself w/o warning
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48483 --- Comment #10 from Lisp2D lisp2d at lisp2d dot net 2011-04-07 13:19:16 UTC --- (In reply to comment #9) No, the variable is in scope after its identifier, so it can be used in the initializer expression, e.g. int i = sizeof(i); // ok int i = i+1; // not ok My opinion is that C habit rules here. In C int i=i+1; - int i; i=i+1; without constructors, objects e.t.c. This example will bring just a warning. In C++ Class i(i.Method()); eval i.Method() - construct with Class::Class(result) another order of calculation. (see comment 2) Must be an error. The answer of question gives the C++ standard. Show me this document.
[Bug c++/48483] Construct from yourself w/o warning
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48483 --- Comment #11 from Jonathan Wakely redi at gcc dot gnu.org 2011-04-07 13:26:16 UTC --- (In reply to comment #10) The answer of question gives the C++ standard. Show me this document. 3.3.1 Point of declaration [basic.scope.decl] 1 The point of declaration for a name is immediately after its complete declarator (clause 8) and before its initializer (if any), except as noted below. [Example: int x = 12; { int x = x; } Here the second x is initialized with its own (indeterminate) value. ]
[Bug c++/48483] Construct from yourself w/o warning
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48483 --- Comment #12 from Jonathan Wakely redi at gcc dot gnu.org 2011-04-07 13:29:43 UTC --- For the example in comment 2 G++, EDG and Clang++ all accept it without warning. MSVC rejects it, but is wrong to do so.
[Bug c++/48483] Construct from yourself w/o warning
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48483 --- Comment #13 from Jonathan Wakely redi at gcc dot gnu.org 2011-04-07 13:38:11 UTC --- (In reply to comment #10) (In reply to comment #9) No, the variable is in scope after its identifier, so it can be used in the initializer expression, e.g. int i = sizeof(i); // ok int i = i+1; // not ok My opinion is that C habit rules here. In C int i=i+1; - int i; i=i+1; In C++ this can be written: int i(i+1); The initialization rules for C++ do without constructors, objects e.t.c. This example will bring just a warning. In C++ Class i(i.Method()); eval i.Method() - construct with Class::Class(result) Note that this is similar to: Class i = i.Method(); For the purposes of your complaint it's not relevant that it's a class with a constructor. Obviously i.Method() has to be evaluated first at runtime, but that doesn't make it a compile-time error. i is in scope as soon a Class i has been parsed, so the expression i.Method() in the initializer can be compiled. another order of calculation. (see comment 2) Must be an error. No. The behaviour at runtime is undefined, but it is not an error at compile time - the syntax is valid, but the result is undefined. I understand what you're saying, but you're wrong. C++ doesn't work how you think it works.
[Bug c++/48483] Construct from yourself w/o warning
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48483 --- Comment #14 from Jonathan Wakely redi at gcc dot gnu.org 2011-04-07 13:39:45 UTC --- (In reply to comment #13) (In reply to comment #10) (In reply to comment #9) No, the variable is in scope after its identifier, so it can be used in the initializer expression, e.g. int i = sizeof(i); // ok int i = i+1; // not ok My opinion is that C habit rules here. In C int i=i+1; - int i; i=i+1; In C++ this can be written: int i(i+1); The initialization rules for C++ do ... not care if the type is a class or not, the variable name is still in scope before the initializer is parsed.
[Bug c++/48483] Construct from yourself w/o warning
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48483 --- Comment #15 from Lisp2D lisp2d at lisp2d dot net 2011-04-07 13:58:38 UTC --- (In reply to comment #12) For the example in comment 2 G++, EDG and Clang++ all accept it without warning. MSVC rejects it, but is wrong to do so. The answer is good. Let's talk about warnings. I think that processing option -Winit-self must to be rewritten. Just create SymbolVariableProcessingInConstructor and checking of all symbols in line. This will free the brain.
[Bug c++/48483] Construct from yourself w/o warning
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48483 Jonathan Wakely redi at gcc dot gnu.org changed: What|Removed |Added Status|UNCONFIRMED |NEW Last reconfirmed||2011.04.07 14:01:09 Ever Confirmed|0 |1 Severity|normal |enhancement --- Comment #16 from Jonathan Wakely redi at gcc dot gnu.org 2011-04-07 14:01:09 UTC --- Confirming as an enhancement request for a warning from: struct A { A(int); int i; }; A a(a.i); -Winit-self doesn't work properly for C++, this is a known issue.
[Bug c++/48483] Construct from yourself w/o warning
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48483 --- Comment #1 from Dmitry Gorbachev d.g.gorbachev at gmail dot com 2011-04-06 19:20:02 UTC --- No warnings and deep stillness. $ g++ -O -Wuninitialized pr48483.cc pr48483.cc: In function 'int main()': pr48483.cc:13:30: warning: 'a.A::b' is used uninitialized in this function From http://gcc.gnu.org/onlinedocs/gcc-4.5.2/gcc/Warning-Options.html#index-Wuninitialized-309: Because these warnings depend on optimization, the exact variables or elements for which there are warnings will depend on the precise optimization options and version of GCC used.
[Bug c++/48483] Construct from yourself w/o warning
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48483 --- Comment #2 from Lisp2D lisp2d at lisp2d dot net 2011-04-06 21:15:42 UTC --- Try the next example, more close to my code: #includeiostream classA{ public: inta; A(intx):a(x){} intTheInt(){returna;} }; voidFunc(Aa){ std::cloga.a=a.astd::endl;} intmain(void){ Aa(a.TheInt()); Func(a);} g++ -O -Winit-self -obug -Wall -Wextra bug.cpp no warning
[Bug c++/48483] Construct from yourself w/o warning
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48483 Dmitry Gorbachev d.g.gorbachev at gmail dot com changed: What|Removed |Added CC||d.g.gorbachev at gmail dot ||com --- Comment #3 from Dmitry Gorbachev d.g.gorbachev at gmail dot com 2011-04-06 22:38:20 UTC --- Simpler testcase: int main(void) { struct S { int a; } s; return s.a; } It's a regression (latest working version is 4.4). I think there is a duplicate PR somewhere.
[Bug c++/48483] Construct from yourself w/o warning
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48483 Dmitry Gorbachev d.g.gorbachev at gmail dot com changed: What|Removed |Added CC||manu at gcc dot gnu.org --- Comment #4 from Dmitry Gorbachev d.g.gorbachev at gmail dot com 2011-04-06 22:57:57 UTC --- It's similar to bug 42905.
[Bug c++/48483] Construct from yourself w/o warning
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48483 --- Comment #5 from Jonathan Wakely redi at gcc dot gnu.org 2011-04-06 23:50:38 UTC --- and PR 18016 but I don't think my patch will catch this there's a limit to how much silliness the compiler can catch, at some point you have to just not write silly code ;)
[Bug c++/48483] Construct from yourself w/o warning
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48483 --- Comment #6 from Dmitry Gorbachev d.g.gorbachev at gmail dot com 2011-04-07 01:38:53 UTC --- there's a limit to how much silliness the compiler can catch, at some point you have to just not write silly code ;) Yes, these reduced fragments look embarrassing. Of course, compiler is not a tool for static checking. So, it's simply a small enhancement request, rather then a complain about bugs in GCC. GCC devs should concentrate efforts on real bugs. Still, it would be good if somebody will fix this thing.
[Bug c++/48483] Construct from yourself w/o warning
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48483 --- Comment #7 from Lisp2D lisp2d at lisp2d dot net 2011-04-07 04:33:52 UTC --- The example http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48483#c2 shows a compiler bug. TYPE VARIABLE [ARGUMENT-TO-CONSTRUCT] The compiler must doing like this: 1. Compile ARGUMENT-TO-CONSTRUCT 2. Checking of TYPE 3. VARIABLE declaration The BIG mistake is declaration of VARIABLE from wrong position.