CommandHelper

CommandHelper

46.5k Downloads

"@var was already defined" runtime error on typed foreach loop variable

Pieter12345 opened this issue ยท 1 comments

commented

Code

foreach(int @a in array(1, 2)) { }

or

foreach(array(1, 2) as int @a) { }

or

foreach(array(1, 2), int @a) { }

Results in

mei 03, 2023 8:59:51 P.M. com.laytonsmith.core.Static LogDebug
SEVERE: [ERROR][runtime] @a was already defined at C:\Program Files\MethodScript\Interpreter:1.1 but is being redefined.

Expected behavior is that @a can be typed without runtime error. Do note that program flow/execution is unaffected by this 'error'.

Note that this runtime error shows once per inner foreach execution for nested foreach calls:

foreach(int @a in array(1, 2, 3)) { foreach(int @b in array(1, 2, 3)) { } }

Results in

mei 03, 2023 9:08:43 P.M. com.laytonsmith.core.Static LogDebug
SEVERE: [ERROR][runtime] @a was already defined at C:\Program Files\MethodScript\Interpreter:1.1 but is being redefined.
mei 03, 2023 9:08:43 P.M. com.laytonsmith.core.Static LogDebug
SEVERE: [ERROR][runtime] @b was already defined at C:\Program Files\MethodScript\Interpreter:1.34 but is being redefined.
mei 03, 2023 9:08:43 P.M. com.laytonsmith.core.Static LogDebug
SEVERE: [ERROR][runtime] @b was already defined at C:\Program Files\MethodScript\Interpreter:1.37 but is being redefined.
mei 03, 2023 9:08:43 P.M. com.laytonsmith.core.Static LogDebug
SEVERE: [ERROR][runtime] @b was already defined at C:\Program Files\MethodScript\Interpreter:1.37 but is being redefined.

MethodScript version: Uncertain, with strict mode and static analysis enabled. MethodScript jar was last changed on 03-04-2023.

commented

Seems to occur when 2 foreach functions in a row use the same typed variable name, or when using nested foreach functions with a typed variable in the inner loop. (strict mode/static analysis don't have to be on)