tracker issue : CF-4204083

select a category, or use search below
(searches all categories and all time range)
Title:

Var scope continues to be in effect even after local-scoped variable becomes null

| View in Tracker

Status/Resolution/Reason: Closed/Withdrawn/AsDesigned

Reporter/Name(from Bugbase): A. B. / ()

Created: 03/10/2019

Components: Language, Scopes

Versions: 2018

Failure Type: Others

Found In Build/Fixed In Build: 2018.0.02.314033 /

Priority/Frequency: Normal /

Locale/System: / Platforms All

Vote Count: 0

Problem Description: 
In a function the function-local (var) scope continues to be in effect in memory after an originally var-scoped variable becomes null.

Steps to Reproduce:
Create and run a CFM page containing the following code

<cfscript>
f();

void function f () {
	// Assign testVar
	var testVar=1;
	var isTestVarNull=isNull(local.testVar);
	var isTestVarDefinedInLocalScope=isDefined("local.testVar");
	var isTestVarKeyInLocalStruct=structKeyExists(local, "testVar");
	writedump(var=local,label="Local scope: first dump - with assignment 'var testVar=1;'");
	
	// TestVar now takes null value
	testVar=cacheget("nonExistentCacheId");
	isTestVarNull=isNull(local.testVar);
	isTestVarDefinedInLocalScope=isDefined("local.testVar");
	isTestVarKeyInLocalStruct=structKeyExists(local, "testVar");
	writedump(var=local,label="Local scope: second dump - after testVar takes null value");

	// Reassign testVar
	testVar=5;
	isTestVarNull=isNull(local.testVar);
	isTestVarDefinedInLocalScope=isDefined("local.testVar");
	isTestVarKeyInLocalStruct=structKeyExists(local, "testVar");
	writedump(var=local,label="Local scope: third dump - after reassignment 'testVar=5;' (without 'var')");
}
</cfscript>

This code assigns the function-local variable testVar the value 1. After that, testVar is made to become null. Then follows the assignment testVar=5, without scope. 

Actual Result (see attached dumps):
isTestVarNull is No;
isTestVarDefinedInLocalScope is Yes;
isTestVarKeyInLocalStruct is Yes;

Expected Result:
I expected testVar, and any references to it, to have disappeared from memory. I also expected that, following the null assignment, the assignment testVar=5 (without scope) would define a new variable in variables scope. Hence that:
 
isTestVarNull is Yes;
isTestVarDefinedInLocalScope is No;
isTestVarKeyInLocalStruct is No;

Any Workarounds:
Not relevant

Attachments:

Comments:

This is as per design. If you declare a variable in function local scope and then you declare a variable(non-local) with the same then the variable declared in local scope takes priority and any value set operation on that variable will happen on the local variable.  
Comment by Vijay M.
30747 | May 15, 2019 06:53:15 AM GMT
Hi Vijay, Thanks for your reply. If the situation is so by design, then the design is wrong. Were we to accept such a design, it would lead to inconsistency in the CFML language. I shall now show you why. Consider the 5 lines of code that follow the comment "// TestVar now takes null value". The resulting dump is: "Local scope: second dump - after testVar takes null value - struct [empty] ISTESTVARDEFINEDINLOCALSCOPE NO ISTESTVARKEYINLOCALSTRUCT NO ISTESTVARNULL YES" This tells you that: 1) testVar is null; 2) local.testvar is undefined; 3) structKeyExists(local, "testVar"). In other words, local.testVar no longer exists. Now, in CFML, if you define a variable without any scope, then its scope is implicitly the variables scope. Hence, in the next line that follows, namely, // Reassign testVar testVar=5; a brand new variable is defined. By CFML rules, its scope must be the variables scope.
Comment by A. B.
30748 | May 16, 2019 10:11:35 AM GMT
Please read: 3) structKeyExists(local, "testVar") is false.
Comment by A. B.
30749 | May 16, 2019 10:12:53 AM GMT