From 8fc14b6e626b5f0b98fb0cee425b7813b9de9851 Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Tue, 11 Jun 2013 14:19:47 -0400 Subject: [PATCH 1/4] librustc: Don't allow duplicate methods on a trait. --- src/librustc/middle/resolve.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/librustc/middle/resolve.rs b/src/librustc/middle/resolve.rs index 1adb991c96e..8c7751c879f 100644 --- a/src/librustc/middle/resolve.rs +++ b/src/librustc/middle/resolve.rs @@ -1333,7 +1333,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 +1357,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()); } From c6f3103006f3e91daf4ffbd026ff64d77f858675 Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Tue, 11 Jun 2013 14:53:33 -0400 Subject: [PATCH 2/4] librustc: Don't allow duplicate methods on impls. --- src/librustc/middle/resolve.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/librustc/middle/resolve.rs b/src/librustc/middle/resolve.rs index 8c7751c879f..e4be8dac712 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))); + } + } } } From 47772bcb737b229586cc9a9d96a9abda005b907c Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Tue, 11 Jun 2013 15:08:40 -0400 Subject: [PATCH 3/4] Add tests for duplicate methods on traits/impls. --- src/librustc/middle/resolve.rs | 8 ++++---- src/test/compile-fail/impl-duplicate-methods.rs | 17 +++++++++++++++++ .../compile-fail/trait-duplicate-methods.rs | 16 ++++++++++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 src/test/compile-fail/impl-duplicate-methods.rs create mode 100644 src/test/compile-fail/trait-duplicate-methods.rs diff --git a/src/librustc/middle/resolve.rs b/src/librustc/middle/resolve.rs index e4be8dac712..af8cd758fc5 100644 --- a/src/librustc/middle/resolve.rs +++ b/src/librustc/middle/resolve.rs @@ -1238,10 +1238,10 @@ impl Resolver { 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", + fmt!("duplicate definition of method `%s`", *self.session.str_of(ident))); self.session.span_note(*old_sp, - fmt!("first definition of method %s here", + fmt!("first definition of method `%s` here", *self.session.str_of(ident))); } } @@ -1375,10 +1375,10 @@ impl Resolver { 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", + fmt!("duplicate definition of method `%s`", *self.session.str_of(ident))); self.session.span_note(*old_sp, - fmt!("first definition of method %s here", + fmt!("first definition of method `%s` here", *self.session.str_of(ident))); } } 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() {} From aa9a992f3e4134f06bd005fb3299688ac6314659 Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Wed, 12 Jun 2013 02:45:07 -0400 Subject: [PATCH 4/4] libsyntax: Remove duplicate methods. --- src/librustc/middle/typeck/check/mod.rs | 5 ----- src/libsyntax/parse/parser.rs | 7 ------- 2 files changed, 12 deletions(-) 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 {