diff --git a/src/librustc/middle/resolve.rs b/src/librustc/middle/resolve.rs index 1adb991c96e..af8cd758fc5 100644 --- a/src/librustc/middle/resolve.rs +++ b/src/librustc/middle/resolve.rs @@ -1226,11 +1226,25 @@ impl Resolver { // the same module that declared the type. // Bail out early if there are no static methods. + let mut methods_seen = HashMap::new(); let mut has_static_methods = false; for methods.each |method| { match method.explicit_self.node { sty_static => has_static_methods = true, - _ => {} + _ => { + // Make sure you can't define duplicate methods + let ident = method.ident; + let span = method.span; + let old_sp = methods_seen.find_or_insert(ident, span); + if *old_sp != span { + self.session.span_err(span, + fmt!("duplicate definition of method `%s`", + *self.session.str_of(ident))); + self.session.span_note(*old_sp, + fmt!("first definition of method `%s` here", + *self.session.str_of(ident))); + } + } } } @@ -1333,7 +1347,7 @@ impl Resolver { } // Add the names of all the methods to the trait info. - let mut method_names = HashSet::new(); + let mut method_names = HashMap::new(); for methods.each |method| { let ty_m = trait_method_to_ty_method(method); @@ -1357,13 +1371,22 @@ impl Resolver { ty_m.span); } _ => { - method_names.insert(ident); + // Make sure you can't define duplicate methods + let old_sp = method_names.find_or_insert(ident, ty_m.span); + if *old_sp != ty_m.span { + self.session.span_err(ty_m.span, + fmt!("duplicate definition of method `%s`", + *self.session.str_of(ident))); + self.session.span_note(*old_sp, + fmt!("first definition of method `%s` here", + *self.session.str_of(ident))); + } } } } let def_id = local_def(item.id); - for method_names.each |name| { + for method_names.each |name, _| { if !self.method_map.contains_key(name) { self.method_map.insert(*name, HashSet::new()); } diff --git a/src/librustc/middle/typeck/check/mod.rs b/src/librustc/middle/typeck/check/mod.rs index a75c1decd0d..2c3cdd2b684 100644 --- a/src/librustc/middle/typeck/check/mod.rs +++ b/src/librustc/middle/typeck/check/mod.rs @@ -722,11 +722,6 @@ impl FnCtxt { } } - pub fn expr_to_str(&self, expr: @ast::expr) -> ~str { - fmt!("expr(%?:%s)", expr.id, - pprust::expr_to_str(expr, self.tcx().sess.intr())) - } - pub fn block_region(&self) -> ty::Region { ty::re_scope(self.region_lb) } diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 03b4c0c5ca1..8dd80be4f9c 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -1063,13 +1063,6 @@ impl Parser { } } - pub fn token_is_lifetime(&self, tok: &token::Token) -> bool { - match *tok { - token::LIFETIME(_) => true, - _ => false - } - } - /// Parses a single lifetime // matches lifetime = ( LIFETIME ) | ( IDENT / ) pub fn parse_lifetime(&self) -> ast::Lifetime { diff --git a/src/test/compile-fail/impl-duplicate-methods.rs b/src/test/compile-fail/impl-duplicate-methods.rs new file mode 100644 index 00000000000..ec766e5ce9b --- /dev/null +++ b/src/test/compile-fail/impl-duplicate-methods.rs @@ -0,0 +1,17 @@ +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +struct Foo; +impl Foo { + fn orange(&self){} + fn orange(&self){} //~ ERROR error: duplicate definition of method `orange` +} + +fn main() {} diff --git a/src/test/compile-fail/trait-duplicate-methods.rs b/src/test/compile-fail/trait-duplicate-methods.rs new file mode 100644 index 00000000000..e2ba5267eba --- /dev/null +++ b/src/test/compile-fail/trait-duplicate-methods.rs @@ -0,0 +1,16 @@ +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +trait Foo { + fn orange(&self); + fn orange(&self); //~ ERROR error: duplicate definition of method `orange` +} + +fn main() {}