Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Forward references in local variable declarations

40 views
Skip to first unread message

Mark Rada

unread,
Mar 17, 2014, 3:12:23 PM3/17/14
to
Reviewing tests from last week, I am unable to find any tests that check
for forward references of local variable declarations that check if the
initializer for a local variable uses itself.

Well, not quite true, I found one test,
Je_5_AmbiguousInvoke_LocalInOwnInitializer, but it is supposed to fail
for a different reason and is not clear on the Joos behaviour.

The JLS allows a variable to be used in its own initializer, but I am
guessing that is because full Java would also perform the definite
assignment check, which Joos does not include.

This test case would demonstrate the situation:

public class test {
public test() {
int a = a = 42;
}
}

My compiler fails to allow this, but doesn't fail any type checking
tests, so I don't think this case is covered by the Joos delta spec and
it is not in the assignment 3 spec either.

So, can we assume that this will not happen, or should we be performing
the same forward reference check on locals that we do for fields?

olh...@uwaterloo.ca

unread,
Mar 17, 2014, 5:05:08 PM3/17/14
to
On Mon, Mar 17, 2014 at 03:12:23PM -0400, Mark Rada wrote:
> Reviewing tests from last week, I am unable to find any tests that check for
> forward references of local variable declarations that check if the
> initializer for a local variable uses itself.

This is tested in A4, not A3. The reason is that in Java, definite
assignment for local variables is a static analysis. Although you
don't have to do the static analysis for Joos, the tests are
included in A4.

For example, there is the A4 test
Je_8_DefiniteAssignment_InitToItself.

> Well, not quite true, I found one test,
> Je_5_AmbiguousInvoke_LocalInOwnInitializer, but it is supposed to fail for a
> different reason and is not clear on the Joos behaviour.
>
> The JLS allows a variable to be used in its own initializer, but I am
> guessing that is because full Java would also perform the definite
> assignment check, which Joos does not include.
>
> This test case would demonstrate the situation:
>
> public class test {
> public test() {
> int a = a = 42;
> }
> }
>
> My compiler fails to allow this, but doesn't fail any type checking tests,
> so I don't think this case is covered by the Joos delta spec and it is not
> in the assignment 3 spec either.

This kind of thing is forbidden by the "local variable in own
initializer" entry in the Joos specification table.

Your Joos compiler should give a compile-time error for your specific
test class. A4 includes similar tests.

> So, can we assume that this will not happen, or should we be performing the
> same forward reference check on locals that we do for fields?

For locals, it is not a forward reference check. The check is that
a local variable must not be used in its own initializer.

0 new messages