Skip to content

Commit c7a58b5

Browse files
committed
Auto merge of #28933 - fhahn:issue-28837-partialeq-note, r=alexcrichton
this PR adds notes for missing `PartialEq` and `PartialOrd`. I've added a test case but it seems like `NOTE` is ignored by the test runner. #28837
2 parents ad5a43a + b21ae1a commit c7a58b5

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

src/librustc_typeck/check/op.rs

+21
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,27 @@ 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+
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"),
205+
hir::BiLt | hir::BiLe | hir::BiGt | hir::BiGe =>
206+
Some("std::cmp::PartialOrd"),
207+
_ => None
208+
};
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+
missing_trait, lhs_ty);
214+
}
194215
}
195216
}
196217
fcx.tcx().types.err

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

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +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+
11+
struct A;
12+
13+
fn main() {
14+
let a = A;
15+
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`
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`
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`
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`
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`
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`
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`
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`
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`
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`
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`
48+
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`
51+
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`
54+
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`
57+
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`
60+
}

0 commit comments

Comments
 (0)