Skip to content

Commit 94a055c

Browse files
committed
auto merge of #13333 : Ryman/rust/improve_incompatible_type_error, r=alexcrichton
This can be a frustrating error message, ideally we should print the signature mismatch, but hinting that it's a trait incompatibility helps tracking root cause. Also beefed up the testcases for this. Ideally we would print the signature mismatch in the error helper?
2 parents 2dcdc75 + 28938d0 commit 94a055c

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ fn compare_impl_method(tcx: &ty::ctxt,
923923
result::Err(ref terr) => {
924924
tcx.sess.span_err(
925925
impl_m_span,
926-
format!("method `{}` has an incompatible type: {}",
926+
format!("method `{}` has an incompatible type for trait: {}",
927927
token::get_ident(trait_m.ident),
928928
ty::type_err_to_str(tcx, terr)));
929929
ty::note_and_explain_type_err(tcx, terr);

src/test/compile-fail/wrong-mul-method-signature.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,52 @@
1313
// (In this case the mul method should take &f64 and not f64)
1414
// See: #11450
1515

16+
struct Vec1 {
17+
x: f64
18+
}
19+
20+
// Expecting ref in input signature
21+
impl Mul<f64, Vec1> for Vec1 {
22+
fn mul(&self, s: f64) -> Vec1 {
23+
//~^ ERROR: method `mul` has an incompatible type for trait: expected &-ptr but found f64
24+
Vec1 {
25+
x: self.x * s
26+
}
27+
}
28+
}
29+
1630
struct Vec2 {
1731
x: f64,
1832
y: f64
1933
}
2034

35+
// Wrong type parameter ordering
2136
impl Mul<Vec2, f64> for Vec2 {
2237
fn mul(&self, s: f64) -> Vec2 {
23-
//~^ ERROR: method `mul` has an incompatible type: expected &-ptr but found f64
38+
//~^ ERROR: method `mul` has an incompatible type for trait: expected &-ptr but found f64
2439
Vec2 {
2540
x: self.x * s,
2641
y: self.y * s
2742
}
2843
}
2944
}
3045

46+
struct Vec3 {
47+
x: f64,
48+
y: f64,
49+
z: f64
50+
}
51+
52+
// Unexpected return type
53+
impl Mul<f64, i32> for Vec3 {
54+
fn mul(&self, s: &f64) -> f64 {
55+
//~^ ERROR: method `mul` has an incompatible type for trait: expected i32 but found f64
56+
*s
57+
}
58+
}
59+
3160
pub fn main() {
61+
Vec1 { x: 1.0 } * 2.0;
3262
Vec2 { x: 1.0, y: 2.0 } * 2.0;
63+
Vec3 { x: 1.0, y: 2.0, z: 3.0 } * 2.0;
3364
}

0 commit comments

Comments
 (0)