Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 59edd67

Browse files
committed
Auto merge of rust-lang#116497 - compiler-errors:impl-span, r=cjgillot
Extend `impl`'s `def_span` to include its where clauses Typically, we highlight the def-span of an impl in a diagnostic due to either: 1. coherence error 2. trait evaluation cycle 3. invalid implementation of built-in trait I find that an impl's where clauses are very often required to understanding why these errors come about, which is unfortunate since where clauses may be located on different lines and don't show up in the error. This PR expands the def-span of impls to include these where clauses. r? cjgillot since you've touched this code a while back to make some spans shorter, but you can also reassign to wg-diagnostics or compiler if you're busy or have no strong opinions.
2 parents cdddcd3 + 592163f commit 59edd67

30 files changed

+122
-69
lines changed

compiler/rustc_middle/src/hir/map/mod.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -970,12 +970,15 @@ impl<'hir> Map<'hir> {
970970
// SyntaxContext of the visibility.
971971
sig.span.find_ancestor_in_same_ctxt(*outer_span).unwrap_or(*outer_span)
972972
}
973+
// Impls, including their where clauses.
974+
Node::Item(Item {
975+
kind: ItemKind::Impl(Impl { generics, .. }),
976+
span: outer_span,
977+
..
978+
}) => until_within(*outer_span, generics.where_clause_span),
973979
// Constants and Statics.
974980
Node::Item(Item {
975-
kind:
976-
ItemKind::Const(ty, ..)
977-
| ItemKind::Static(ty, ..)
978-
| ItemKind::Impl(Impl { self_ty: ty, .. }),
981+
kind: ItemKind::Const(ty, ..) | ItemKind::Static(ty, ..),
979982
span: outer_span,
980983
..
981984
})

src/tools/clippy/tests/ui/crashes/ice-6252.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ LL | const VAL: T;
3131
| ------------ `VAL` from trait
3232
...
3333
LL | impl<N, M> TypeVal<usize> for Multiply<N, M> where N: TypeVal<VAL> {}
34-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `VAL` in implementation
34+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `VAL` in implementation
3535

3636
error: aborting due to 3 previous errors
3737

tests/ui/associated-types/associated-types-coherence-failure.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ error[E0119]: conflicting implementations of trait `IntoCow<'_, _>` for type `Co
22
--> $DIR/associated-types-coherence-failure.rs:21:1
33
|
44
LL | impl<'a, B: ?Sized> IntoCow<'a, B> for <B as ToOwned>::Owned where B: ToOwned {
5-
| ------------------------------------------------------------ first implementation here
5+
| ----------------------------------------------------------------------------- first implementation here
66
...
77
LL | impl<'a, B: ?Sized> IntoCow<'a, B> for Cow<'a, B> where B: ToOwned {
8-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `Cow<'_, _>`
8+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `Cow<'_, _>`
99

1010
error[E0119]: conflicting implementations of trait `IntoCow<'_, _>` for type `&_`
1111
--> $DIR/associated-types-coherence-failure.rs:28:1
1212
|
1313
LL | impl<'a, B: ?Sized> IntoCow<'a, B> for <B as ToOwned>::Owned where B: ToOwned {
14-
| ------------------------------------------------------------ first implementation here
14+
| ----------------------------------------------------------------------------- first implementation here
1515
...
1616
LL | impl<'a, B: ?Sized> IntoCow<'a, B> for &'a B where B: ToOwned {
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `&_`
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `&_`
1818

1919
error: aborting due to 2 previous errors
2020

tests/ui/associated-types/hr-associated-type-bound-2.stderr

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error[E0275]: overflow evaluating the requirement `for<'b> u32: X<'b>`
22
--> $DIR/hr-associated-type-bound-2.rs:11:1
33
|
4-
LL | impl X<'_> for u32
5-
| ^^^^^^^^^^^^^^^^^^
4+
LL | / impl X<'_> for u32
5+
LL | | where
6+
LL | | for<'b> <Self as X<'b>>::U: Clone,
7+
| |______________________________________^
68
|
79
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`hr_associated_type_bound_2`)
810
note: required for `u32` to implement `for<'b> X<'b>`

tests/ui/associated-types/impl-wf-cycle-1.stderr

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
error[E0275]: overflow evaluating the requirement `<(T,) as Grault>::A == _`
22
--> $DIR/impl-wf-cycle-1.rs:15:1
33
|
4-
LL | impl<T: Grault> Grault for (T,)
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4+
LL | / impl<T: Grault> Grault for (T,)
5+
LL | |
6+
LL | | where
7+
LL | | Self::A: Baz,
8+
LL | | Self::B: Fiz,
9+
| |_________________^
610
|
711
note: required for `(T,)` to implement `Grault`
812
--> $DIR/impl-wf-cycle-1.rs:15:17

tests/ui/associated-types/impl-wf-cycle-2.stderr

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
error[E0275]: overflow evaluating the requirement `<(T,) as Grault>::A == _`
22
--> $DIR/impl-wf-cycle-2.rs:7:1
33
|
4-
LL | impl<T: Grault> Grault for (T,)
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4+
LL | / impl<T: Grault> Grault for (T,)
5+
LL | |
6+
LL | | where
7+
LL | | Self::A: Copy,
8+
| |__________________^
69
|
710
note: required for `(T,)` to implement `Grault`
811
--> $DIR/impl-wf-cycle-2.rs:7:17

tests/ui/coherence/coherence-overlap-downstream.next.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ error[E0119]: conflicting implementations of trait `Foo<_>` for type `i32`
1010
--> $DIR/coherence-overlap-downstream.rs:17:1
1111
|
1212
LL | impl<X, T> Foo<X> for T where T: Bar<X> {}
13-
| ----------------------- first implementation here
13+
| --------------------------------------- first implementation here
1414
LL | impl<X> Foo<X> for i32 {}
1515
| ^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `i32`
1616
|

tests/ui/coherence/coherence-overlap-downstream.old.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ error[E0119]: conflicting implementations of trait `Foo<_>` for type `i32`
1010
--> $DIR/coherence-overlap-downstream.rs:17:1
1111
|
1212
LL | impl<X, T> Foo<X> for T where T: Bar<X> {}
13-
| ----------------------- first implementation here
13+
| --------------------------------------- first implementation here
1414
LL | impl<X> Foo<X> for i32 {}
1515
| ^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `i32`
1616
|

tests/ui/coherence/coherence-overlap-upstream.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0119]: conflicting implementations of trait `Foo` for type `i16`
22
--> $DIR/coherence-overlap-upstream.rs:13:1
33
|
44
LL | impl<T> Foo for T where T: Remote {}
5-
| ----------------- first implementation here
5+
| --------------------------------- first implementation here
66
LL | impl Foo for i16 {}
77
| ^^^^^^^^^^^^^^^^ conflicting implementation for `i16`
88
|

tests/ui/coherence/coherence-wasm-bindgen.stderr

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
error: conflicting implementations of trait `IntoWasmAbi` for type `&dyn Fn(&_) -> _`
22
--> $DIR/coherence-wasm-bindgen.rs:28:1
33
|
4-
LL | impl<'a, 'b, A, R> IntoWasmAbi for &'a (dyn Fn(A) -> R + 'b)
5-
| ------------------------------------------------------------ first implementation here
4+
LL | / impl<'a, 'b, A, R> IntoWasmAbi for &'a (dyn Fn(A) -> R + 'b)
5+
LL | | where
6+
LL | | A: FromWasmAbi,
7+
LL | | R: ReturnWasmAbi,
8+
| |_____________________- first implementation here
69
...
7-
LL | impl<'a, 'b, A, R> IntoWasmAbi for &'a (dyn for<'x> Fn(&'x A) -> R + 'b)
8-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `&dyn Fn(&_) -> _`
10+
LL | / impl<'a, 'b, A, R> IntoWasmAbi for &'a (dyn for<'x> Fn(&'x A) -> R + 'b)
11+
LL | | where
12+
LL | | A: RefFromWasmAbi,
13+
LL | | R: ReturnWasmAbi,
14+
| |_____________________^ conflicting implementation for `&dyn Fn(&_) -> _`
915
|
1016
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
1117
= note: for more information, see issue #56105 <https://github.com/rust-lang/rust/issues/56105>

0 commit comments

Comments
 (0)