Skip to content

Commit a4dd850

Browse files
committed
Auto merge of rust-lang#74735 - Aaron1011:fix/wf-impl-self-type, r=estebank
Use the proper span when WF-checking an impl self type
2 parents 8e5489c + 116ad51 commit a4dd850

File tree

7 files changed

+43
-12
lines changed

7 files changed

+43
-12
lines changed

src/librustc_trait_selection/traits/wf.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -300,13 +300,21 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
300300
trait_ref
301301
.substs
302302
.iter()
303-
.filter(|arg| {
303+
.enumerate()
304+
.filter(|(_, arg)| {
304305
matches!(arg.unpack(), GenericArgKind::Type(..) | GenericArgKind::Const(..))
305306
})
306-
.filter(|arg| !arg.has_escaping_bound_vars())
307-
.map(|arg| {
307+
.filter(|(_, arg)| !arg.has_escaping_bound_vars())
308+
.map(|(i, arg)| {
309+
let mut new_cause = cause.clone();
310+
// The first subst is the self ty - use the correct span for it.
311+
if i == 0 {
312+
if let Some(hir::ItemKind::Impl { self_ty, .. }) = item.map(|i| &i.kind) {
313+
new_cause.make_mut().span = self_ty.span;
314+
}
315+
}
308316
traits::Obligation::new(
309-
cause.clone(),
317+
new_cause,
310318
param_env,
311319
ty::PredicateKind::WellFormed(arg).to_predicate(tcx),
312320
)

src/test/ui/coherence/coherence-impl-trait-for-trait-object-safe.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
error[E0038]: the trait `NotObjectSafe` cannot be made into an object
2-
--> $DIR/coherence-impl-trait-for-trait-object-safe.rs:7:6
2+
--> $DIR/coherence-impl-trait-for-trait-object-safe.rs:7:24
33
|
44
LL | trait NotObjectSafe { fn eq(&self, other: Self); }
55
| ------------- ---- ...because method `eq` references the `Self` type in this parameter
66
| |
77
| this trait cannot be made into an object...
88
LL | impl NotObjectSafe for dyn NotObjectSafe { }
9-
| ^^^^^^^^^^^^^ the trait `NotObjectSafe` cannot be made into an object
9+
| ^^^^^^^^^^^^^^^^^ the trait `NotObjectSafe` cannot be made into an object
1010
|
1111
= help: consider moving `eq` to another trait
1212

src/test/ui/feature-gates/feature-gate-object_safe_for_dispatch.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ LL | fn return_non_object_safe_rc() -> std::rc::Rc<dyn NonObjectSafe4> {
5252
= help: consider moving `foo` to another trait
5353

5454
error[E0038]: the trait `NonObjectSafe1` cannot be made into an object
55-
--> $DIR/feature-gate-object_safe_for_dispatch.rs:38:6
55+
--> $DIR/feature-gate-object_safe_for_dispatch.rs:38:16
5656
|
5757
LL | trait NonObjectSafe1: Sized {}
5858
| -------------- ----- ...because it requires `Self: Sized`
5959
| |
6060
| this trait cannot be made into an object...
6161
...
6262
LL | impl Trait for dyn NonObjectSafe1 {}
63-
| ^^^^^ the trait `NonObjectSafe1` cannot be made into an object
63+
| ^^^^^^^^^^^^^^^^^^ the trait `NonObjectSafe1` cannot be made into an object
6464

6565
error: aborting due to 5 previous errors
6666

src/test/ui/issues/issue-21837.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0277]: the trait bound `T: Bound` is not satisfied
2-
--> $DIR/issue-21837.rs:8:9
2+
--> $DIR/issue-21837.rs:8:20
33
|
44
LL | pub struct Foo<T: Bound>(T);
55
| ----- required by this bound in `Foo`
66
...
77
LL | impl<T> Trait2 for Foo<T> {}
8-
| ^^^^^^ the trait `Bound` is not implemented for `T`
8+
| ^^^^^^ the trait `Bound` is not implemented for `T`
99
|
1010
help: consider restricting type parameter `T`
1111
|

src/test/ui/unsized/unsized-trait-impl-self-type.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0277]: the size for values of type `X` cannot be known at compilation time
2-
--> $DIR/unsized-trait-impl-self-type.rs:10:17
2+
--> $DIR/unsized-trait-impl-self-type.rs:10:27
33
|
44
LL | struct S5<Y>(Y);
55
| - required by this bound in `S5`
66
LL |
77
LL | impl<X: ?Sized> T3<X> for S5<X> {
8-
| - ^^^^^ doesn't have a size known at compile-time
8+
| - ^^^^^ doesn't have a size known at compile-time
99
| |
1010
| this type parameter needs to be `std::marker::Sized`
1111
|

src/test/ui/wf/wf-impl-self-type.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Tests that we point at the proper location for an error
2+
// involving the self-type of an impl
3+
4+
trait Foo {}
5+
impl Foo for Option<[u8]> {} //~ ERROR the size for
6+
7+
fn main() {}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
2+
--> $DIR/wf-impl-self-type.rs:5:14
3+
|
4+
LL | impl Foo for Option<[u8]> {}
5+
| ^^^^^^^^^^^^ doesn't have a size known at compile-time
6+
|
7+
::: $SRC_DIR/libcore/option.rs:LL:COL
8+
|
9+
LL | pub enum Option<T> {
10+
| - required by this bound in `std::option::Option`
11+
|
12+
= help: the trait `std::marker::Sized` is not implemented for `[u8]`
13+
14+
error: aborting due to previous error
15+
16+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)