Skip to content

Commit e755889

Browse files
committed
Move overlapping span to a note
1 parent 921f35f commit e755889

File tree

7 files changed

+99
-46
lines changed

7 files changed

+99
-46
lines changed

src/librustc_infer/infer/error_reporting/nice_region_error/static_impl_trait.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,36 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
5353

5454
// Customize the spans and labels depending on their relative order so
5555
// that split sentences flow correctly.
56-
if sup_origin.span().shrink_to_hi() <= return_sp.shrink_to_lo() {
56+
if sup_origin.span().overlaps(return_sp) && sp == sup_origin.span() {
57+
// Avoid the following:
58+
//
59+
// error: cannot infer an appropriate lifetime
60+
// --> $DIR/must_outlive_least_region_or_bound.rs:18:50
61+
// |
62+
// LL | fn foo(x: &i32) -> Box<dyn Debug> { Box::new(x) }
63+
// | ---- ---------^-
64+
// | | | |
65+
// | | | ...and is captured here
66+
// | | ...is required to be `'static` by this...
67+
// | this data with the anonymous lifetime `'_`...
68+
//
69+
// and instead show:
70+
//
71+
// error: cannot infer an appropriate lifetime
72+
// --> $DIR/must_outlive_least_region_or_bound.rs:18:50
73+
// |
74+
// LL | fn foo(x: &i32) -> Box<dyn Debug> { Box::new(x) }
75+
// | ---- ^ ...is captured here with a `'static` requirement
76+
// | |
77+
// | this data with the anonymous lifetime `'_`...
78+
// |
79+
// note: ...is required to be `'static` by this
80+
// |
81+
// LL | fn elided3(x: &i32) -> Box<dyn Debug> { Box::new(x) }
82+
// | ^^^^^^^^^^^
83+
err.span_label(sup_origin.span(), "...is captured here...");
84+
err.span_note(return_sp, "...and required to be `'static` by this");
85+
} else if sup_origin.span() <= return_sp {
5786
err.span_label(sup_origin.span(), "...is captured here...");
5887
err.span_label(return_sp, "...and required to be `'static` by this");
5988
} else {

src/test/ui/impl-trait/must_outlive_least_region_or_bound.stderr

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,15 @@ error: cannot infer an appropriate lifetime
109109
--> $DIR/must_outlive_least_region_or_bound.rs:18:50
110110
|
111111
LL | fn elided3(x: &i32) -> Box<dyn Debug> { Box::new(x) }
112-
| ---- ---------^-
113-
| | | |
114-
| | | ...and is captured here
115-
| | ...is required to be `'static` by this...
112+
| ---- ^ ...is captured here...
113+
| |
116114
| this data with the anonymous lifetime `'_`...
117115
|
116+
note: ...and required to be `'static` by this
117+
--> $DIR/must_outlive_least_region_or_bound.rs:18:41
118+
|
119+
LL | fn elided3(x: &i32) -> Box<dyn Debug> { Box::new(x) }
120+
| ^^^^^^^^^^^
118121
help: to permit non-static references in a trait object value, you can add an explicit bound for the anonymous lifetime `'_`
119122
|
120123
LL | fn elided3(x: &i32) -> Box<dyn Debug + '_> { Box::new(x) }
@@ -124,12 +127,15 @@ error: cannot infer an appropriate lifetime
124127
--> $DIR/must_outlive_least_region_or_bound.rs:21:59
125128
|
126129
LL | fn explicit3<'a>(x: &'a i32) -> Box<dyn Debug> { Box::new(x) }
127-
| ------- ---------^-
128-
| | | |
129-
| | | ...and is captured here
130-
| | ...is required to be `'static` by this...
130+
| ------- ^ ...is captured here...
131+
| |
131132
| this data with lifetime `'a`...
132133
|
134+
note: ...and required to be `'static` by this
135+
--> $DIR/must_outlive_least_region_or_bound.rs:21:50
136+
|
137+
LL | fn explicit3<'a>(x: &'a i32) -> Box<dyn Debug> { Box::new(x) }
138+
| ^^^^^^^^^^^
133139
help: to permit non-static references in a trait object value, you can add an explicit bound for lifetime `'a`
134140
|
135141
LL | fn explicit3<'a>(x: &'a i32) -> Box<dyn Debug + 'a> { Box::new(x) }
@@ -139,12 +145,15 @@ error: cannot infer an appropriate lifetime
139145
--> $DIR/must_outlive_least_region_or_bound.rs:24:60
140146
|
141147
LL | fn elided4(x: &i32) -> Box<dyn Debug + 'static> { Box::new(x) }
142-
| ---- ---------^-
143-
| | | |
144-
| | | ...and is captured here
145-
| | ...is required to be `'static` by this...
148+
| ---- ^ ...is captured here...
149+
| |
146150
| this data with the anonymous lifetime `'_`...
147151
|
152+
note: ...and required to be `'static` by this
153+
--> $DIR/must_outlive_least_region_or_bound.rs:24:51
154+
|
155+
LL | fn elided4(x: &i32) -> Box<dyn Debug + 'static> { Box::new(x) }
156+
| ^^^^^^^^^^^
148157
help: consider changing the trait object's explicit `'static` bound to the anonymous lifetime `'_`
149158
|
150159
LL | fn elided4(x: &i32) -> Box<dyn Debug + '_> { Box::new(x) }
@@ -158,12 +167,13 @@ error: cannot infer an appropriate lifetime
158167
--> $DIR/must_outlive_least_region_or_bound.rs:27:69
159168
|
160169
LL | fn explicit4<'a>(x: &'a i32) -> Box<dyn Debug + 'static> { Box::new(x) }
161-
| ------- ---------^-
162-
| | | |
163-
| | | ...and is captured here
164-
| | ...is required to be `'static` by this...
165-
| this data with lifetime `'a`...
170+
| ------- this data with lifetime `'a`... ^ ...is captured here...
166171
|
172+
note: ...and required to be `'static` by this
173+
--> $DIR/must_outlive_least_region_or_bound.rs:27:60
174+
|
175+
LL | fn explicit4<'a>(x: &'a i32) -> Box<dyn Debug + 'static> { Box::new(x) }
176+
| ^^^^^^^^^^^
167177
help: consider changing the trait object's explicit `'static` bound to lifetime `'a`
168178
|
169179
LL | fn explicit4<'a>(x: &'a i32) -> Box<dyn Debug + 'a> { Box::new(x) }

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ error: cannot infer an appropriate lifetime
44
LL | fn foo<T: Any>(value: &T) -> Box<dyn Any> {
55
| -- this data with the anonymous lifetime `'_`...
66
LL | Box::new(value) as Box<dyn Any>
7-
| ---------^^^^^-
8-
| | |
9-
| | ...and is captured here
10-
| ...is required to be `'static` by this...
7+
| ^^^^^ ...is captured here...
118
|
9+
note: ...and required to be `'static` by this
10+
--> $DIR/issue-16922.rs:4:5
11+
|
12+
LL | Box::new(value) as Box<dyn Any>
13+
| ^^^^^^^^^^^^^^^
1214
help: to permit non-static references in a trait object value, you can add an explicit bound for the anonymous lifetime `'_`
1315
|
1416
LL | fn foo<T: Any>(value: &T) -> Box<dyn Any + '_> {

src/test/ui/regions/region-object-lifetime-in-coercion.stderr

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ error: cannot infer an appropriate lifetime
44
LL | fn a(v: &[u8]) -> Box<dyn Foo + 'static> {
55
| ----- this data with the anonymous lifetime `'_`...
66
LL | let x: Box<dyn Foo + 'static> = Box::new(v);
7-
| ---------^-
8-
| | |
9-
| | ...and is captured here
10-
| ...is required to be `'static` by this...
7+
| ^ ...is captured here...
118
|
9+
note: ...and required to be `'static` by this
10+
--> $DIR/region-object-lifetime-in-coercion.rs:8:37
11+
|
12+
LL | let x: Box<dyn Foo + 'static> = Box::new(v);
13+
| ^^^^^^^^^^^
1214
help: consider changing the trait object's explicit `'static` bound to the anonymous lifetime `'_`
1315
|
1416
LL | fn a(v: &[u8]) -> Box<dyn Foo + '_> {
@@ -24,11 +26,13 @@ error: cannot infer an appropriate lifetime
2426
LL | fn b(v: &[u8]) -> Box<dyn Foo + 'static> {
2527
| ----- this data with the anonymous lifetime `'_`...
2628
LL | Box::new(v)
27-
| ---------^-
28-
| | |
29-
| | ...and is captured here
30-
| ...is required to be `'static` by this...
29+
| ^ ...is captured here...
3130
|
31+
note: ...and required to be `'static` by this
32+
--> $DIR/region-object-lifetime-in-coercion.rs:13:5
33+
|
34+
LL | Box::new(v)
35+
| ^^^^^^^^^^^
3236
help: consider changing the trait object's explicit `'static` bound to the anonymous lifetime `'_`
3337
|
3438
LL | fn b(v: &[u8]) -> Box<dyn Foo + '_> {
@@ -45,11 +49,13 @@ LL | fn c(v: &[u8]) -> Box<dyn Foo> {
4549
| ----- this data with the anonymous lifetime `'_`...
4650
...
4751
LL | Box::new(v)
48-
| ---------^-
49-
| | |
50-
| | ...and is captured here
51-
| ...is required to be `'static` by this...
52+
| ^ ...is captured here...
53+
|
54+
note: ...and required to be `'static` by this
55+
--> $DIR/region-object-lifetime-in-coercion.rs:19:5
5256
|
57+
LL | Box::new(v)
58+
| ^^^^^^^^^^^
5359
help: to permit non-static references in a trait object value, you can add an explicit bound for the anonymous lifetime `'_`
5460
|
5561
LL | fn c(v: &[u8]) -> Box<dyn Foo + '_> {

src/test/ui/regions/regions-close-object-into-object-2.stderr

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ error: cannot infer an appropriate lifetime
44
LL | fn g<'a, T: 'static>(v: Box<dyn A<T> + 'a>) -> Box<dyn X + 'static> {
55
| ------------------ this data with lifetime `'a`...
66
LL | box B(&*v) as Box<dyn X>
7-
| ------^^^---------------
8-
| | |
9-
| | ...and is captured here
10-
| ...is required to be `'static` by this...
7+
| ^^^ ...is captured here...
118
|
9+
note: ...and required to be `'static` by this
10+
--> $DIR/regions-close-object-into-object-2.rs:10:5
11+
|
12+
LL | box B(&*v) as Box<dyn X>
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^
1214
help: consider changing the trait object's explicit `'static` bound to lifetime `'a`
1315
|
1416
LL | fn g<'a, T: 'static>(v: Box<dyn A<T> + 'a>) -> Box<dyn X + 'a> {

src/test/ui/regions/regions-close-object-into-object-4.stderr

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ error: cannot infer an appropriate lifetime
44
LL | fn i<'a, T, U>(v: Box<dyn A<U>+'a>) -> Box<dyn X + 'static> {
55
| ---------------- this data with lifetime `'a`...
66
LL | box B(&*v) as Box<dyn X>
7-
| ------^^^---------------
8-
| | |
9-
| | ...and is captured here
10-
| ...is required to be `'static` by this...
7+
| ^^^ ...is captured here...
118
|
9+
note: ...and required to be `'static` by this
10+
--> $DIR/regions-close-object-into-object-4.rs:10:5
11+
|
12+
LL | box B(&*v) as Box<dyn X>
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^
1214
help: consider changing the trait object's explicit `'static` bound to lifetime `'a`
1315
|
1416
LL | fn i<'a, T, U>(v: Box<dyn A<U>+'a>) -> Box<dyn X + 'a> {

src/test/ui/regions/regions-proc-bound-capture.stderr

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ LL | fn static_proc(x: &isize) -> Box<dyn FnMut() -> (isize) + 'static> {
55
| ------ this data with the anonymous lifetime `'_`...
66
LL | // This is illegal, because the region bound on `proc` is 'static.
77
LL | Box::new(move || { *x })
8-
| ---------^^^^^^^^^^^^^^-
9-
| | |
10-
| | ...and is captured here
11-
| ...is required to be `'static` by this...
8+
| ^^^^^^^^^^^^^^ ...is captured here...
129
|
10+
note: ...and required to be `'static` by this
11+
--> $DIR/regions-proc-bound-capture.rs:9:5
12+
|
13+
LL | Box::new(move || { *x })
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^
1315
help: consider changing the trait object's explicit `'static` bound to the anonymous lifetime `'_`
1416
|
1517
LL | fn static_proc(x: &isize) -> Box<dyn FnMut() -> (isize) + '_> {

0 commit comments

Comments
 (0)