Skip to content

Commit 7945dcd

Browse files
authored
Rollup merge of #67011 - Aaron1011:fix/expected-found-span, r=Dylan-DPC
Include a span in more `expected...found` notes In most places, we use a span when emitting `expected...found` errors. However, there were a couple of places where we didn't use any span, resulting in hard-to-interpret error messages. This commit attaches the relevant span to these notes, and additionally switches over to using `note_expected_found` instead of manually formatting the message
2 parents 8e6cf86 + 168e35d commit 7945dcd

33 files changed

+325
-134
lines changed

src/librustc/infer/error_reporting/mod.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -1809,12 +1809,17 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
18091809
sub_region,
18101810
"...",
18111811
);
1812-
err.note(&format!(
1813-
"...so that the {}:\nexpected {}\n found {}",
1814-
sup_trace.cause.as_requirement_str(),
1815-
sup_expected.content(),
1816-
sup_found.content()
1812+
err.span_note(sup_trace.cause.span, &format!(
1813+
"...so that the {}",
1814+
sup_trace.cause.as_requirement_str()
18171815
));
1816+
1817+
err.note_expected_found(
1818+
&"",
1819+
sup_expected,
1820+
&"",
1821+
sup_found
1822+
);
18181823
err.emit();
18191824
return;
18201825
}

src/librustc/infer/error_reporting/note.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,20 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
1313
match *origin {
1414
infer::Subtype(ref trace) => {
1515
if let Some((expected, found)) = self.values_str(&trace.values) {
16-
let expected = expected.content();
17-
let found = found.content();
18-
err.note(&format!("...so that the {}:\nexpected {}\n found {}",
19-
trace.cause.as_requirement_str(),
20-
expected,
21-
found));
16+
err.span_note(
17+
trace.cause.span,
18+
&format!(
19+
"...so that the {}",
20+
trace.cause.as_requirement_str()
21+
)
22+
);
23+
24+
err.note_expected_found(
25+
&"",
26+
expected,
27+
&"",
28+
found
29+
);
2230
} else {
2331
// FIXME: this really should be handled at some earlier stage. Our
2432
// handling of region checking when type errors are present is

src/test/ui/associated-types/cache/project-fn-ret-invariant.transmute.stderr

+14-6
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,21 @@ note: first, the lifetime cannot outlive the lifetime `'a` as defined on the fun
99
|
1010
LL | fn baz<'a,'b>(x: Type<'a>) -> Type<'static> {
1111
| ^^
12-
= note: ...so that the expression is assignable:
13-
expected Type<'_>
14-
found Type<'a>
12+
note: ...so that the expression is assignable
13+
--> $DIR/project-fn-ret-invariant.rs:48:13
14+
|
15+
LL | bar(foo, x)
16+
| ^
17+
= note: expected `Type<'_>`
18+
found `Type<'a>`
1519
= note: but, the lifetime must be valid for the static lifetime...
16-
= note: ...so that the expression is assignable:
17-
expected Type<'static>
18-
found Type<'_>
20+
note: ...so that the expression is assignable
21+
--> $DIR/project-fn-ret-invariant.rs:48:4
22+
|
23+
LL | bar(foo, x)
24+
| ^^^^^^^^^^^
25+
= note: expected `Type<'static>`
26+
found `Type<'_>`
1927

2028
error: aborting due to previous error
2129

src/test/ui/c-variadic/variadic-ffi-4.stderr

+7-3
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,13 @@ note: first, the lifetime cannot outlive the anonymous lifetime #2 defined on th
4949
|
5050
LL | let _ = ap.with_copy(|ap| { ap });
5151
| ^^^^^^^^^^^
52-
= note: ...so that the expression is assignable:
53-
expected core::ffi::VaList<'_, '_>
54-
found core::ffi::VaList<'_, '_>
52+
note: ...so that the expression is assignable
53+
--> $DIR/variadic-ffi-4.rs:16:33
54+
|
55+
LL | let _ = ap.with_copy(|ap| { ap });
56+
| ^^
57+
= note: expected `core::ffi::VaList<'_, '_>`
58+
found `core::ffi::VaList<'_, '_>`
5559
note: but, the lifetime must be valid for the method call at 16:13...
5660
--> $DIR/variadic-ffi-4.rs:16:13
5761
|

src/test/ui/impl-header-lifetime-elision/dyn-trait.stderr

+14-6
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,21 @@ note: first, the lifetime cannot outlive the lifetime `'a` as defined on the fun
99
|
1010
LL | fn with_dyn_debug_static<'a>(x: Box<dyn Debug + 'a>) {
1111
| ^^
12-
= note: ...so that the expression is assignable:
13-
expected std::boxed::Box<dyn std::fmt::Debug>
14-
found std::boxed::Box<(dyn std::fmt::Debug + 'a)>
12+
note: ...so that the expression is assignable
13+
--> $DIR/dyn-trait.rs:20:16
14+
|
15+
LL | static_val(x);
16+
| ^
17+
= note: expected `std::boxed::Box<dyn std::fmt::Debug>`
18+
found `std::boxed::Box<(dyn std::fmt::Debug + 'a)>`
1519
= note: but, the lifetime must be valid for the static lifetime...
16-
= note: ...so that the types are compatible:
17-
expected StaticTrait
18-
found StaticTrait
20+
note: ...so that the types are compatible
21+
--> $DIR/dyn-trait.rs:20:5
22+
|
23+
LL | static_val(x);
24+
| ^^^^^^^^^^
25+
= note: expected `StaticTrait`
26+
found `StaticTrait`
1927

2028
error: aborting due to previous error
2129

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ note: but, the lifetime must be valid for the lifetime `'a` as defined on the tr
2121
|
2222
LL | trait T<'a> {
2323
| ^^
24-
= note: ...so that the types are compatible:
25-
expected &'a Self
26-
found &Self
24+
note: ...so that the types are compatible
25+
--> $DIR/issue-16683.rs:4:14
26+
|
27+
LL | self.a();
28+
| ^
29+
= note: expected `&'a Self`
30+
found `&Self`
2731

2832
error: aborting due to previous error
2933

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ note: but, the lifetime must be valid for the lifetime `'a` as defined on the tr
2222
|
2323
LL | trait Foo<'a> {
2424
| ^^
25-
= note: ...so that the types are compatible:
26-
expected &'a Self
27-
found &Self
25+
note: ...so that the types are compatible
26+
--> $DIR/issue-17758.rs:7:14
27+
|
28+
LL | self.foo();
29+
| ^^^
30+
= note: expected `&'a Self`
31+
found `&Self`
2832

2933
error: aborting due to previous error
3034

src/test/ui/issues/issue-20831-debruijn.stderr

+13-3
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,19 @@ note: ...but the lifetime must also be valid for the lifetime `'a` as defined on
8888
|
8989
LL | impl<'a> Publisher<'a> for MyStruct<'a> {
9090
| ^^
91-
= note: ...so that the types are compatible:
92-
expected Publisher<'_>
93-
found Publisher<'_>
91+
note: ...so that the types are compatible
92+
--> $DIR/issue-20831-debruijn.rs:28:5
93+
|
94+
LL | / fn subscribe(&mut self, t : Box<dyn Subscriber<Input=<Self as Publisher>::Output> + 'a>) {
95+
LL | | // Not obvious, but there is an implicit lifetime here -------^
96+
LL | |
97+
LL | |
98+
... |
99+
LL | | self.sub = t;
100+
LL | | }
101+
| |_____^
102+
= note: expected `Publisher<'_>`
103+
found `Publisher<'_>`
94104

95105
error: aborting due to 3 previous errors
96106

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ note: first, the lifetime cannot outlive the lifetime `'a` as defined on the fun
99
|
1010
LL | fn transmute_lifetime<'a, 'b, T>(t: &'a (T,)) -> &'b T {
1111
| ^^
12-
= note: ...so that the types are compatible:
13-
expected (&&(T,),)
14-
found (&&'a (T,),)
12+
note: ...so that the types are compatible
13+
--> $DIR/issue-52213.rs:2:11
14+
|
15+
LL | match (&t,) {
16+
| ^^^^^
17+
= note: expected `(&&(T,),)`
18+
found `(&&'a (T,),)`
1519
note: but, the lifetime must be valid for the lifetime `'b` as defined on the function body at 1:27...
1620
--> $DIR/issue-52213.rs:1:27
1721
|

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

+14-6
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ note: ...so that the type `std::iter::Map<<Self as Graph<'a>>::EdgesIter, [closu
1515
LL | Box::new(self.out_edges(u).map(|e| e.target()))
1616
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1717
= note: but, the lifetime must be valid for the static lifetime...
18-
= note: ...so that the expression is assignable:
19-
expected std::boxed::Box<(dyn std::iter::Iterator<Item = <Self as Graph<'a>>::Node> + 'static)>
20-
found std::boxed::Box<dyn std::iter::Iterator<Item = <Self as Graph<'a>>::Node>>
18+
note: ...so that the expression is assignable
19+
--> $DIR/issue-55796.rs:16:9
20+
|
21+
LL | Box::new(self.out_edges(u).map(|e| e.target()))
22+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
23+
= note: expected `std::boxed::Box<(dyn std::iter::Iterator<Item = <Self as Graph<'a>>::Node> + 'static)>`
24+
found `std::boxed::Box<dyn std::iter::Iterator<Item = <Self as Graph<'a>>::Node>>`
2125

2226
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
2327
--> $DIR/issue-55796.rs:21:9
@@ -36,9 +40,13 @@ note: ...so that the type `std::iter::Map<<Self as Graph<'a>>::EdgesIter, [closu
3640
LL | Box::new(self.in_edges(u).map(|e| e.target()))
3741
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3842
= note: but, the lifetime must be valid for the static lifetime...
39-
= note: ...so that the expression is assignable:
40-
expected std::boxed::Box<(dyn std::iter::Iterator<Item = <Self as Graph<'a>>::Node> + 'static)>
41-
found std::boxed::Box<dyn std::iter::Iterator<Item = <Self as Graph<'a>>::Node>>
43+
note: ...so that the expression is assignable
44+
--> $DIR/issue-55796.rs:21:9
45+
|
46+
LL | Box::new(self.in_edges(u).map(|e| e.target()))
47+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
48+
= note: expected `std::boxed::Box<(dyn std::iter::Iterator<Item = <Self as Graph<'a>>::Node> + 'static)>`
49+
found `std::boxed::Box<dyn std::iter::Iterator<Item = <Self as Graph<'a>>::Node>>`
4250

4351
error: aborting due to 2 previous errors
4452

src/test/ui/nll/issue-55394.stderr

+7-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ note: but, the lifetime must be valid for the lifetime `'_` as defined on the im
2121
|
2222
LL | impl Foo<'_> {
2323
| ^^
24-
= note: ...so that the expression is assignable:
25-
expected Foo<'_>
26-
found Foo<'_>
24+
note: ...so that the expression is assignable
25+
--> $DIR/issue-55394.rs:9:9
26+
|
27+
LL | Foo { bar }
28+
| ^^^^^^^^^^^
29+
= note: expected `Foo<'_>`
30+
found `Foo<'_>`
2731

2832
error: aborting due to previous error
2933

src/test/ui/nll/normalization-bounds-error.stderr

+7-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ note: ...but the lifetime must also be valid for the lifetime `'a` as defined on
1414
|
1515
LL | fn visit_seq<'d, 'a: 'd>() -> <&'a () as Visitor<'d>>::Value {}
1616
| ^^
17-
= note: ...so that the types are compatible:
18-
expected Visitor<'d>
19-
found Visitor<'_>
17+
note: ...so that the types are compatible
18+
--> $DIR/normalization-bounds-error.rs:12:1
19+
|
20+
LL | fn visit_seq<'d, 'a: 'd>() -> <&'a () as Visitor<'d>>::Value {}
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
22+
= note: expected `Visitor<'d>`
23+
found `Visitor<'_>`
2024

2125
error: aborting due to previous error
2226

src/test/ui/nll/type-alias-free-regions.stderr

+28-12
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,25 @@ LL | / fn from_box(b: Box<B>) -> Self {
1111
LL | | C { f: b }
1212
LL | | }
1313
| |_____^
14-
= note: ...so that the expression is assignable:
15-
expected std::boxed::Box<std::boxed::Box<&isize>>
16-
found std::boxed::Box<std::boxed::Box<&isize>>
14+
note: ...so that the expression is assignable
15+
--> $DIR/type-alias-free-regions.rs:17:16
16+
|
17+
LL | C { f: b }
18+
| ^
19+
= note: expected `std::boxed::Box<std::boxed::Box<&isize>>`
20+
found `std::boxed::Box<std::boxed::Box<&isize>>`
1721
note: but, the lifetime must be valid for the lifetime `'a` as defined on the impl at 15:6...
1822
--> $DIR/type-alias-free-regions.rs:15:6
1923
|
2024
LL | impl<'a> FromBox<'a> for C<'a> {
2125
| ^^
22-
= note: ...so that the expression is assignable:
23-
expected C<'a>
24-
found C<'_>
26+
note: ...so that the expression is assignable
27+
--> $DIR/type-alias-free-regions.rs:17:9
28+
|
29+
LL | C { f: b }
30+
| ^^^^^^^^^^
31+
= note: expected `C<'a>`
32+
found `C<'_>`
2533

2634
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
2735
--> $DIR/type-alias-free-regions.rs:27:16
@@ -36,17 +44,25 @@ LL | / fn from_tuple(b: (B,)) -> Self {
3644
LL | | C { f: Box::new(b.0) }
3745
LL | | }
3846
| |_____^
39-
= note: ...so that the expression is assignable:
40-
expected std::boxed::Box<&isize>
41-
found std::boxed::Box<&isize>
47+
note: ...so that the expression is assignable
48+
--> $DIR/type-alias-free-regions.rs:27:25
49+
|
50+
LL | C { f: Box::new(b.0) }
51+
| ^^^
52+
= note: expected `std::boxed::Box<&isize>`
53+
found `std::boxed::Box<&isize>`
4254
note: but, the lifetime must be valid for the lifetime `'a` as defined on the impl at 25:6...
4355
--> $DIR/type-alias-free-regions.rs:25:6
4456
|
4557
LL | impl<'a> FromTuple<'a> for C<'a> {
4658
| ^^
47-
= note: ...so that the expression is assignable:
48-
expected C<'a>
49-
found C<'_>
59+
note: ...so that the expression is assignable
60+
--> $DIR/type-alias-free-regions.rs:27:9
61+
|
62+
LL | C { f: Box::new(b.0) }
63+
| ^^^^^^^^^^^^^^^^^^^^^^
64+
= note: expected `C<'a>`
65+
found `C<'_>`
5066

5167
error: aborting due to 2 previous errors
5268

src/test/ui/nll/user-annotations/constant-in-expr-inherent-1.stderr

+7-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ note: first, the lifetime cannot outlive the lifetime `'a` as defined on the fun
99
|
1010
LL | fn foo<'a>(_: &'a u32) -> &'static u32 {
1111
| ^^
12-
= note: ...so that the types are compatible:
13-
expected Foo<'_>
14-
found Foo<'a>
12+
note: ...so that the types are compatible
13+
--> $DIR/constant-in-expr-inherent-1.rs:8:5
14+
|
15+
LL | <Foo<'a>>::C
16+
| ^^^^^^^^^^^^
17+
= note: expected `Foo<'_>`
18+
found `Foo<'a>`
1519
= note: but, the lifetime must be valid for the static lifetime...
1620
note: ...so that reference does not outlive borrowed content
1721
--> $DIR/constant-in-expr-inherent-1.rs:8:5

src/test/ui/nll/user-annotations/constant-in-expr-trait-item-3.stderr

+7-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ note: first, the lifetime cannot outlive the lifetime `'a` as defined on the fun
99
|
1010
LL | fn foo<'a, T: Foo<'a>>() -> &'static u32 {
1111
| ^^
12-
= note: ...so that the types are compatible:
13-
expected Foo<'_>
14-
found Foo<'a>
12+
note: ...so that the types are compatible
13+
--> $DIR/constant-in-expr-trait-item-3.rs:10:5
14+
|
15+
LL | T::C
16+
| ^^^^
17+
= note: expected `Foo<'_>`
18+
found `Foo<'a>`
1519
= note: but, the lifetime must be valid for the static lifetime...
1620
note: ...so that reference does not outlive borrowed content
1721
--> $DIR/constant-in-expr-trait-item-3.rs:10:5

src/test/ui/object-lifetime/object-lifetime-default-elision.stderr

+14-6
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ note: but, the lifetime must be valid for the lifetime `'b` as defined on the fu
1919
|
2020
LL | fn load3<'a,'b>(ss: &'a dyn SomeTrait) -> &'b dyn SomeTrait {
2121
| ^^
22-
= note: ...so that the expression is assignable:
23-
expected &'b (dyn SomeTrait + 'b)
24-
found &dyn SomeTrait
22+
note: ...so that the expression is assignable
23+
--> $DIR/object-lifetime-default-elision.rs:71:5
24+
|
25+
LL | ss
26+
| ^^
27+
= note: expected `&'b (dyn SomeTrait + 'b)`
28+
found `&dyn SomeTrait`
2529

2630
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
2731
--> $DIR/object-lifetime-default-elision.rs:71:5
@@ -44,9 +48,13 @@ note: but, the lifetime must be valid for the lifetime `'b` as defined on the fu
4448
|
4549
LL | fn load3<'a,'b>(ss: &'a dyn SomeTrait) -> &'b dyn SomeTrait {
4650
| ^^
47-
= note: ...so that the expression is assignable:
48-
expected &'b (dyn SomeTrait + 'b)
49-
found &dyn SomeTrait
51+
note: ...so that the expression is assignable
52+
--> $DIR/object-lifetime-default-elision.rs:71:5
53+
|
54+
LL | ss
55+
| ^^
56+
= note: expected `&'b (dyn SomeTrait + 'b)`
57+
found `&dyn SomeTrait`
5058

5159
error: aborting due to 2 previous errors
5260

0 commit comments

Comments
 (0)