Skip to content

Commit

Permalink
librustc: Improve error message for incompatible trait method signatu…
Browse files Browse the repository at this point in the history
…res.
  • Loading branch information
Ryman committed Apr 5, 2014
1 parent 4cf8d8c commit 28938d0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/librustc/middle/typeck/check/mod.rs
Expand Up @@ -923,7 +923,7 @@ fn compare_impl_method(tcx: &ty::ctxt,
result::Err(ref terr) => {
tcx.sess.span_err(
impl_m_span,
format!("method `{}` has an incompatible type: {}",
format!("method `{}` has an incompatible type for trait: {}",
token::get_ident(trait_m.ident),
ty::type_err_to_str(tcx, terr)));
ty::note_and_explain_type_err(tcx, terr);
Expand Down
33 changes: 32 additions & 1 deletion src/test/compile-fail/wrong-mul-method-signature.rs
Expand Up @@ -13,21 +13,52 @@
// (In this case the mul method should take &f64 and not f64)
// See: #11450

struct Vec1 {
x: f64
}

// Expecting ref in input signature
impl Mul<f64, Vec1> for Vec1 {
fn mul(&self, s: f64) -> Vec1 {
//~^ ERROR: method `mul` has an incompatible type for trait: expected &-ptr but found f64
Vec1 {
x: self.x * s
}
}
}

struct Vec2 {
x: f64,
y: f64
}

// Wrong type parameter ordering
impl Mul<Vec2, f64> for Vec2 {
fn mul(&self, s: f64) -> Vec2 {
//~^ ERROR: method `mul` has an incompatible type: expected &-ptr but found f64
//~^ ERROR: method `mul` has an incompatible type for trait: expected &-ptr but found f64
Vec2 {
x: self.x * s,
y: self.y * s
}
}
}

struct Vec3 {
x: f64,
y: f64,
z: f64
}

// Unexpected return type
impl Mul<f64, i32> for Vec3 {
fn mul(&self, s: &f64) -> f64 {
//~^ ERROR: method `mul` has an incompatible type for trait: expected i32 but found f64
*s
}
}

pub fn main() {
Vec1 { x: 1.0 } * 2.0;
Vec2 { x: 1.0, y: 2.0 } * 2.0;
Vec3 { x: 1.0, y: 2.0, z: 3.0 } * 2.0;
}

5 comments on commit 28938d0

@bors
Copy link
Contributor

@bors bors commented on 28938d0 Apr 5, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at Ryman@28938d0

@bors
Copy link
Contributor

@bors bors commented on 28938d0 Apr 5, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging Ryman/rust/improve_incompatible_type_error = 28938d0 into auto

@bors
Copy link
Contributor

@bors bors commented on 28938d0 Apr 5, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ryman/rust/improve_incompatible_type_error = 28938d0 merged ok, testing candidate = 94a055c

@bors
Copy link
Contributor

@bors bors commented on 28938d0 Apr 5, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 28938d0 Apr 5, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 94a055c

Please sign in to comment.