Opened 7 years ago

Closed 7 years ago

Last modified 43 years ago

#378 closed Bugs (Fixed)

Variable not initialized does not work for array access

Reported by: nobody Owned by: axelcl
Priority: 5 Milestone:
Component: PHP Parser Version: None
Keywords: Cc:

Description

If the first reference to an unitialized variable is an
array access then no warning is produced.

Illustrative example:

function example_with_no_warning () {
  print ($abc [1]);
  print ($abc);
}

function example_with_warning () {
  print ($abc);
}

Change History (3)

comment:1 Changed 7 years ago by bananeweizen

Logged In: YES 
user_id=440739

This can eventually be fixed by changing the Parser.php at
about line 3143 from

  ref = null;
  getNextToken();
  expr();
  if (token != TokenNameRBRACE) {
    throwSyntaxError("'}' expected in reference variable.");
  }
  getNextToken();
} else if (token == TokenNameLBRACKET) {
  if (ref != null && ref instanceof FieldReference) {
    FieldReference fref = (FieldReference) ref;
    addVariableSet(fref.token);
  }
  ref = null;

to

//  ref = null;
  getNextToken();
  expr();
  if (token != TokenNameRBRACE) {
    throwSyntaxError("'}' expected in reference variable.");
  }
  getNextToken();
} else if (token == TokenNameLBRACKET) {
//  if (ref != null && ref instanceof FieldReference) {
//    FieldReference fref = (FieldReference) ref;
//    addVariableSet(fref.token);
//  }
//  ref = null;

but I don't oversee the consequences completely. From my
point of view removing those assignments and the variable
reference is completely legal, but I'm not really
experienced with the parser, so one of the core developers
should look at this.

comment:2 Changed 7 years ago by axelcl

  • Status changed from assigned to closed
Logged In: YES 
user_id=1143825

I added a change to CVS-20051219.
(see Parser.java patch below)

BTW: most parser changes could be tested with the JUnit test
cases in CVS module: net.sourceforge.phpeclipse.tests
See for example the parser test suite:
 net.sourceforge.phpeclipse.tests.parser.PHPParserTestSuite
I checked into CVS today.
Unfortunately all problem messages are switched off for the
unit tests. So the above warning couldn't be detected.


Index: Parser.java
===================================================================
RCS file:
/cvsroot/phpeclipse/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/parser/Parser.java,v
retrieving revision 1.84
diff -u -r1.84 Parser.java
--- Parser.java	19 Dec 2005 20:34:36 -0000	1.84
+++ Parser.java	19 Dec 2005 21:53:26 -0000
@@ -3126,7 +3126,11 @@
 			} else if (token == TokenNameLBRACKET) {
 				if (ref != null && ref instanceof FieldReference) {
 					FieldReference fref = (FieldReference) ref;
-					addVariableSet(fref.token);
+					if (!containsVariableSet(fref.token)) {
+						problemReporter.uninitializedLocalVariable(new
String(fref.token), fref.sourceStart(), fref.sourceEnd(),
+								referenceContext, compilationUnit.compilationResult);
+						addVariableSet(fref.token);
+					}
 				}
 				ref = null;
 				getNextToken();

comment:3 Changed 7 years ago by axelcl

Logged In: YES 
user_id=1143825

Sorry, I meant:
"Unfortunately all warning messages are switched off for the
unit tests."
and NOT "all problem messages".
Note: See TracTickets for help on using tickets.