rustc: disallow duplicate methods in trait impls

Closes #8153
This commit is contained in:
Corey Richardson 2014-03-18 10:42:23 -04:00 committed by Alex Crichton
parent 1f4c63049e
commit 1607871dc2
2 changed files with 33 additions and 0 deletions

View File

@ -48,6 +48,8 @@ use util::ppaux::Repr;
use std::rc::Rc;
use std::vec_ng::Vec;
use std::vec_ng;
use collections::HashSet;
use syntax::abi::AbiSet;
use syntax::ast::{RegionTyParamBound, TraitTyParamBound};
use syntax::ast;
@ -478,7 +480,12 @@ fn convert_methods(ccx: &CrateCtxt,
rcvr_visibility: ast::Visibility)
{
let tcx = ccx.tcx;
let mut seen_methods = HashSet::new();
for m in ms.iter() {
if !seen_methods.insert(m.ident.repr(ccx.tcx)) {
tcx.sess.span_err(m.span, "duplicate method in trait impl");
}
let num_rcvr_ty_params = rcvr_ty_generics.type_param_defs().len();
let m_ty_generics = ty_generics_for_fn_or_method(ccx, &m.generics,
num_rcvr_ty_params);

View File

@ -0,0 +1,26 @@
// 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.
// Test that duplicate methods in impls are not allowed
struct Foo;
trait Bar {
fn bar(&self) -> int;
}
impl Bar for Foo {
fn bar(&self) -> int {1}
fn bar(&self) -> int {2} //~ ERROR duplicate method
}
fn main() {
println!("{}", Foo.bar());
}