Yes, it can be a difficult error to spot. In fact, it's it's so tricky that I made a mistake here: I was assuming the scoping rules of Python or of nonstrict _javascript_, where only functions establish lexical scopes. In Lua (as in C and strict-mode _javascript_), any code block establishes a lexical scope. So depending on your scoping rules, the nonlocal version of the shadow function could print 1 or 2.
Furthermore, depending on the implementation, it's possible that the presence of "x = 2" there might create the local variable even if it's never executed. This sounds like a silly hypothetical but something similar is actually a common source of bugs in Python and nonstrict _javascript_ code. In this case, again depending on the implementation, it might print "nil"!