Skip to content
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

Don't Add Specialized Notes to Error Messages Pointing at a Type #121717

Closed
Closed
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
8 changes: 8 additions & 0 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,14 @@ impl<'hir> Map<'hir> {
Some(body_id)
}

/// Check if a given `LocalDefId` is in an impl block.
pub fn is_impl_block(self, id: LocalDefId) -> bool {
matches!(
self.get_if_local(id.to_def_id()),
Some(Node::Item(Item { kind: ItemKind::Impl(_), .. }))
)
}

/// Given a body owner's id, returns the `BodyId` associated with it.
#[track_caller]
pub fn body_owned_by(self, id: LocalDefId) -> BodyId {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,20 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
ty::print::with_no_trimmed_paths!(ty::print::with_no_visible_paths!({
let generics = self.tcx.generics_of(def_id);
let self_ty = trait_ref.self_ty();
// This is also included through the generics list as `Self`,
// but the parser won't allow you to use it
flags.push((sym::_Self, Some(self_ty.to_string())));
if let Some(def) = self_ty.ty_adt_def() {
// We also want to be able to select self's original
// signature with no type arguments resolved
flags.push((
sym::_Self,
Some(self.tcx.type_of(def.did()).instantiate_identity().to_string()),
));

// Don't add specialized notes on types
if !self.tcx.hir().is_impl_block(obligation.cause.body_id) {
Comment on lines +154 to +155
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of flat out removing the information from rustc_on_unimplemented, wouldn't it be better to add the information that this obligation is coming from an impl block so that it can be specially targeted for custom notes too? I'm thinking of things like "implementing this trait requires you to implement this other one because foo". That would require changing the rustc_on_unimplemented annotation itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the comments.

Based on the suggestions, the error would roughly look like this?

error[E0277]: `Vec<T>` does not implement the trait `Iterator`
  --> test.rs:12:17
   |
12 | impl<T> Bar for Vec<T> {}
   |                 ^^^^^^ Implementing this trait for `Vec<T>` requires you to implement trait `Iterator` for `Vec<T>`
   |
   = help: consider implementing the trait `Iterator` for `Vec<T>`
note: required by a bound in `Bar`
  --> test.rs:10:12
   |
10 | trait Bar: Iterator {}
   |            ^^^^^^^^ required by this bound in `Bar`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, that's exactly the right amount of context needed for this.

// This is also included through the generics list as `Self`,
// but the parser won't allow you to use it
flags.push((sym::_Self, Some(self_ty.to_string())));
if let Some(def) = self_ty.ty_adt_def() {
// We also want to be able to select self's original
// signature with no type arguments resolved
flags.push((
sym::_Self,
Some(self.tcx.type_of(def.did()).instantiate_identity().to_string()),
));
}
}

for param in generics.params.iter() {
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/errors/traits/no-specialized-error-for-types-121398.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
trait Bar: Iterator {}

impl Bar for String {} //~ ERROR `String` is not an iterator [E0277]

impl<T> Bar for Vec<T> {} //~ ERROR `Vec<T>` is not an iterator [E0277]

use std::ops::IndexMut;
trait Foo<Idx>: IndexMut<Idx> {}

impl<Idx> Foo<Idx> for String {} //~ ERROR the type `String` cannot be mutably indexed by `Idx` [E0277]

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
error[E0277]: `String` is not an iterator
--> $DIR/no-specialized-error-for-types-121398.rs:3:14
|
LL | impl Bar for String {}
| ^^^^^^ `String` is not an iterator
|
= help: the trait `Iterator` is not implemented for `String`
note: required by a bound in `Bar`
--> $DIR/no-specialized-error-for-types-121398.rs:1:12
|
LL | trait Bar: Iterator {}
| ^^^^^^^^ required by this bound in `Bar`

error[E0277]: `Vec<T>` is not an iterator
--> $DIR/no-specialized-error-for-types-121398.rs:5:17
|
LL | impl<T> Bar for Vec<T> {}
| ^^^^^^ `Vec<T>` is not an iterator
|
= help: the trait `Iterator` is not implemented for `Vec<T>`
note: required by a bound in `Bar`
--> $DIR/no-specialized-error-for-types-121398.rs:1:12
|
LL | trait Bar: Iterator {}
| ^^^^^^^^ required by this bound in `Bar`

error[E0277]: the type `String` cannot be mutably indexed by `Idx`
--> $DIR/no-specialized-error-for-types-121398.rs:10:24
|
LL | impl<Idx> Foo<Idx> for String {}
| ^^^^^^ `String` cannot be mutably indexed by `Idx`
|
= help: the trait `IndexMut<Idx>` is not implemented for `String`
note: required by a bound in `Foo`
--> $DIR/no-specialized-error-for-types-121398.rs:8:17
|
LL | trait Foo<Idx>: IndexMut<Idx> {}
| ^^^^^^^^^^^^^ required by this bound in `Foo`
help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement
|
LL | impl<Idx> Foo<Idx> for String where String: IndexMut<Idx> {}
| +++++++++++++++++++++++++++

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0277`.
4 changes: 4 additions & 0 deletions tests/ui/errors/traits/specialized-error-for-expr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
let s = String::from("hello");
for _ in s {} //~ `String` is not an iterator [E0277]
}
12 changes: 12 additions & 0 deletions tests/ui/errors/traits/specialized-error-for-expr.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0277]: `String` is not an iterator
--> $DIR/specialized-error-for-expr.rs:3:14
|
LL | for _ in s {}
| ^ `String` is not an iterator; try calling `.chars()` or `.bytes()`
|
= help: the trait `Iterator` is not implemented for `String`, which is required by `String: IntoIterator`
= note: required for `String` to implement `IntoIterator`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.
Loading