From 6c209d1cc49a26247c7055c3da546dbe243ed81b Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Fri, 9 Oct 2015 22:42:46 +0200 Subject: [PATCH 1/3] Add notes for missing PartialEq and PartialOrd, closes #28837 --- src/librustc_typeck/check/op.rs | 14 ++++++++++++++ src/test/compile-fail/issue-28837.rs | 20 ++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/test/compile-fail/issue-28837.rs diff --git a/src/librustc_typeck/check/op.rs b/src/librustc_typeck/check/op.rs index 7be61327f81..ab145db8242 100644 --- a/src/librustc_typeck/check/op.rs +++ b/src/librustc_typeck/check/op.rs @@ -191,6 +191,20 @@ 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); + match op.node { + hir::BiEq => + span_note!(fcx.tcx().sess, lhs_expr.span, + "an implementation of `std::cmp::PartialEq` might be \ + missing for `{}` or one of its type paramters", + lhs_ty), + hir::BiLt | hir::BiLe | hir::BiGt | hir::BiGe => + span_note!(fcx.tcx().sess, lhs_expr.span, + "an implementation of `std::cmp::PartialOrd` might be \ + missing for `{}` or one of its type paramters", + lhs_ty), + _ => () + + }; } } fcx.tcx().types.err diff --git a/src/test/compile-fail/issue-28837.rs b/src/test/compile-fail/issue-28837.rs new file mode 100644 index 00000000000..9f2a565e732 --- /dev/null +++ b/src/test/compile-fail/issue-28837.rs @@ -0,0 +1,20 @@ +struct A; + +fn main() { + let a = A; + + if a == a {} //~ ERROR binary operation `==` cannot be applied to type `A` + //^~ NOTE an implementation of `std::cmp::PartialEq` might be missing for `A` or one of + + if a < a {} //~ ERROR binary operation `<` cannot be applied to type `A` + //^~ NOTE an implementation of `std::cmp::PartialOrd` might be missing for `A` or one of + + if a <= a {} //~ ERROR binary operation `<=` cannot be applied to type `A` + //^~ NOTE an implementation of `std::cmp::PartialOrd` might be missing for `A` or one of + + if a > a {} //~ ERROR binary operation `>` cannot be applied to type `A` + //^~ NOTE an implementation of `std::cmp::PartialOrd` might be missing for `A` or one of + + if a >= a {} //~ ERROR binary operation `>=` cannot be applied to type `A` + //^~ NOTE an implementation of `std::cmp::PartialOrd` might be missing for `A` or one of +} From c154782435c9e4a9b737c14d8a8a402938a46df5 Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Sat, 10 Oct 2015 13:03:18 +0200 Subject: [PATCH 2/3] Add notes for all potentially missing std::ops traits --- src/librustc_typeck/check/op.rs | 32 +++++++++------ src/test/compile-fail/issue-28837.rs | 60 +++++++++++++++++++++++----- 2 files changed, 70 insertions(+), 22 deletions(-) diff --git a/src/librustc_typeck/check/op.rs b/src/librustc_typeck/check/op.rs index ab145db8242..b00535c9610 100644 --- a/src/librustc_typeck/check/op.rs +++ b/src/librustc_typeck/check/op.rs @@ -191,20 +191,28 @@ 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); - match op.node { - hir::BiEq => - span_note!(fcx.tcx().sess, lhs_expr.span, - "an implementation of `std::cmp::PartialEq` might be \ - missing for `{}` or one of its type paramters", - 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 => - span_note!(fcx.tcx().sess, lhs_expr.span, - "an implementation of `std::cmp::PartialOrd` might be \ - missing for `{}` or one of its type paramters", - lhs_ty), - _ => () - + 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 `{}` \ + or one of its type arguments", + missing_trait, lhs_ty); + } } } fcx.tcx().types.err diff --git a/src/test/compile-fail/issue-28837.rs b/src/test/compile-fail/issue-28837.rs index 9f2a565e732..6baaebc3244 100644 --- a/src/test/compile-fail/issue-28837.rs +++ b/src/test/compile-fail/issue-28837.rs @@ -1,20 +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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + struct A; fn main() { let a = A; - if a == a {} //~ ERROR binary operation `==` cannot be applied to type `A` - //^~ NOTE an implementation of `std::cmp::PartialEq` might be missing for `A` or one of + a + a; //~ ERROR binary operation `+` cannot be applied to type `A` + //~^ NOTE an implementation of `std::ops::Add` might be missing for `A` or - if a < a {} //~ ERROR binary operation `<` cannot be applied to type `A` - //^~ NOTE an implementation of `std::cmp::PartialOrd` might be missing for `A` or one of + a - a; //~ ERROR binary operation `-` cannot be applied to type `A` + //~^ NOTE an implementation of `std::ops::Sub` might be missing for `A` or one of - if a <= a {} //~ ERROR binary operation `<=` cannot be applied to type `A` - //^~ NOTE an implementation of `std::cmp::PartialOrd` might be missing for `A` or one of + a * a; //~ ERROR binary operation `*` cannot be applied to type `A` + //~^ NOTE an implementation of `std::ops::Mul` might be missing for `A` or one of - if a > a {} //~ ERROR binary operation `>` cannot be applied to type `A` - //^~ NOTE an implementation of `std::cmp::PartialOrd` might be missing for `A` or one of + a / a; //~ ERROR binary operation `/` cannot be applied to type `A` + //~^ NOTE an implementation of `std::ops::Div` might be missing for `A` or one of - if a >= a {} //~ ERROR binary operation `>=` cannot be applied to type `A` - //^~ NOTE an implementation of `std::cmp::PartialOrd` might be missing for `A` or one of + a % a; //~ ERROR binary operation `%` cannot be applied to type `A` + //~^ NOTE an implementation of `std::ops::Rem` might be missing for `A` or one of + + a & a; //~ ERROR binary operation `&` cannot be applied to type `A` + //~^ NOTE an implementation of `std::ops::BitAnd` might be missing for `A` or one of + + a | a; //~ ERROR binary operation `|` cannot be applied to type `A` + //~^ NOTE an implementation of `std::ops::BitOr` might be missing for `A` or one of + + a << a; //~ ERROR binary operation `<<` cannot be applied to type `A` + //~^ NOTE an implementation of `std::ops::Shl` might be missing for `A` or one of + + a >> a; //~ ERROR binary operation `>>` cannot be applied to type `A` + //~^ NOTE an implementation of `std::ops::Shr` might be missing for `A` or one of + + a == a; //~ ERROR binary operation `==` cannot be applied to type `A` + //~^ NOTE an implementation of `std::cmp::PartialEq` might be missing for `A` or one of + + a != a; //~ ERROR binary operation `!=` cannot be applied to type `A` + //~^ NOTE an implementation of `std::cmp::PartialEq` might be missing for `A` or one of + + a < a; //~ ERROR binary operation `<` cannot be applied to type `A` + //~^ NOTE an implementation of `std::cmp::PartialOrd` might be missing for `A` or one of + + a <= a; //~ ERROR binary operation `<=` cannot be applied to type `A` + //~^ NOTE an implementation of `std::cmp::PartialOrd` might be missing for `A` or one of + + a > a; //~ ERROR binary operation `>` cannot be applied to type `A` + //~^ NOTE an implementation of `std::cmp::PartialOrd` might be missing for `A` or one of + + a >= a; //~ ERROR binary operation `>=` cannot be applied to type `A` + //~^ NOTE an implementation of `std::cmp::PartialOrd` might be missing for `A` or one of } From b21ae1ab1ad582964c12136f645f80865a4c8d54 Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Mon, 12 Oct 2015 00:25:50 +0200 Subject: [PATCH 3/3] Reword note about missing trait implementation --- src/librustc_typeck/check/op.rs | 3 +-- src/test/compile-fail/issue-28837.rs | 32 ++++++++++++++-------------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/librustc_typeck/check/op.rs b/src/librustc_typeck/check/op.rs index b00535c9610..0c65f68f02e 100644 --- a/src/librustc_typeck/check/op.rs +++ b/src/librustc_typeck/check/op.rs @@ -209,8 +209,7 @@ fn check_overloaded_binop<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, if let Some(missing_trait) = missing_trait { span_note!(fcx.tcx().sess, lhs_expr.span, - "an implementation of `{}` might be missing for `{}` \ - or one of its type arguments", + "an implementation of `{}` might be missing for `{}`", missing_trait, lhs_ty); } } diff --git a/src/test/compile-fail/issue-28837.rs b/src/test/compile-fail/issue-28837.rs index 6baaebc3244..c7cf63bf2c4 100644 --- a/src/test/compile-fail/issue-28837.rs +++ b/src/test/compile-fail/issue-28837.rs @@ -13,48 +13,48 @@ struct A; 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` or + 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` or one of + //~^ 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` or one of + //~^ 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` or one of + //~^ 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` or one of + //~^ 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` or one of + //~^ 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` or one of + //~^ 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` or one of + //~^ 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` or one of + //~^ 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` or one of + //~^ 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` or one of + //~^ 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` or one of + //~^ 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` or one of + //~^ 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` or one of + //~^ 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` or one of + //~^ NOTE an implementation of `std::cmp::PartialOrd` might be missing for `A` }