[ty] Track enclosing definitions and nested references in the semantic index
This commit adds duplicate errors for invalid `nonlocal` statements,
which breaks some tests. The following commit removes `infer_nonlocal`
from `infer.rs` and unbreaks those tests.