beluga icon indicating copy to clipboard operation
beluga copied to clipboard

fix incorrect "uninitialized reference" problem

Open mycoboco opened this issue 9 years ago • 4 comments

mycoboco avatar Dec 14 '15 06:12 mycoboco

This issue is because the order in which an expression appers in code differs from that in which it is evaluated. For example, given this:

for (p = q; p < q+n; p = r)
    r = next();

when beluga encounters the third expression, it mistakenly issues a warning that r is referenced uninitialized.

Probably I have to give this kind of detection up until a more formal form of control flow analysis is introduced.

mycoboco avatar Dec 14 '15 11:12 mycoboco

I'll have to look at the source code, but isn't it possible to just put the third expression on a stack that would then be popped once the scope ends? You basically need a pointer on whatever scope abstraction there is and delay the detection to there.

ghost avatar Dec 19 '15 09:12 ghost

Thanks for your comment.

Sounds like a nice approach. The parse tree from for's last expression can be remembered and used to check for uninitialized access after parsing the loop body, as you said. However, without tracking of control flow, it is not easy, for example, to say that i is referenced after being initialized in this contrived example:

int j, i;

goto label1;
label3:
j = i;
goto label2;
label1:
i = 0;
goto label3;
label2:
;

The diagnostic for "uninitialized reference" was mistakenly included when I designed code to issue similar warnings, "defined but not used" and "set but not used". Rather introducing a workaround for a specific case, preparing a method to traverse parse trees following control flow would allow other useful diagnostics not to mention fixing the problem.

mycoboco avatar Dec 21 '15 07:12 mycoboco

Temporarily removed the check for uninitialized references.

mycoboco avatar Dec 25 '15 07:12 mycoboco