After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 574352 - variable initialized in do-while loop not recognized.
variable initialized in do-while loop not recognized.
Status: RESOLVED OBSOLETE
Product: vala
Classification: Core
Component: Semantic Analyzer
0.22.x
Other All
: Normal normal
: ---
Assigned To: Vala maintainers
Vala maintainers
: 601340 645771 (view as bug list)
Depends on:
Blocks:
 
 
Reported: 2009-03-06 05:53 UTC by rainwoodman
Modified: 2018-05-22 13:18 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
simplify the do statement (2.33 KB, patch)
2010-01-24 16:11 UTC, Luca Bruno
rejected Details | Review
Fix do-while statement flow analysis. (3.27 KB, patch)
2010-07-23 09:58 UTC, Luca Bruno
reviewed Details | Review

Description rainwoodman 2009-03-06 05:53:03 UTC
Please describe the problem:
If a variable is initialized in a do-while loop iteration (which is guaranteed to be executed at least once), the symantic analyzer still complains the variable is not initialized.


Steps to reproduce:
  public virtual Vector sample(bool open) {
┊   ┊   ┊   double x, y, z;
┊   ┊   ┊   double r;
┊   ┊   ┊   Vector point;
┊   ┊   ┊   Sense s = Sense.OUT;
┊   ┊   ┊   do{
┊   ┊   ┊   ┊   Gsl.Randist.dir_3d( rng, out x, out y, out z);
┊   ┊   ┊   ┊   r = rng.uniform();
┊   ┊   ┊   ┊   r = cbrt(r) * bounding_radius;
┊   ┊   ┊   ┊   point = Vector(x * r, y * r, z * r);
┊   ┊   ┊   ┊   body_to_world(ref point);
┊   ┊   ┊   ┊   s = sense(point);
┊   ┊   ┊   } while(s == Sense.OUT || (open && s == Sense.ON));
┊   ┊   ┊   return point;
┊   ┊   }


Actual results:
Compilation failure with error possibly not initialized variable point.

Expected results:


Does this happen every time?


Other information:
the work around is to set point when define it. But this would be a duplication of work since it is going to be initizized in the do-while loop.
Comment 1 Luca Bruno 2010-01-24 16:11:00 UTC
Created attachment 152156 [details] [review]
simplify the do statement

The do statement currently check for the condition at the beginning of the loop. This makes the control flow analyzer work harder to understand that the first time the body of the loop is being executed.

This patch checks for the condition at the end of the loop, which looks like more natural to me for the control flow. I don't know if this will break anything else, but tests don't fail and the generated C code is even easier to read.
Comment 2 Jürg Billeter 2010-01-24 21:10:10 UTC
Doesn't this break continue statements?
Comment 3 Luca Bruno 2010-01-24 22:04:48 UTC
(In reply to comment #2)
> Doesn't this break continue statements?

You're right, I'm sorry. What about adding an extra property like Loop.is_do_loop or Loop.reaches_end to always connect the last block of the loop body to the block after the loop?
Comment 4 Jürg Billeter 2010-03-23 07:21:06 UTC
*** Bug 601340 has been marked as a duplicate of this bug. ***
Comment 5 Jürg Billeter 2010-03-23 07:39:11 UTC
The proper way to solve this is to rewrite the loop with goto statements internally, as far as I can tell. This would avoid the problematic 'first' variable that gets generated now when rewriting the do loop with a while (true) loop.
Comment 6 zarevucky.jiri 2010-07-22 02:59:35 UTC
I think the proper way to solve this is to do all the semantic and control flow analysis before breaking the code apart and rewriting it. It would also make some of the code more easily understandable.
Comment 7 Jürg Billeter 2010-07-22 12:02:25 UTC
This would introduce more complexity into flow analysis as it would require the flow analyzer to deal with error handling and reachability analysis within (conditional) subexpressions.

Simplifying the code tree as early as possible sounds like the best approach to me. Why do you think it would be better to move the simplifications to a later stage?
Comment 8 zarevucky.jiri 2010-07-23 02:13:55 UTC
Because, as can be seen in this bug, sometimes the simplifications loose some of the semantic meaning the code has. So the possibilities are to either have buggy analysis, or to figure out some very special, probably very ugly, way to do those simplifications, which in the end is (in my opinion) more trouble than making flow analysis smarter.
Comment 9 Luca Bruno 2010-07-23 09:58:42 UTC
Created attachment 166434 [details] [review]
Fix do-while statement flow analysis.

Fixes bug 574352.
Comment 10 Jürg Billeter 2010-08-04 14:04:56 UTC
Comment on attachment 166434 [details] [review]
Fix do-while statement flow analysis.

As discussed on IRC, the plan is to change the code generator in a way that makes it possible to drop the code simplifications in the Vala AST completely.
Comment 11 Maciej (Matthew) Piechotka 2011-04-30 10:58:59 UTC
*** Bug 645771 has been marked as a duplicate of this bug. ***
Comment 12 GNOME Infrastructure Team 2018-05-22 13:18:48 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to GNOME's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/vala/issues/27.