Skip to content

Commit 579a139

Browse files
committed
rustc: catch non-trait methods before typeck.
Closes #3973.
1 parent d77cb22 commit 579a139

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

src/librustc/middle/resolve.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4021,6 +4021,9 @@ impl<'a> Resolver<'a> {
40214021

40224022
this.with_current_self_type(self_type, |this| {
40234023
for method in methods.iter() {
4024+
// If this is a trait impl, ensure the method exists in trait
4025+
this.check_trait_method(&**method);
4026+
40244027
// We also need a new scope for the method-specific type parameters.
40254028
this.resolve_method(MethodRibKind(id, Provided(method.id)),
40264029
&**method);
@@ -4030,6 +4033,21 @@ impl<'a> Resolver<'a> {
40304033
});
40314034
}
40324035

4036+
fn check_trait_method(&self, method: &Method) {
4037+
// If there is a TraitRef in scope for an impl, then the method must be in the trait.
4038+
for &(did, ref trait_ref) in self.current_trait_ref.iter() {
4039+
let method_name = method.ident.name;
4040+
4041+
if self.method_map.borrow().find(&(method_name, did)).is_none() {
4042+
let path_str = self.path_idents_to_str(&trait_ref.path);
4043+
self.resolve_error(method.span,
4044+
format!("method `{}` is not a member of trait `{}`",
4045+
token::get_name(method_name),
4046+
path_str).as_slice());
4047+
}
4048+
}
4049+
}
4050+
40334051
fn resolve_module(&mut self, module: &Mod, _span: Span,
40344052
_name: Ident, id: NodeId) {
40354053
// Write the implementations in scope into the module metadata.

src/librustc/middle/typeck/check/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,8 @@ fn check_impl_methods_against_trait(ccx: &CrateCtxt,
784784
&impl_trait_ref.substs);
785785
}
786786
None => {
787-
tcx.sess.span_err(
787+
// This is span_bug as it should have already been caught in resolve.
788+
tcx.sess.span_bug(
788789
impl_method.span,
789790
format!(
790791
"method `{}` is not a member of trait `{}`",

src/test/compile-fail/issue-3973.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,30 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// ignore-test
12-
13-
use std::io;
14-
1511
struct Point {
1612
x: f64,
1713
y: f64,
1814
}
1915

20-
impl ToStr for Point { //~ ERROR implements a method not defined in the trait
16+
trait NewTrait {
17+
fn a(&self) -> String;
18+
}
19+
20+
impl NewTrait for Point {
2121
fn new(x: f64, y: f64) -> Point {
22+
//~^ ERROR method `new` is not a member of trait `NewTrait`
2223
Point { x: x, y: y }
2324
}
2425

25-
fn to_str(&self) -> String {
26+
fn a(&self) -> String {
2627
format!("({}, {})", self.x, self.y)
2728
}
2829
}
2930

3031
fn main() {
3132
let p = Point::new(0.0, 0.0);
32-
println!("{}", p.to_str());
33+
//~^ ERROR unresolved name `Point::new`
34+
//~^^ ERROR unresolved name
35+
//~^^^ ERROR use of undeclared module `Point`
36+
println!("{}", p.a());
3337
}

0 commit comments

Comments
 (0)