The problem is that @eval evaluates the following expression in the global scope, even though it is executed inside a function. Actually, after running test1() you’ll also have the variable my_var in Main.
I’m not sure about what’s happening in the second example, but the error does not happen if the name of the variable outside the loop is different.