Compiler optimizations for Scopes, checkDoubleDefs, Namer

Resubmission of #3771, adds a unit test for Scope and
fixes the boostrap error from the previous attempt.

Review by @gkossakowski