From b2f7b3b79f3117885b265575f6c5dbf934757797 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 21 Dec 2022 12:04:59 -0300 Subject: Control variables in for loops are read only --- testes/nextvar.lua | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'testes/nextvar.lua') diff --git a/testes/nextvar.lua b/testes/nextvar.lua index 80b3d05c..87910ef9 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua @@ -609,10 +609,12 @@ do a = 0; for i=1.0, 0.99999, -1 do a=a+1 end; assert(a==1) end -do -- changing the control variable - local a - a = 0; for i = 1, 10 do a = a + 1; i = "x" end; assert(a == 10) - a = 0; for i = 10.0, 1, -1 do a = a + 1; i = "x" end; assert(a == 10) +do -- attempt to change the control variable + local st, msg = load "for i = 1, 10 do i = 10 end" + assert(not st and string.find(msg, "assign to const variable 'i'")) + + local st, msg = load "for v, k in pairs{} do v = 10 end" + assert(not st and string.find(msg, "assign to const variable 'v'")) end -- conversion -- cgit v1.2.3-55-g6feb