Skip to content

Don't allow duplicate methods. #7064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 12, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions src/librustc/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
}
}
}

Expand Down Expand Up @@ -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);

Expand All @@ -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());
}
Expand Down
5 changes: 0 additions & 5 deletions src/librustc/middle/typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
7 changes: 0 additions & 7 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions src/test/compile-fail/impl-duplicate-methods.rs
Original file line number Diff line number Diff line change
@@ -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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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() {}
16 changes: 16 additions & 0 deletions src/test/compile-fail/trait-duplicate-methods.rs
Original file line number Diff line number Diff line change
@@ -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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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() {}