Skip to content

Commit 747287a

Browse files
Add better error message when == operator is badly used
1 parent 9f15631 commit 747287a

File tree

16 files changed

+72
-57
lines changed

16 files changed

+72
-57
lines changed

src/librustc/traits/error_reporting.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,23 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
559559
trait_ref.to_predicate(),
560560
post_message);
561561

562+
let unimplemented_note = self.on_unimplemented_note(trait_ref, obligation);
563+
if let Some(ref s) = unimplemented_note {
564+
// If it has a custom "#[rustc_on_unimplemented]"
565+
// error message, let's display it as the label!
566+
err.span_label(span, s.as_str());
567+
err.help(&format!("{}the trait `{}` is not implemented for `{}`",
568+
pre_message,
569+
trait_ref,
570+
trait_ref.self_ty()));
571+
} else {
572+
err.span_label(span,
573+
&*format!("{}the trait `{}` is not implemented for `{}`",
574+
pre_message,
575+
trait_ref,
576+
trait_ref.self_ty()));
577+
}
578+
562579
// Try to report a help message
563580
if !trait_ref.has_infer_types() &&
564581
self.predicate_can_apply(trait_ref) {
@@ -571,21 +588,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
571588
// which is somewhat confusing.
572589
err.help(&format!("consider adding a `where {}` bound",
573590
trait_ref.to_predicate()));
574-
} else if let Some(s) = self.on_unimplemented_note(trait_ref, obligation) {
575-
// If it has a custom "#[rustc_on_unimplemented]"
576-
// error message, let's display it!
577-
err.note(&s);
578-
} else {
591+
} else if unimplemented_note.is_none() {
579592
// Can't show anything else useful, try to find similar impls.
580593
let impl_candidates = self.find_similar_impl_candidates(trait_ref);
581594
self.report_similar_impl_candidates(impl_candidates, &mut err);
582595
}
583596

584-
err.span_label(span,
585-
format!("{}the trait `{}` is not implemented for `{}`",
586-
pre_message,
587-
trait_ref,
588-
trait_ref.self_ty()));
589597
err
590598
}
591599

src/test/compile-fail/E0277-2.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ fn is_send<T: Send>() { }
2525
fn main() {
2626
is_send::<Foo>();
2727
//~^ ERROR the trait bound `*const u8: std::marker::Send` is not satisfied in `Foo`
28-
//~| NOTE within `Foo`, the trait `std::marker::Send` is not implemented for `*const u8`
2928
//~| NOTE: `*const u8` cannot be sent between threads safely
3029
//~| NOTE: required because it appears within the type `Baz`
3130
//~| NOTE: required because it appears within the type `Bar`

src/test/compile-fail/E0277.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ fn some_func<T: Foo>(foo: T) {
2020

2121
fn f(p: Path) { }
2222
//~^ ERROR the trait bound `[u8]: std::marker::Sized` is not satisfied in `std::path::Path`
23-
//~| NOTE within `std::path::Path`, the trait `std::marker::Sized` is not implemented for `[u8]`
2423
//~| NOTE `[u8]` does not have a constant size known at compile-time
2524
//~| NOTE required because it appears within the type `std::path::Path`
2625
//~| NOTE all local variables must have a statically known size

src/test/compile-fail/const-unsized.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,21 @@ use std::fmt::Debug;
1212

1313
const CONST_0: Debug+Sync = *(&0 as &(Debug+Sync));
1414
//~^ ERROR `std::fmt::Debug + std::marker::Sync + 'static: std::marker::Sized` is not satisfied
15-
//~| NOTE the trait `std::marker::Sized` is not implemented for `std::fmt::Debug + std::marker::Syn
1615
//~| NOTE does not have a constant size known at compile-time
1716
//~| NOTE constant expressions must have a statically known size
1817

1918
const CONST_FOO: str = *"foo";
2019
//~^ ERROR `str: std::marker::Sized` is not satisfied
21-
//~| NOTE the trait `std::marker::Sized` is not implemented for `str`
2220
//~| NOTE does not have a constant size known at compile-time
2321
//~| NOTE constant expressions must have a statically known size
2422

2523
static STATIC_1: Debug+Sync = *(&1 as &(Debug+Sync));
2624
//~^ ERROR `std::fmt::Debug + std::marker::Sync + 'static: std::marker::Sized` is not satisfied
27-
//~| NOTE the trait `std::marker::Sized` is not implemented for `std::fmt::Debug + std::marker::Syn
2825
//~| NOTE does not have a constant size known at compile-time
2926
//~| NOTE constant expressions must have a statically known size
3027

3128
static STATIC_BAR: str = *"bar";
3229
//~^ ERROR `str: std::marker::Sized` is not satisfied
33-
//~| NOTE the trait `std::marker::Sized` is not implemented for `str`
3430
//~| NOTE does not have a constant size known at compile-time
3531
//~| NOTE constant expressions must have a statically known size
3632

src/test/compile-fail/impl-trait/auto-trait-leak.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@ fn send<T: Send>(_: T) {}
2626
fn main() {
2727
send(before());
2828
//~^ ERROR the trait bound `std::rc::Rc<std::cell::Cell<i32>>: std::marker::Send` is not satisfied
29-
//~| NOTE the trait `std::marker::Send` is not implemented for `std::rc::Rc<std::cell::Cell<i32>>`
3029
//~| NOTE `std::rc::Rc<std::cell::Cell<i32>>` cannot be sent between threads safely
3130
//~| NOTE required because it appears within the type `[closure
3231
//~| NOTE required because it appears within the type `impl std::ops::Fn<(i32,)>`
3332
//~| NOTE required by `send`
3433

3534
send(after());
3635
//~^ ERROR the trait bound `std::rc::Rc<std::cell::Cell<i32>>: std::marker::Send` is not satisfied
37-
//~| NOTE the trait `std::marker::Send` is not implemented for `std::rc::Rc<std::cell::Cell<i32>>`
3836
//~| NOTE `std::rc::Rc<std::cell::Cell<i32>>` cannot be sent between threads safely
3937
//~| NOTE required because it appears within the type `[closure
4038
//~| NOTE required because it appears within the type `impl std::ops::Fn<(i32,)>`

src/test/compile-fail/on-unimplemented/multiple-impls.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,14 @@ impl Index<Bar<usize>> for [i32] {
4242
fn main() {
4343
Index::index(&[] as &[i32], 2u32);
4444
//~^ ERROR E0277
45-
//~| NOTE the trait `Index<u32>` is not implemented for `[i32]`
4645
//~| NOTE trait message
4746
//~| NOTE required by
4847
Index::index(&[] as &[i32], Foo(2u32));
4948
//~^ ERROR E0277
50-
//~| NOTE the trait `Index<Foo<u32>>` is not implemented for `[i32]`
5149
//~| NOTE on impl for Foo
5250
//~| NOTE required by
5351
Index::index(&[] as &[i32], Bar(2u32));
5452
//~^ ERROR E0277
55-
//~| NOTE the trait `Index<Bar<u32>>` is not implemented for `[i32]`
5653
//~| NOTE on impl for Bar
5754
//~| NOTE required by
5855
}

src/test/compile-fail/on-unimplemented/on-impl.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ impl Index<usize> for [i32] {
3131
fn main() {
3232
Index::<u32>::index(&[1, 2, 3] as &[i32], 2u32);
3333
//~^ ERROR E0277
34-
//~| NOTE the trait `Index<u32>` is not implemented for `[i32]`
3534
//~| NOTE a usize is required
3635
//~| NOTE required by
3736
}

src/test/compile-fail/on-unimplemented/on-trait.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ pub fn main() {
3535
//~^ ERROR
3636
//~^^ NOTE a collection of type `std::option::Option<std::vec::Vec<u8>>` cannot be built from an iterator over elements of type `&u8`
3737
//~^^^ NOTE required by `collect`
38-
//~| NOTE the trait `MyFromIterator<&u8>` is not implemented for `std::option::Option<std::vec::Vec<u8>>`
38+
3939
let x: String = foobar(); //~ ERROR
4040
//~^ NOTE test error `std::string::String` with `u8` `_` `u32`
4141
//~^^ NOTE required by `foobar`
42-
//~| NOTE the trait `Foo<u8, _, u32>` is not implemented for `std::string::String`
4342
}

src/test/compile-fail/on-unimplemented/slice-index.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ fn main() {
2020
let x = &[1, 2, 3] as &[i32];
2121
x[1i32]; //~ ERROR E0277
2222
//~| NOTE slice indices are of type `usize` or ranges of `usize`
23-
//~| NOTE trait `std::slice::SliceIndex<[i32]>` is not implemented for `i32`
2423
//~| NOTE required because of the requirements on the impl of `std::ops::Index<i32>`
2524
x[..1i32]; //~ ERROR E0277
2625
//~| NOTE slice indices are of type `usize` or ranges of `usize`
27-
//~| NOTE trait `std::slice::SliceIndex<[i32]>` is not implemented for `std::ops::RangeTo<i32>`
2826
//~| NOTE requirements on the impl of `std::ops::Index<std::ops::RangeTo<i32>>`
2927
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2017 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+
fn foo<T: PartialEq>(a: &T, b: T) {
12+
a == b; //~ ERROR E0277
13+
//~| NOTE can't compare `&T` with `T`
14+
//~| HELP the trait `std::cmp::PartialEq<T>` is not implemented for `&T`
15+
//~| HELP consider adding a `where &T: std::cmp::PartialEq<T>` bound
16+
}
17+
18+
fn main() {
19+
foo(&1, 1);
20+
}

0 commit comments

Comments
 (0)