Skip to content

Commit 9b36030

Browse files
committed
On E0283, point at method with the requirements
On required type annotation diagnostic error, point at method with the requirements if the span is available.
1 parent bb345a0 commit 9b36030

23 files changed

+260
-48
lines changed

src/librustc/traits/error_reporting.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,13 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
12411241
}
12421242
ObligationCauseCode::ItemObligation(item_def_id) => {
12431243
let item_name = tcx.item_path_str(item_def_id);
1244-
err.note(&format!("required by `{}`", item_name));
1244+
let msg = format!("required by `{}`", item_name);
1245+
if let Some(sp) = tcx.hir.span_if_local(item_def_id) {
1246+
let sp = tcx.sess.codemap().def_span(sp);
1247+
err.span_note(sp, &msg);
1248+
} else {
1249+
err.note(&msg);
1250+
}
12451251
}
12461252
ObligationCauseCode::ObjectCastObligation(object_ty) => {
12471253
err.note(&format!("required for the cast to the object type `{}`",

src/test/ui/anonymous-higher-ranked-lifetime.stderr

+55-11
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ error[E0631]: type mismatch in closure arguments
66
| |
77
| expected signature of `for<'r, 's> fn(&'r (), &'s ()) -> _`
88
|
9-
= note: required by `f1`
9+
note: required by `f1`
10+
--> $DIR/anonymous-higher-ranked-lifetime.rs:26:1
11+
|
12+
26 | fn f1<F>(_: F) where F: Fn(&(), &()) {}
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1014

1115
error[E0631]: type mismatch in closure arguments
1216
--> $DIR/anonymous-higher-ranked-lifetime.rs:13:5
@@ -16,7 +20,11 @@ error[E0631]: type mismatch in closure arguments
1620
| |
1721
| expected signature of `for<'a, 'r> fn(&'a (), &'r ()) -> _`
1822
|
19-
= note: required by `f2`
23+
note: required by `f2`
24+
--> $DIR/anonymous-higher-ranked-lifetime.rs:27:1
25+
|
26+
27 | fn f2<F>(_: F) where F: for<'a> Fn(&'a (), &()) {}
27+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2028

2129
error[E0631]: type mismatch in closure arguments
2230
--> $DIR/anonymous-higher-ranked-lifetime.rs:14:5
@@ -26,7 +34,11 @@ error[E0631]: type mismatch in closure arguments
2634
| |
2735
| expected signature of `for<'r> fn(&(), &'r ()) -> _`
2836
|
29-
= note: required by `f3`
37+
note: required by `f3`
38+
--> $DIR/anonymous-higher-ranked-lifetime.rs:28:1
39+
|
40+
28 | fn f3<'a, F>(_: F) where F: Fn(&'a (), &()) {}
41+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3042

3143
error[E0631]: type mismatch in closure arguments
3244
--> $DIR/anonymous-higher-ranked-lifetime.rs:15:5
@@ -36,7 +48,11 @@ error[E0631]: type mismatch in closure arguments
3648
| |
3749
| expected signature of `for<'s, 'r> fn(&'s (), &'r ()) -> _`
3850
|
39-
= note: required by `f4`
51+
note: required by `f4`
52+
--> $DIR/anonymous-higher-ranked-lifetime.rs:29:1
53+
|
54+
29 | fn f4<F>(_: F) where F: for<'r> Fn(&(), &'r ()) {}
55+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4056

4157
error[E0631]: type mismatch in closure arguments
4258
--> $DIR/anonymous-higher-ranked-lifetime.rs:16:5
@@ -46,7 +62,11 @@ error[E0631]: type mismatch in closure arguments
4662
| |
4763
| expected signature of `for<'r> fn(&'r (), &'r ()) -> _`
4864
|
49-
= note: required by `f5`
65+
note: required by `f5`
66+
--> $DIR/anonymous-higher-ranked-lifetime.rs:30:1
67+
|
68+
30 | fn f5<F>(_: F) where F: for<'r> Fn(&'r (), &'r ()) {}
69+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5070

5171
error[E0631]: type mismatch in closure arguments
5272
--> $DIR/anonymous-higher-ranked-lifetime.rs:17:5
@@ -56,7 +76,11 @@ error[E0631]: type mismatch in closure arguments
5676
| |
5777
| expected signature of `for<'r> fn(&'r (), std::boxed::Box<for<'s> std::ops::Fn(&'s ()) + 'static>) -> _`
5878
|
59-
= note: required by `g1`
79+
note: required by `g1`
80+
--> $DIR/anonymous-higher-ranked-lifetime.rs:33:1
81+
|
82+
33 | fn g1<F>(_: F) where F: Fn(&(), Box<Fn(&())>) {}
83+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6084

6185
error[E0631]: type mismatch in closure arguments
6286
--> $DIR/anonymous-higher-ranked-lifetime.rs:18:5
@@ -66,7 +90,11 @@ error[E0631]: type mismatch in closure arguments
6690
| |
6791
| expected signature of `for<'r> fn(&'r (), for<'s> fn(&'s ())) -> _`
6892
|
69-
= note: required by `g2`
93+
note: required by `g2`
94+
--> $DIR/anonymous-higher-ranked-lifetime.rs:34:1
95+
|
96+
34 | fn g2<F>(_: F) where F: Fn(&(), fn(&())) {}
97+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7098

7199
error[E0631]: type mismatch in closure arguments
72100
--> $DIR/anonymous-higher-ranked-lifetime.rs:19:5
@@ -76,7 +104,11 @@ error[E0631]: type mismatch in closure arguments
76104
| |
77105
| expected signature of `for<'s> fn(&'s (), std::boxed::Box<for<'r> std::ops::Fn(&'r ()) + 'static>) -> _`
78106
|
79-
= note: required by `g3`
107+
note: required by `g3`
108+
--> $DIR/anonymous-higher-ranked-lifetime.rs:35:1
109+
|
110+
35 | fn g3<F>(_: F) where F: for<'s> Fn(&'s (), Box<Fn(&())>) {}
111+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
80112

81113
error[E0631]: type mismatch in closure arguments
82114
--> $DIR/anonymous-higher-ranked-lifetime.rs:20:5
@@ -86,7 +118,11 @@ error[E0631]: type mismatch in closure arguments
86118
| |
87119
| expected signature of `for<'s> fn(&'s (), for<'r> fn(&'r ())) -> _`
88120
|
89-
= note: required by `g4`
121+
note: required by `g4`
122+
--> $DIR/anonymous-higher-ranked-lifetime.rs:36:1
123+
|
124+
36 | fn g4<F>(_: F) where F: Fn(&(), for<'r> fn(&'r ())) {}
125+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
90126

91127
error[E0631]: type mismatch in closure arguments
92128
--> $DIR/anonymous-higher-ranked-lifetime.rs:21:5
@@ -96,7 +132,11 @@ error[E0631]: type mismatch in closure arguments
96132
| |
97133
| expected signature of `for<'r, 's> fn(&'r (), std::boxed::Box<for<'t0> std::ops::Fn(&'t0 ()) + 'static>, &'s (), for<'t0, 't1> fn(&'t0 (), &'t1 ())) -> _`
98134
|
99-
= note: required by `h1`
135+
note: required by `h1`
136+
--> $DIR/anonymous-higher-ranked-lifetime.rs:39:1
137+
|
138+
39 | fn h1<F>(_: F) where F: Fn(&(), Box<Fn(&())>, &(), fn(&(), &())) {}
139+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
100140

101141
error[E0631]: type mismatch in closure arguments
102142
--> $DIR/anonymous-higher-ranked-lifetime.rs:22:5
@@ -106,7 +146,11 @@ error[E0631]: type mismatch in closure arguments
106146
| |
107147
| expected signature of `for<'r, 't0> fn(&'r (), std::boxed::Box<for<'s> std::ops::Fn(&'s ()) + 'static>, &'t0 (), for<'s, 't1> fn(&'s (), &'t1 ())) -> _`
108148
|
109-
= note: required by `h2`
149+
note: required by `h2`
150+
--> $DIR/anonymous-higher-ranked-lifetime.rs:40:1
151+
|
152+
40 | fn h2<F>(_: F) where F: for<'t0> Fn(&(), Box<Fn(&())>, &'t0 (), fn(&(), &())) {}
153+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
110154

111155
error: aborting due to 11 previous errors
112156

src/test/ui/did_you_mean/issue-39802-show-5-trait-impls.stderr

+15-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ error[E0277]: the trait bound `i8: Foo<i32>` is not satisfied
1010
<i8 as Foo<u32>>
1111
<i8 as Foo<u64>>
1212
<i8 as Foo<bool>>
13-
= note: required by `Foo::bar`
13+
note: required by `Foo::bar`
14+
--> $DIR/issue-39802-show-5-trait-impls.rs:12:5
15+
|
16+
12 | fn bar(&self){}
17+
| ^^^^^^^^^^^^^
1418

1519
error[E0277]: the trait bound `u8: Foo<i32>` is not satisfied
1620
--> $DIR/issue-39802-show-5-trait-impls.rs:35:5
@@ -23,7 +27,11 @@ error[E0277]: the trait bound `u8: Foo<i32>` is not satisfied
2327
<u8 as Foo<u32>>
2428
<u8 as Foo<u64>>
2529
<u8 as Foo<bool>>
26-
= note: required by `Foo::bar`
30+
note: required by `Foo::bar`
31+
--> $DIR/issue-39802-show-5-trait-impls.rs:12:5
32+
|
33+
12 | fn bar(&self){}
34+
| ^^^^^^^^^^^^^
2735

2836
error[E0277]: the trait bound `bool: Foo<i32>` is not satisfied
2937
--> $DIR/issue-39802-show-5-trait-impls.rs:36:5
@@ -37,7 +45,11 @@ error[E0277]: the trait bound `bool: Foo<i32>` is not satisfied
3745
<bool as Foo<u32>>
3846
<bool as Foo<u64>>
3947
and 2 others
40-
= note: required by `Foo::bar`
48+
note: required by `Foo::bar`
49+
--> $DIR/issue-39802-show-5-trait-impls.rs:12:5
50+
|
51+
12 | fn bar(&self){}
52+
| ^^^^^^^^^^^^^
4153

4254
error: aborting due to 3 previous errors
4355

src/test/ui/did_you_mean/recursion_limit.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ error[E0275]: overflow evaluating the requirement `K: std::marker::Send`
1515
= note: required because it appears within the type `C`
1616
= note: required because it appears within the type `B`
1717
= note: required because it appears within the type `A`
18-
= note: required by `is_send`
18+
note: required by `is_send`
19+
--> $DIR/recursion_limit.rs:41:1
20+
|
21+
41 | fn is_send<T:Send>() { }
22+
| ^^^^^^^^^^^^^^^^^^^^
1923

2024
error: aborting due to previous error
2125

src/test/ui/fmt/send-sync.stderr

+10-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ error[E0277]: the trait bound `*mut std::ops::Fn() + 'static: std::marker::Sync`
1212
= note: required because it appears within the type `[std::fmt::ArgumentV1<'_>]`
1313
= note: required because of the requirements on the impl of `std::marker::Send` for `&[std::fmt::ArgumentV1<'_>]`
1414
= note: required because it appears within the type `std::fmt::Arguments<'_>`
15-
= note: required by `send`
15+
note: required by `send`
16+
--> $DIR/send-sync.rs:11:1
17+
|
18+
11 | fn send<T: Send>(_: T) {}
19+
| ^^^^^^^^^^^^^^^^^^^^^^
1620

1721
error[E0277]: the trait bound `*mut std::ops::Fn() + 'static: std::marker::Sync` is not satisfied in `std::fmt::Arguments<'_>`
1822
--> $DIR/send-sync.rs:19:5
@@ -28,7 +32,11 @@ error[E0277]: the trait bound `*mut std::ops::Fn() + 'static: std::marker::Sync`
2832
= note: required because it appears within the type `[std::fmt::ArgumentV1<'_>]`
2933
= note: required because it appears within the type `&[std::fmt::ArgumentV1<'_>]`
3034
= note: required because it appears within the type `std::fmt::Arguments<'_>`
31-
= note: required by `sync`
35+
note: required by `sync`
36+
--> $DIR/send-sync.rs:12:1
37+
|
38+
12 | fn sync<T: Sync>(_: T) {}
39+
| ^^^^^^^^^^^^^^^^^^^^^^
3240

3341
error: aborting due to 2 previous errors
3442

src/test/ui/generator/not-send-sync.stderr

+10-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ error[E0277]: the trait bound `std::cell::Cell<i32>: std::marker::Sync` is not s
77
= help: the trait `std::marker::Sync` is not implemented for `std::cell::Cell<i32>`
88
= note: required because of the requirements on the impl of `std::marker::Send` for `&std::cell::Cell<i32>`
99
= note: required because it appears within the type `[generator@$DIR/not-send-sync.rs:26:17: 30:6 a:&std::cell::Cell<i32> _]`
10-
= note: required by `main::assert_send`
10+
note: required by `main::assert_send`
11+
--> $DIR/not-send-sync.rs:17:5
12+
|
13+
17 | fn assert_send<T: Send>(_: T) {}
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1115

1216
error[E0277]: the trait bound `std::cell::Cell<i32>: std::marker::Sync` is not satisfied in `[generator@$DIR/not-send-sync.rs:19:17: 23:6 (std::cell::Cell<i32>, ())]`
1317
--> $DIR/not-send-sync.rs:19:5
@@ -18,7 +22,11 @@ error[E0277]: the trait bound `std::cell::Cell<i32>: std::marker::Sync` is not s
1822
= help: within `[generator@$DIR/not-send-sync.rs:19:17: 23:6 (std::cell::Cell<i32>, ())]`, the trait `std::marker::Sync` is not implemented for `std::cell::Cell<i32>`
1923
= note: required because it appears within the type `(std::cell::Cell<i32>, ())`
2024
= note: required because it appears within the type `[generator@$DIR/not-send-sync.rs:19:17: 23:6 (std::cell::Cell<i32>, ())]`
21-
= note: required by `main::assert_sync`
25+
note: required by `main::assert_sync`
26+
--> $DIR/not-send-sync.rs:16:5
27+
|
28+
16 | fn assert_sync<T: Sync>(_: T) {}
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2230

2331
error: aborting due to 2 previous errors
2432

src/test/ui/impl-trait/auto-trait-leak.stderr

+10-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ error[E0277]: the trait bound `std::rc::Rc<std::cell::Cell<i32>>: std::marker::S
77
= help: within `impl std::ops::Fn<(i32,)>`, the trait `std::marker::Send` is not implemented for `std::rc::Rc<std::cell::Cell<i32>>`
88
= note: required because it appears within the type `[closure@$DIR/auto-trait-leak.rs:21:5: 21:22 p:std::rc::Rc<std::cell::Cell<i32>>]`
99
= note: required because it appears within the type `impl std::ops::Fn<(i32,)>`
10-
= note: required by `send`
10+
note: required by `send`
11+
--> $DIR/auto-trait-leak.rs:24:1
12+
|
13+
24 | fn send<T: Send>(_: T) {}
14+
| ^^^^^^^^^^^^^^^^^^^^^^
1115

1216
error[E0277]: the trait bound `std::rc::Rc<std::cell::Cell<i32>>: std::marker::Send` is not satisfied in `impl std::ops::Fn<(i32,)>`
1317
--> $DIR/auto-trait-leak.rs:30:5
@@ -18,7 +22,11 @@ error[E0277]: the trait bound `std::rc::Rc<std::cell::Cell<i32>>: std::marker::S
1822
= help: within `impl std::ops::Fn<(i32,)>`, the trait `std::marker::Send` is not implemented for `std::rc::Rc<std::cell::Cell<i32>>`
1923
= note: required because it appears within the type `[closure@$DIR/auto-trait-leak.rs:38:5: 38:22 p:std::rc::Rc<std::cell::Cell<i32>>]`
2024
= note: required because it appears within the type `impl std::ops::Fn<(i32,)>`
21-
= note: required by `send`
25+
note: required by `send`
26+
--> $DIR/auto-trait-leak.rs:24:1
27+
|
28+
24 | fn send<T: Send>(_: T) {}
29+
| ^^^^^^^^^^^^^^^^^^^^^^
2230

2331
error[E0391]: unsupported cyclic reference between types/traits detected
2432
--> $DIR/auto-trait-leak.rs:44:1

src/test/ui/issue-24424.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ error[E0283]: type annotations required: cannot resolve `T0: Trait0<'l0>`
44
14 | impl <'l0, 'l1, T0> Trait1<'l0, T0> for bool where T0 : Trait0<'l0>, T0 : Trait0<'l1> {}
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: required by `Trait0`
7+
note: required by `Trait0`
8+
--> $DIR/issue-24424.rs:12:1
9+
|
10+
12 | trait Trait0<'l0> {}
11+
| ^^^^^^^^^^^^^^^^^
812

913
error: aborting due to previous error
1014

src/test/ui/mismatched_types/E0631.stderr

+20-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ error[E0631]: type mismatch in closure arguments
66
| |
77
| expected signature of `fn(usize) -> _`
88
|
9-
= note: required by `foo`
9+
note: required by `foo`
10+
--> $DIR/E0631.rs:13:1
11+
|
12+
13 | fn foo<F: Fn(usize)>(_: F) {}
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
1014

1115
error[E0631]: type mismatch in closure arguments
1216
--> $DIR/E0631.rs:18:5
@@ -16,7 +20,11 @@ error[E0631]: type mismatch in closure arguments
1620
| |
1721
| expected signature of `fn(usize) -> _`
1822
|
19-
= note: required by `bar`
23+
note: required by `bar`
24+
--> $DIR/E0631.rs:14:1
25+
|
26+
14 | fn bar<F: Fn<usize>>(_: F) {}
27+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
2028

2129
error[E0631]: type mismatch in function arguments
2230
--> $DIR/E0631.rs:19:5
@@ -27,7 +35,11 @@ error[E0631]: type mismatch in function arguments
2735
19 | foo(f); //~ ERROR type mismatch
2836
| ^^^ expected signature of `fn(usize) -> _`
2937
|
30-
= note: required by `foo`
38+
note: required by `foo`
39+
--> $DIR/E0631.rs:13:1
40+
|
41+
13 | fn foo<F: Fn(usize)>(_: F) {}
42+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
3143

3244
error[E0631]: type mismatch in function arguments
3345
--> $DIR/E0631.rs:20:5
@@ -38,7 +50,11 @@ error[E0631]: type mismatch in function arguments
3850
20 | bar(f); //~ ERROR type mismatch
3951
| ^^^ expected signature of `fn(usize) -> _`
4052
|
41-
= note: required by `bar`
53+
note: required by `bar`
54+
--> $DIR/E0631.rs:14:1
55+
|
56+
14 | fn bar<F: Fn<usize>>(_: F) {}
57+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
4258

4359
error: aborting due to 4 previous errors
4460

src/test/ui/mismatched_types/closure-arg-count.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ error[E0593]: closure is expected to take 1 argument, but it takes 0 arguments
3030
| |
3131
| expected closure that takes 1 argument
3232
|
33-
= note: required by `f`
33+
note: required by `f`
34+
--> $DIR/closure-arg-count.rs:13:1
35+
|
36+
13 | fn f<F: Fn<usize>>(_: F) {}
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^
3438

3539
error[E0593]: closure is expected to take a single tuple as argument, but it takes 2 distinct arguments
3640
--> $DIR/closure-arg-count.rs:24:53

src/test/ui/mismatched_types/closure-arg-type-mismatch.stderr

+10-2
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,23 @@ error[E0631]: type mismatch in function arguments
3131
| expected signature of `for<'r> fn(*mut &'r u32) -> _`
3232
| found signature of `fn(*mut &'a u32) -> _`
3333
|
34-
= note: required by `baz`
34+
note: required by `baz`
35+
--> $DIR/closure-arg-type-mismatch.rs:18:1
36+
|
37+
18 | fn baz<F: Fn(*mut &u32)>(_: F) {}
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3539

3640
error[E0271]: type mismatch resolving `for<'r> <fn(*mut &'a u32) as std::ops::FnOnce<(*mut &'r u32,)>>::Output == ()`
3741
--> $DIR/closure-arg-type-mismatch.rs:20:5
3842
|
3943
20 | baz(f); //~ ERROR type mismatch
4044
| ^^^ expected bound lifetime parameter, found concrete lifetime
4145
|
42-
= note: required by `baz`
46+
note: required by `baz`
47+
--> $DIR/closure-arg-type-mismatch.rs:18:1
48+
|
49+
18 | fn baz<F: Fn(*mut &u32)>(_: F) {}
50+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4351

4452
error: aborting due to 5 previous errors
4553

0 commit comments

Comments
 (0)