Skip to content

Allow impls and traits having static and instance methods with the same name #4279

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

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 8 additions & 3 deletions src/librustc/middle/typeck/check/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,9 @@ impl LookupContext {

let tcx = self.tcx();
let ms = ty::trait_methods(tcx, did);
let index = match vec::position(*ms, |m| m.ident == self.m_name) {
let index = match vec::position(*ms,
|m| (m.self_ty != ast::sty_static &&
m.ident == self.m_name)) {
Some(i) => i,
None => { return; } // no method with the right name
};
Expand Down Expand Up @@ -508,7 +510,9 @@ impl LookupContext {
let tcx = self.tcx();
let methods = ty::trait_methods(tcx, did); // XXX: Inherited methods.
let index;
match vec::position(*methods, |m| m.ident == self.m_name) {
match vec::position(*methods,
|m| (m.self_ty != ast::sty_static &&
m.ident == self.m_name)) {
Some(i) => index = i,
None => return
}
Expand Down Expand Up @@ -552,7 +556,8 @@ impl LookupContext {
let idx = {
// FIXME #3453 can't use impl_info.methods.position
match vec::position(impl_info.methods,
|m| m.ident == self.m_name) {
|m| (m.self_type != ast::sty_static &&
m.ident == self.m_name)) {
Some(idx) => idx,
None => { return; } // No method with the right name.
}
Expand Down
77 changes: 47 additions & 30 deletions src/librustc/middle/typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,34 +314,6 @@ fn compare_impl_method(tcx: ty::ctxt,

let impl_m = &cm.mty;

// FIXME(#2687)---this check is too strict. For example, a trait
// method with self type `&self` or `&mut self` should be
// implementable by an `&const self` method (the impl assumes less
// than the trait provides).
if impl_m.self_ty != trait_m.self_ty {
if impl_m.self_ty == ast::sty_static {
// Needs to be a fatal error because otherwise,
// method::transform_self_type_for_method ICEs
tcx.sess.span_fatal(cm.span,
fmt!("method `%s` is declared as \
static in its impl, but not in \
its trait", tcx.sess.str_of(impl_m.ident)));
}
else if trait_m.self_ty == ast::sty_static {
tcx.sess.span_fatal(cm.span,
fmt!("method `%s` is declared as \
static in its trait, but not in \
its impl", tcx.sess.str_of(impl_m.ident)));
}
else {
tcx.sess.span_err(
cm.span,
fmt!("method `%s`'s self type does \
not match the trait method's \
self type", tcx.sess.str_of(impl_m.ident)));
}
}

if impl_m.tps.len() != trait_m.tps.len() {
tcx.sess.span_err(
cm.span,
Expand Down Expand Up @@ -483,12 +455,37 @@ fn check_methods_against_trait(ccx: @crate_ctxt,
// we'll catch it in coherence
let trait_ms = ty::trait_methods(tcx, did);
for impl_ms.each |impl_m| {
match trait_ms.find(|trait_m| trait_m.ident == impl_m.mty.ident) {
Some(ref trait_m) => {
match find_matching_trait_method(impl_m, trait_ms) {
Some(ref trait_m) if trait_m.self_ty == impl_m.mty.self_ty => {
compare_impl_method(
ccx.tcx, tps.len(), impl_m, trait_m,
&tpt.substs, selfty);
}
Some(ref trait_m) => {
// Method name matches, self-type differs.
// Needs to be a fatal error because otherwise,
// method::transform_self_type_for_method ICEs
if (impl_m.mty.self_ty == ast::sty_static) {
tcx.sess.span_fatal(impl_m.span,
fmt!("method `%s` is declared as \
static in its impl, but not in \
its trait", tcx.sess.str_of(impl_m.mty.ident)));
} else if (trait_m.self_ty == ast::sty_static) {
tcx.sess.span_fatal(impl_m.span,
fmt!("method `%s` is declared as \
static in its trait, but not in \
its impl", tcx.sess.str_of(impl_m.mty.ident)));
} else {
// FIXME(#2687)---this check is too strict. For example,
// a trait method with self type `&self` or `&mut self`
// should be implementable by an `&const self` method
// (the impl assumes less than the trait provides).
tcx.sess.span_err(impl_m.span,
fmt!("method `%s`'s self type does \
not match the trait method's \
self type", tcx.sess.str_of(impl_m.mty.ident)));
}
}
None => {
// This method is not part of the trait
tcx.sess.span_err(
Expand All @@ -501,6 +498,26 @@ fn check_methods_against_trait(ccx: @crate_ctxt,
}
} // fn

// Find a matching method in the trait. We favor those
// with the same name and self-type as the impl method.
// At least, we try to find one with the same name to
// fail with an explicit error message.
fn find_matching_trait_method(impl_m: &ConvertedMethod,
trait_ms: &~[ty::method])
-> Option<ty::method> {

let mut last_name_match = None;
for trait_ms.each |m| {
if (m.ident == impl_m.mty.ident) {
last_name_match = Some(*m);
if (m.self_ty == impl_m.mty.self_ty) {
return last_name_match;
}
}
}
last_name_match
}

fn convert_field(ccx: @crate_ctxt,
rp: Option<ty::region_variance>,
bounds: @~[ty::param_bounds],
Expand Down
20 changes: 20 additions & 0 deletions src/test/compile-fail/issue-4265.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2012 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 Trait {
static fn bar();

fn foo() {
self.bar();
//~^ ERROR type `self` does not implement any method in scope named `bar`
}
}

fn main() {}
27 changes: 27 additions & 0 deletions src/test/run-pass/issue-4265.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2012 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 Trait {
static fn fun();
fn fun();
}

struct A {
v: bool
}

impl A: Trait {
static fn fun() {}
fn fun() {}
}

fn main() {
A { v: true }.fun();
}