Skip to content

Commit 8e73bb6

Browse files
committed
detect and report shadows in nested bindings
1 parent 1ad5f7d commit 8e73bb6

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

src/rustc/middle/resolve.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -681,28 +681,29 @@ fn visit_arm_with_scope(a: ast::arm, sc: scopes, v: vt<scopes>) {
681681

682682
// This is only for irrefutable patterns (e.g. ones that appear in a let)
683683
// So if x occurs, and x is already known to be a enum, that's always an error
684-
fn visit_local_with_scope(e: @env, loc: @local, sc:scopes, v:vt<scopes>) {
685-
// Check whether the given local has the same name as a enum that's
686-
// in scope
687-
// We disallow this, in order to make alt patterns consisting of
688-
// a single identifier unambiguous (does the pattern "foo" refer
689-
// to enum foo, or is it binding a new name foo?)
690-
alt loc.node.pat.node {
691-
pat_ident(an_ident,_) {
692-
alt lookup_in_scope(*e, sc, loc.span, path_to_ident(an_ident),
693-
ns_val, false) {
694-
some(ast::def_variant(enum_id,variant_id)) {
695-
// Declaration shadows an enum that's in scope.
696-
// That's an error.
697-
e.sess.span_err(loc.span,
698-
#fmt("declaration of `%s` shadows an \
699-
enum that's in scope",
700-
path_to_ident(an_ident)));
701-
}
684+
fn visit_local_with_scope(e: @env, loc: @local, &&sc: scopes, v:vt<scopes>) {
685+
// Check whether the given local has the same name as a enum that's in
686+
// scope. We disallow this, in order to make alt patterns consisting of a
687+
// single identifier unambiguous (does the pattern "foo" refer to enum
688+
// foo, or is it binding a new name foo?)
689+
pat_util::walk_pat(loc.node.pat) { |p|
690+
alt p.node {
691+
pat_ident(path, _) {
692+
alt lookup_in_scope(*e, sc, loc.span, path_to_ident(path),
693+
ns_val, false) {
694+
some(ast::def_variant(enum_id, variant_id)) {
695+
// Declaration shadows an enum that's in scope.
696+
// That's an error.
697+
e.sess.span_err(loc.span,
698+
#fmt("declaration of `%s` shadows an \
699+
enum that's in scope",
700+
path_to_ident(path)));
701+
}
702702
_ {}
703+
}
703704
}
704-
}
705-
_ {}
705+
_ {}
706+
}
706707
}
707708
visit::visit_local(loc, sc, v);
708709
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
enum foo = uint;
2+
3+
fn main() {
4+
let (foo, _) = (2, 3); //! ERROR declaration of `foo` shadows an enum that's in scope
5+
}

0 commit comments

Comments
 (0)