Skip to content

Commit c154782

Browse files
committed
Add notes for all potentially missing std::ops traits
1 parent 6c209d1 commit c154782

File tree

2 files changed

+70
-22
lines changed

2 files changed

+70
-22
lines changed

src/librustc_typeck/check/op.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -191,20 +191,28 @@ fn check_overloaded_binop<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
191191
"binary operation `{}` cannot be applied to type `{}`",
192192
hir_util::binop_to_string(op.node),
193193
lhs_ty);
194-
match op.node {
195-
hir::BiEq =>
196-
span_note!(fcx.tcx().sess, lhs_expr.span,
197-
"an implementation of `std::cmp::PartialEq` might be \
198-
missing for `{}` or one of its type paramters",
199-
lhs_ty),
194+
let missing_trait = match op.node {
195+
hir::BiAdd => Some("std::ops::Add"),
196+
hir::BiSub => Some("std::ops::Sub"),
197+
hir::BiMul => Some("std::ops::Mul"),
198+
hir::BiDiv => Some("std::ops::Div"),
199+
hir::BiRem => Some("std::ops::Rem"),
200+
hir::BiBitAnd => Some("std::ops::BitAnd"),
201+
hir::BiBitOr => Some("std::ops::BitOr"),
202+
hir::BiShl => Some("std::ops::Shl"),
203+
hir::BiShr => Some("std::ops::Shr"),
204+
hir::BiEq | hir::BiNe => Some("std::cmp::PartialEq"),
200205
hir::BiLt | hir::BiLe | hir::BiGt | hir::BiGe =>
201-
span_note!(fcx.tcx().sess, lhs_expr.span,
202-
"an implementation of `std::cmp::PartialOrd` might be \
203-
missing for `{}` or one of its type paramters",
204-
lhs_ty),
205-
_ => ()
206-
206+
Some("std::cmp::PartialOrd"),
207+
_ => None
207208
};
209+
210+
if let Some(missing_trait) = missing_trait {
211+
span_note!(fcx.tcx().sess, lhs_expr.span,
212+
"an implementation of `{}` might be missing for `{}` \
213+
or one of its type arguments",
214+
missing_trait, lhs_ty);
215+
}
208216
}
209217
}
210218
fcx.tcx().types.err

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

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,60 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
111
struct A;
212

313
fn main() {
414
let a = A;
515

6-
if a == a {} //~ ERROR binary operation `==` cannot be applied to type `A`
7-
//^~ NOTE an implementation of `std::cmp::PartialEq` might be missing for `A` or one of
16+
a + a; //~ ERROR binary operation `+` cannot be applied to type `A`
17+
//~^ NOTE an implementation of `std::ops::Add` might be missing for `A` or
18+
19+
a - a; //~ ERROR binary operation `-` cannot be applied to type `A`
20+
//~^ NOTE an implementation of `std::ops::Sub` might be missing for `A` or one of
21+
22+
a * a; //~ ERROR binary operation `*` cannot be applied to type `A`
23+
//~^ NOTE an implementation of `std::ops::Mul` might be missing for `A` or one of
24+
25+
a / a; //~ ERROR binary operation `/` cannot be applied to type `A`
26+
//~^ NOTE an implementation of `std::ops::Div` might be missing for `A` or one of
27+
28+
a % a; //~ ERROR binary operation `%` cannot be applied to type `A`
29+
//~^ NOTE an implementation of `std::ops::Rem` might be missing for `A` or one of
30+
31+
a & a; //~ ERROR binary operation `&` cannot be applied to type `A`
32+
//~^ NOTE an implementation of `std::ops::BitAnd` might be missing for `A` or one of
33+
34+
a | a; //~ ERROR binary operation `|` cannot be applied to type `A`
35+
//~^ NOTE an implementation of `std::ops::BitOr` might be missing for `A` or one of
36+
37+
a << a; //~ ERROR binary operation `<<` cannot be applied to type `A`
38+
//~^ NOTE an implementation of `std::ops::Shl` might be missing for `A` or one of
39+
40+
a >> a; //~ ERROR binary operation `>>` cannot be applied to type `A`
41+
//~^ NOTE an implementation of `std::ops::Shr` might be missing for `A` or one of
42+
43+
a == a; //~ ERROR binary operation `==` cannot be applied to type `A`
44+
//~^ NOTE an implementation of `std::cmp::PartialEq` might be missing for `A` or one of
45+
46+
a != a; //~ ERROR binary operation `!=` cannot be applied to type `A`
47+
//~^ NOTE an implementation of `std::cmp::PartialEq` might be missing for `A` or one of
848

9-
if a < a {} //~ ERROR binary operation `<` cannot be applied to type `A`
10-
//^~ NOTE an implementation of `std::cmp::PartialOrd` might be missing for `A` or one of
49+
a < a; //~ ERROR binary operation `<` cannot be applied to type `A`
50+
//~^ NOTE an implementation of `std::cmp::PartialOrd` might be missing for `A` or one of
1151

12-
if a <= a {} //~ ERROR binary operation `<=` cannot be applied to type `A`
13-
//^~ NOTE an implementation of `std::cmp::PartialOrd` might be missing for `A` or one of
52+
a <= a; //~ ERROR binary operation `<=` cannot be applied to type `A`
53+
//~^ NOTE an implementation of `std::cmp::PartialOrd` might be missing for `A` or one of
1454

15-
if a > a {} //~ ERROR binary operation `>` cannot be applied to type `A`
16-
//^~ NOTE an implementation of `std::cmp::PartialOrd` might be missing for `A` or one of
55+
a > a; //~ ERROR binary operation `>` cannot be applied to type `A`
56+
//~^ NOTE an implementation of `std::cmp::PartialOrd` might be missing for `A` or one of
1757

18-
if a >= a {} //~ ERROR binary operation `>=` cannot be applied to type `A`
19-
//^~ NOTE an implementation of `std::cmp::PartialOrd` might be missing for `A` or one of
58+
a >= a; //~ ERROR binary operation `>=` cannot be applied to type `A`
59+
//~^ NOTE an implementation of `std::cmp::PartialOrd` might be missing for `A` or one of
2060
}

0 commit comments

Comments
 (0)