Skip to content

Commit cbaf112

Browse files
committed
Find span for inner type affected in trait bound
Try to find a more specific span to point to when an obligation affects a type in a type parameter: ``` error[E0277]: the trait bound `String: Copy` is not satisfied --> $DIR/unsatisfied-bounds-where-clause-on-assoc-ty.rs:13:16 | LL | let _: S::<String>::X; | ^^^^^^ the trait `Copy` is not implemented for `String` | note: required by a bound in `S<T>::X` --> $DIR/unsatisfied-bounds-where-clause-on-assoc-ty.rs:9:12 | LL | type X = () | - required by a bound in this associated type LL | where LL | T: Copy; | ^^^^ required by this bound in `S<T>::X` ```
1 parent 7278554 commit cbaf112

19 files changed

+88
-49
lines changed

compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,27 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
269269
(err_msg, None)
270270
};
271271

272+
let mut expr_finder = FindExprBySpan::new(span, self.tcx);
273+
274+
let hir_id = self.tcx.local_def_id_to_hir_id(obligation.cause.body_id);
275+
match self.tcx.hir_node(hir_id) {
276+
hir::Node::Item(item) => expr_finder.visit_item(item),
277+
hir::Node::TraitItem(item) => expr_finder.visit_trait_item(item),
278+
hir::Node::ImplItem(item) => expr_finder.visit_impl_item(item),
279+
_ => {}
280+
}
281+
if let Some(ty) = expr_finder.ty_result {
282+
let self_ty = main_trait_predicate.skip_binder().self_ty();
283+
let mut tyfinder = FindType { ty: self_ty, spans: vec![] };
284+
tyfinder.visit_ty(unsafe { std::mem::transmute(ty) });
285+
if let [sp] = tyfinder.spans[..]
286+
&& !sp.from_expansion()
287+
&& !span.from_expansion()
288+
{
289+
span = sp;
290+
}
291+
}
292+
272293
let mut err = struct_span_code_err!(self.dcx(), span, E0277, "{}", err_msg);
273294
*err.long_ty_path() = long_ty_file;
274295

@@ -3361,3 +3382,20 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
33613382
}
33623383
}
33633384
}
3385+
3386+
struct FindType<'tcx> {
3387+
ty: Ty<'tcx>,
3388+
spans: Vec<Span>,
3389+
}
3390+
3391+
impl<'v> Visitor<'v> for FindType<'v> {
3392+
fn visit_path(&mut self, path: &hir::Path<'v>, _: hir::HirId) {
3393+
match (path.res, self.ty.kind()) {
3394+
(hir::def::Res::Def(_kind, def_id), ty::Adt(def, _)) if def_id == def.did() => {
3395+
self.spans.push(path.span);
3396+
}
3397+
_ => {}
3398+
}
3399+
hir::intravisit::walk_path(self, path)
3400+
}
3401+
}

compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl<'hir> FindExprBySpan<'hir> {
6969
}
7070

7171
impl<'v> Visitor<'v> for FindExprBySpan<'v> {
72-
type NestedFilter = rustc_middle::hir::nested_filter::OnlyBodies;
72+
type NestedFilter = rustc_middle::hir::nested_filter::All;
7373

7474
fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
7575
self.tcx

tests/ui/associated-inherent-types/generic-associated-types-bad.item.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: the trait bound `String: Copy` is not satisfied
2-
--> $DIR/generic-associated-types-bad.rs:16:10
2+
--> $DIR/generic-associated-types-bad.rs:16:17
33
|
44
LL | const _: Ty::Pr<String> = String::new();
5-
| ^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
5+
| ^^^^^^ the trait `Copy` is not implemented for `String`
66
|
77
note: required by a bound in `Ty::Pr`
88
--> $DIR/generic-associated-types-bad.rs:10:16

tests/ui/associated-inherent-types/generic-associated-types-bad.local.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: the trait bound `Vec<()>: Copy` is not satisfied
2-
--> $DIR/generic-associated-types-bad.rs:21:12
2+
--> $DIR/generic-associated-types-bad.rs:21:19
33
|
44
LL | let _: Ty::Pr<Vec<()>>;
5-
| ^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `Vec<()>`
5+
| ^^^^^^^ the trait `Copy` is not implemented for `Vec<()>`
66
|
77
note: required by a bound in `Ty::Pr`
88
--> $DIR/generic-associated-types-bad.rs:10:16

tests/ui/associated-inherent-types/unsatisfied-bounds-where-clause-on-assoc-ty.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: the trait bound `String: Copy` is not satisfied
2-
--> $DIR/unsatisfied-bounds-where-clause-on-assoc-ty.rs:13:12
2+
--> $DIR/unsatisfied-bounds-where-clause-on-assoc-ty.rs:13:16
33
|
44
LL | let _: S::<String>::X;
5-
| ^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
5+
| ^^^^^^ the trait `Copy` is not implemented for `String`
66
|
77
note: required by a bound in `S<T>::X`
88
--> $DIR/unsatisfied-bounds-where-clause-on-assoc-ty.rs:9:12

tests/ui/auto-traits/issue-83857-ub.stderr

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: `Foo<T, U>` cannot be sent between threads safely
2-
--> $DIR/issue-83857-ub.rs:21:38
2+
--> $DIR/issue-83857-ub.rs:21:39
33
|
44
LL | fn generic<T, U>(v: Foo<T, U>, f: fn(<Foo<T, U> as WithAssoc>::Output) -> i32) {
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Foo<T, U>` cannot be sent between threads safely
5+
| ^^^^^^^^^ `Foo<T, U>` cannot be sent between threads safely
66
|
77
= help: the trait `Send` is not implemented for `Foo<T, U>`
88
note: required for `Foo<T, U>` to implement `WithAssoc`
@@ -18,10 +18,10 @@ LL | fn generic<T, U>(v: Foo<T, U>, f: fn(<Foo<T, U> as WithAssoc>::Output) -> i
1818
| +++++++++++++++++++++
1919

2020
error[E0277]: `Foo<T, U>` cannot be sent between threads safely
21-
--> $DIR/issue-83857-ub.rs:21:35
21+
--> $DIR/issue-83857-ub.rs:21:39
2222
|
2323
LL | fn generic<T, U>(v: Foo<T, U>, f: fn(<Foo<T, U> as WithAssoc>::Output) -> i32) {
24-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Foo<T, U>` cannot be sent between threads safely
24+
| ^^^^^^^^^ `Foo<T, U>` cannot be sent between threads safely
2525
|
2626
= help: the trait `Send` is not implemented for `Foo<T, U>`
2727
note: required for `Foo<T, U>` to implement `WithAssoc`
@@ -31,6 +31,7 @@ LL | impl<T: Send> WithAssoc for T {
3131
| ---- ^^^^^^^^^ ^
3232
| |
3333
| unsatisfied trait bound introduced here
34+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
3435
help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement
3536
|
3637
LL | fn generic<T, U>(v: Foo<T, U>, f: fn(<Foo<T, U> as WithAssoc>::Output) -> i32) where Foo<T, U>: Send {

tests/ui/auto-traits/typeck-default-trait-impl-constituent-types-2.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: the trait bound `MyS2: MyTrait` is not satisfied in `(MyS2, MyS)`
2-
--> $DIR/typeck-default-trait-impl-constituent-types-2.rs:17:18
2+
--> $DIR/typeck-default-trait-impl-constituent-types-2.rs:17:19
33
|
44
LL | is_mytrait::<(MyS2, MyS)>();
5-
| ^^^^^^^^^^^ within `(MyS2, MyS)`, the trait `MyTrait` is not implemented for `MyS2`
5+
| ^^^^ within `(MyS2, MyS)`, the trait `MyTrait` is not implemented for `MyS2`
66
|
77
= note: required because it appears within the type `(MyS2, MyS)`
88
note: required by a bound in `is_mytrait`

tests/ui/const-generics/adt_const_params/const_param_ty_bad_empty_array.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: `NotParam` can't be used as a const parameter type
2-
--> $DIR/const_param_ty_bad_empty_array.rs:10:13
2+
--> $DIR/const_param_ty_bad_empty_array.rs:10:14
33
|
44
LL | check::<[NotParam; 0]>();
5-
| ^^^^^^^^^^^^^ the trait `ConstParamTy_` is not implemented for `NotParam`
5+
| ^^^^^^^^ the trait `ConstParamTy_` is not implemented for `NotParam`
66
|
77
= note: required for `[NotParam; 0]` to implement `ConstParamTy_`
88
note: required by a bound in `check`

tests/ui/const-generics/adt_const_params/const_param_ty_generic_bounds_do_not_hold.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: `NotParam` can't be used as a const parameter type
2-
--> $DIR/const_param_ty_generic_bounds_do_not_hold.rs:10:13
2+
--> $DIR/const_param_ty_generic_bounds_do_not_hold.rs:10:14
33
|
44
LL | check::<&NotParam>();
5-
| ^^^^^^^^^ the trait `UnsizedConstParamTy` is not implemented for `NotParam`
5+
| ^^^^^^^^ the trait `UnsizedConstParamTy` is not implemented for `NotParam`
66
|
77
= note: required for `&NotParam` to implement `UnsizedConstParamTy`
88
note: required by a bound in `check`
@@ -12,10 +12,10 @@ LL | fn check<T: std::marker::UnsizedConstParamTy + ?Sized>() {}
1212
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `check`
1313

1414
error[E0277]: `NotParam` can't be used as a const parameter type
15-
--> $DIR/const_param_ty_generic_bounds_do_not_hold.rs:11:13
15+
--> $DIR/const_param_ty_generic_bounds_do_not_hold.rs:11:14
1616
|
1717
LL | check::<[NotParam]>();
18-
| ^^^^^^^^^^ the trait `UnsizedConstParamTy` is not implemented for `NotParam`
18+
| ^^^^^^^^ the trait `UnsizedConstParamTy` is not implemented for `NotParam`
1919
|
2020
= note: required for `[NotParam]` to implement `UnsizedConstParamTy`
2121
note: required by a bound in `check`
@@ -25,10 +25,10 @@ LL | fn check<T: std::marker::UnsizedConstParamTy + ?Sized>() {}
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `check`
2626

2727
error[E0277]: `NotParam` can't be used as a const parameter type
28-
--> $DIR/const_param_ty_generic_bounds_do_not_hold.rs:12:13
28+
--> $DIR/const_param_ty_generic_bounds_do_not_hold.rs:12:14
2929
|
3030
LL | check::<[NotParam; 17]>();
31-
| ^^^^^^^^^^^^^^ the trait `UnsizedConstParamTy` is not implemented for `NotParam`
31+
| ^^^^^^^^ the trait `UnsizedConstParamTy` is not implemented for `NotParam`
3232
|
3333
= note: required for `[NotParam; 17]` to implement `UnsizedConstParamTy`
3434
note: required by a bound in `check`

tests/ui/const-generics/generic_const_exprs/type-alias-bounds.neg.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: the trait bound `String: Copy` is not satisfied
2-
--> $DIR/type-alias-bounds.rs:23:12
2+
--> $DIR/type-alias-bounds.rs:23:29
33
|
44
LL | let _: AliasConstUnused<String>;
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
5+
| ^^^^^^ the trait `Copy` is not implemented for `String`
66
|
77
note: required by a bound in `ct_unused_0::AliasConstUnused`
88
--> $DIR/type-alias-bounds.rs:20:30
@@ -29,10 +29,10 @@ LL | type AliasConstUnused where String: Copy = I32<{ 0; 0 }>;
2929
| ^^^^ required by this bound in `AliasConstUnused`
3030

3131
error[E0277]: the trait bound `String: Copy` is not satisfied
32-
--> $DIR/type-alias-bounds.rs:40:12
32+
--> $DIR/type-alias-bounds.rs:40:26
3333
|
3434
LL | let _: AliasFnUnused<String>;
35-
| ^^^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
35+
| ^^^^^^ the trait `Copy` is not implemented for `String`
3636
|
3737
note: required by a bound in `AliasFnUnused`
3838
--> $DIR/type-alias-bounds.rs:37:27
@@ -41,10 +41,10 @@ LL | type AliasFnUnused<T: Copy> = (T, I32<{ code() }>);
4141
| ^^^^ required by this bound in `AliasFnUnused`
4242

4343
error[E0277]: the trait bound `String: Copy` is not satisfied
44-
--> $DIR/type-alias-bounds.rs:58:12
44+
--> $DIR/type-alias-bounds.rs:58:32
4545
|
4646
LL | let _: AliasAssocConstUsed<String>;
47-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
47+
| ^^^^^^ the trait `Copy` is not implemented for `String`
4848
|
4949
note: required by a bound in `AliasAssocConstUsed`
5050
--> $DIR/type-alias-bounds.rs:56:41
@@ -53,10 +53,10 @@ LL | type AliasAssocConstUsed<T: Trait + Copy> = I32<{ T::DATA }>;
5353
| ^^^^ required by this bound in `AliasAssocConstUsed`
5454

5555
error[E0277]: the trait bound `String: Copy` is not satisfied
56-
--> $DIR/type-alias-bounds.rs:66:12
56+
--> $DIR/type-alias-bounds.rs:66:24
5757
|
5858
LL | let _: AliasFnUsed<String>;
59-
| ^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
59+
| ^^^^^^ the trait `Copy` is not implemented for `String`
6060
|
6161
note: required by a bound in `AliasFnUsed`
6262
--> $DIR/type-alias-bounds.rs:63:33

0 commit comments

Comments
 (0)