luau icon indicating copy to clipboard operation
luau copied to clipboard

Unsealed table refinement leaking from scope

Open MagmaBurnsV opened this issue 2 years ago • 2 comments

Refining an unsealed table inside a scope will allow for that refinement to leak:

local t = {}

local function f()
	t.a = 1
end

print(t.a) -- f's refinement leaks, despite not calling f

This also happens with conditionals like if false then t.a = 1 end.

MagmaBurnsV avatar Mar 25 '23 15:03 MagmaBurnsV

Extending tables in this way is meant to be an affordance to infer types of tables that are initialized piecemeal. Code like what you describe isn't really what it was meant for.

The assignment to t.a should probably not be allowed. Would that break existing code?

andyfriesen avatar Apr 06 '23 17:04 andyfriesen

I personally don't have any code that depends on this behavior. Although I do think it should be allowed in nonstrict because there are some crazy people who do stuff like this:

local function f(a)
	local t = {}
	
	if a then
		t.a = a
	end
	
	return t
end

Otherwise, this definitely should not be allowed in strict.

MagmaBurnsV avatar Apr 07 '23 17:04 MagmaBurnsV