Skip to content

Commit

Permalink
Added defined here note for non-local associated types
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLazyDutchman committed Dec 6, 2023
1 parent 1404dbd commit 1476d5c
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 9 deletions.
20 changes: 11 additions & 9 deletions compiler/rustc_hir_analysis/src/astconv/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,22 +669,24 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
{
is_shadowed = true;

let is_local = tcx.hir().get_if_local(assoc_item.def_id).is_some();
let rename_message = if is_local { ", consider renaming it" } else { "" };
let rename_message =
if assoc_item.def_id.is_local() { ", consider renaming it" } else { "" };
err.span_label(
tcx.def_span(assoc_item.def_id),
format!("`{}{}` shadowed here{}", prefix, item.name, rename_message),
);
}

let rename_message = if is_shadowed { ", consider renaming it" } else { "" };
let rename_message = if is_shadowed && item.def_id.is_local() {
", consider renaming it"
} else {
""
};

if let Some(sp) = tcx.hir().span_if_local(item.def_id) {
err.span_label(
sp,
format!("`{}{}` defined here{}", prefix, item.name, rename_message),
);
}
err.span_label(
tcx.def_span(item.def_id),
format!("`{}{}` defined here{}", prefix, item.name, rename_message),
);
}
if potential_assoc_types.len() == assoc_items.len() {
// When the amount of missing associated types equals the number of
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/associated-type-bounds/overlaping-bound-suggestion.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ LL | inner: <IntoIterator<Item: IntoIterator<Item: >>::IntoIterator as Item>
| | |
| | associated types `Item`, `IntoIter` must be specified
| associated types `Item`, `IntoIter` must be specified
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
|
= note: `Item` defined here
|
= note: `Item` defined here
::: $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
|
= note: `IntoIter` defined here
|
= note: `IntoIter` defined here

error[E0223]: ambiguous associated type
--> $DIR/overlaping-bound-suggestion.rs:7:13
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ LL | type Item;
...
LL | impl<T> Clone for Box<dyn NewIter<Item = T>> {
| ^^^^^^^^^^^^^^^^^ associated type `Item` must be specified
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
|
= note: `Iterator::Item` defined here

error: aborting due to 1 previous error

Expand Down
41 changes: 41 additions & 0 deletions tests/ui/associated-types/missing-associated-types.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ LL | type Foo<Rhs> = dyn Add<Rhs> + Sub<Rhs> + X<Rhs> + Y<Rhs>;
| | | associated type `Output` must be specified
| | associated type `Output` must be specified
| associated type `Output` must be specified
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
|
= note: `Output` defined here
::: $SRC_DIR/core/src/ops/arith.rs:LL:COL
|
= note: `Output` defined here
::: $SRC_DIR/core/src/ops/arith.rs:LL:COL
|
= note: `Output` defined here
|
help: specify the associated types
|
Expand Down Expand Up @@ -52,6 +61,20 @@ LL | type Bar<Rhs> = dyn Add<Rhs> + Sub<Rhs> + X<Rhs> + Z<Rhs>;
| | | associated types `Output` (from trait `Div`), `Output` (from trait `Mul`) must be specified
| | associated type `Output` must be specified
| associated type `Output` must be specified
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
|
= note: `Output` defined here
::: $SRC_DIR/core/src/ops/arith.rs:LL:COL
|
= note: `Output` defined here
::: $SRC_DIR/core/src/ops/arith.rs:LL:COL
|
= note: `Mul::Output` defined here
::: $SRC_DIR/core/src/ops/arith.rs:LL:COL
|
= note: `Output` defined here
|
= note: `Div::Output` defined here
|
help: consider introducing a new type parameter, adding `where` constraints using the fully-qualified path to the associated types
--> $DIR/missing-associated-types.rs:15:43
Expand Down Expand Up @@ -85,6 +108,12 @@ LL | type Baz<Rhs> = dyn Add<Rhs> + Sub<Rhs> + Y<Rhs>;
| | |
| | associated type `Output` must be specified
| associated type `Output` must be specified
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
|
= note: `Output` defined here
::: $SRC_DIR/core/src/ops/arith.rs:LL:COL
|
= note: `Output` defined here
|
help: specify the associated types
|
Expand All @@ -109,6 +138,12 @@ LL | type Bat<Rhs> = dyn Add<Rhs> + Sub<Rhs> + Fine<Rhs>;
| ^^^^^^^^ ^^^^^^^^ associated type `Output` must be specified
| |
| associated type `Output` must be specified
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
|
= note: `Output` defined here
::: $SRC_DIR/core/src/ops/arith.rs:LL:COL
|
= note: `Output` defined here
|
help: specify the associated types
|
Expand All @@ -120,6 +155,12 @@ error[E0191]: the value of the associated types `Output` in `Div`, `Output` in `
|
LL | type Bal<Rhs> = dyn X<Rhs>;
| ^^^^^^ associated types `Output` (from trait `Div`), `Output` (from trait `Mul`) must be specified
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
|
= note: `Mul::Output` defined here
::: $SRC_DIR/core/src/ops/arith.rs:LL:COL
|
= note: `Div::Output` defined here
|
= help: consider introducing a new type parameter, adding `where` constraints using the fully-qualified path to the associated types

Expand Down
3 changes: 3 additions & 0 deletions tests/ui/issues/issue-23024.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ error[E0191]: the value of the associated type `Output` in `FnOnce` must be spec
|
LL | println!("{:?}",(vfnfer[0] as dyn Fn)(3));
| ^^ help: specify the associated type: `Fn<Output = Type>`
--> $SRC_DIR/core/src/ops/function.rs:LL:COL
|
= note: `Output` defined here

error: aborting due to 3 previous errors

Expand Down
6 changes: 6 additions & 0 deletions tests/ui/issues/issue-28344.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ error[E0191]: the value of the associated type `Output` in `BitXor` must be spec
|
LL | let x: u8 = BitXor::bitor(0 as u8, 0 as u8);
| ^^^^^^ help: specify the associated type: `BitXor<Output = Type>`
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL
|
= note: `Output` defined here

error[E0599]: no function or associated item named `bitor` found for trait object `dyn BitXor<_>` in the current scope
--> $DIR/issue-28344.rs:4:25
Expand Down Expand Up @@ -45,6 +48,9 @@ error[E0191]: the value of the associated type `Output` in `BitXor` must be spec
|
LL | let g = BitXor::bitor;
| ^^^^^^ help: specify the associated type: `BitXor<Output = Type>`
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL
|
= note: `Output` defined here

error[E0599]: no function or associated item named `bitor` found for trait object `dyn BitXor<_>` in the current scope
--> $DIR/issue-28344.rs:10:21
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/suggestions/trait-hidden-method.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ error[E0191]: the value of the associated type `Item` in `Iterator` must be spec
|
LL | Box::new(1..=10) as Box<dyn Iterator>
| ^^^^^^^^ help: specify the associated type: `Iterator<Item = Type>`
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
|
= note: `Item` defined here

error[E0271]: expected `Box<dyn Iterator>` to be an iterator that yields `u32`, but it yields `<dyn Iterator as Iterator>::Item`
--> $DIR/trait-hidden-method.rs:3:32
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/traits/alias/object-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ error[E0191]: the value of the associated type `Item` in `Iterator` must be spec
|
LL | let _: &dyn IteratorAlias = &vec![123].into_iter();
| ^^^^^^^^^^^^^ help: specify the associated type: `IteratorAlias<Item = Type>`
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
|
= note: `Item` defined here

error: aborting due to 2 previous errors

Expand Down

0 comments on commit 1476d5c

Please sign in to comment.