Skip to content

Add notes for missing PartialEq and PartialOrd, closes #28837 #28933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/librustc_typeck/check/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,27 @@ fn check_overloaded_binop<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
"binary operation `{}` cannot be applied to type `{}`",
hir_util::binop_to_string(op.node),
lhs_ty);
let missing_trait = match op.node {
hir::BiAdd => Some("std::ops::Add"),
hir::BiSub => Some("std::ops::Sub"),
hir::BiMul => Some("std::ops::Mul"),
hir::BiDiv => Some("std::ops::Div"),
hir::BiRem => Some("std::ops::Rem"),
hir::BiBitAnd => Some("std::ops::BitAnd"),
hir::BiBitOr => Some("std::ops::BitOr"),
hir::BiShl => Some("std::ops::Shl"),
hir::BiShr => Some("std::ops::Shr"),
hir::BiEq | hir::BiNe => Some("std::cmp::PartialEq"),
hir::BiLt | hir::BiLe | hir::BiGt | hir::BiGe =>
Some("std::cmp::PartialOrd"),
_ => None
};

if let Some(missing_trait) = missing_trait {
span_note!(fcx.tcx().sess, lhs_expr.span,
"an implementation of `{}` might be missing for `{}`",
missing_trait, lhs_ty);
}
}
}
fcx.tcx().types.err
Expand Down
60 changes: 60 additions & 0 deletions src/test/compile-fail/issue-28837.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

struct A;
Copy link
Member

Choose a reason for hiding this comment

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

This file I believe will need a license to pass make tidy


fn main() {
let a = A;

a + a; //~ ERROR binary operation `+` cannot be applied to type `A`
//~^ NOTE an implementation of `std::ops::Add` might be missing for `A`

a - a; //~ ERROR binary operation `-` cannot be applied to type `A`
//~^ NOTE an implementation of `std::ops::Sub` might be missing for `A`

a * a; //~ ERROR binary operation `*` cannot be applied to type `A`
//~^ NOTE an implementation of `std::ops::Mul` might be missing for `A`

a / a; //~ ERROR binary operation `/` cannot be applied to type `A`
//~^ NOTE an implementation of `std::ops::Div` might be missing for `A`

a % a; //~ ERROR binary operation `%` cannot be applied to type `A`
//~^ NOTE an implementation of `std::ops::Rem` might be missing for `A`

a & a; //~ ERROR binary operation `&` cannot be applied to type `A`
//~^ NOTE an implementation of `std::ops::BitAnd` might be missing for `A`

a | a; //~ ERROR binary operation `|` cannot be applied to type `A`
//~^ NOTE an implementation of `std::ops::BitOr` might be missing for `A`

a << a; //~ ERROR binary operation `<<` cannot be applied to type `A`
//~^ NOTE an implementation of `std::ops::Shl` might be missing for `A`

a >> a; //~ ERROR binary operation `>>` cannot be applied to type `A`
//~^ NOTE an implementation of `std::ops::Shr` might be missing for `A`

a == a; //~ ERROR binary operation `==` cannot be applied to type `A`
//~^ NOTE an implementation of `std::cmp::PartialEq` might be missing for `A`

a != a; //~ ERROR binary operation `!=` cannot be applied to type `A`
//~^ NOTE an implementation of `std::cmp::PartialEq` might be missing for `A`

a < a; //~ ERROR binary operation `<` cannot be applied to type `A`
//~^ NOTE an implementation of `std::cmp::PartialOrd` might be missing for `A`

a <= a; //~ ERROR binary operation `<=` cannot be applied to type `A`
//~^ NOTE an implementation of `std::cmp::PartialOrd` might be missing for `A`

a > a; //~ ERROR binary operation `>` cannot be applied to type `A`
//~^ NOTE an implementation of `std::cmp::PartialOrd` might be missing for `A`

a >= a; //~ ERROR binary operation `>=` cannot be applied to type `A`
//~^ NOTE an implementation of `std::cmp::PartialOrd` might be missing for `A`
}