Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit dd81e98

Browse files
committed
Clean up visual output logic
1 parent d10fe26 commit dd81e98

14 files changed

+61
-106
lines changed

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs

Lines changed: 21 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -139,89 +139,39 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
139139
// | ...is captured here...
140140
(false, sup_origin.span())
141141
} else {
142-
(true, param.param_ty_span)
142+
(!sup_origin.span().overlaps(return_sp), param.param_ty_span)
143143
};
144144
err.span_label(capture_point, &format!("this data with {}...", lifetime));
145145

146146
debug!("try_report_static_impl_trait: param_info={:?}", param);
147147

148+
let mut spans = spans.clone();
149+
150+
if mention_capture {
151+
spans.push(sup_origin.span());
152+
}
153+
spans.sort();
154+
spans.dedup();
155+
148156
// We try to make the output have fewer overlapping spans if possible.
149-
if (sp == sup_origin.span() || !return_sp.overlaps(sup_origin.span()))
150-
&& sup_origin.span() != return_sp
151-
{
152-
// Customize the spans and labels depending on their relative order so
153-
// that split sentences flow correctly.
154-
if sup_origin.span().overlaps(return_sp) && sp == sup_origin.span() {
155-
// Avoid the following:
156-
//
157-
// error: cannot infer an appropriate lifetime
158-
// --> $DIR/must_outlive_least_region_or_bound.rs:18:50
159-
// |
160-
// LL | fn foo(x: &i32) -> Box<dyn Debug> { Box::new(x) }
161-
// | ---- ---------^-
162-
//
163-
// and instead show:
164-
//
165-
// error: cannot infer an appropriate lifetime
166-
// --> $DIR/must_outlive_least_region_or_bound.rs:18:50
167-
// |
168-
// LL | fn foo(x: &i32) -> Box<dyn Debug> { Box::new(x) }
169-
// | ---- ^
170-
err.span_label(
171-
sup_origin.span(),
172-
&format!(
173-
"...is captured here, requiring it to live as long as `'static`{}",
174-
if spans.is_empty() { "" } else { "..." },
175-
),
176-
);
177-
} else {
178-
if return_sp < sup_origin.span() && mention_capture {
179-
err.span_label(sup_origin.span(), "...is captured here...");
180-
err.span_note(
181-
return_sp,
182-
"...and is required to live as long as `'static` here",
183-
);
184-
} else {
185-
err.span_label(
186-
return_sp,
187-
&format!(
188-
"...is required to live as long as `'static` here{}",
189-
if spans.is_empty() { "" } else { "..." },
190-
),
191-
);
192-
if mention_capture {
193-
let span = sup_origin.span();
194-
let msg = if spans.iter().any(|sp| *sp > span) {
195-
"...is captured here..."
196-
} else {
197-
"...and is captured here"
198-
};
199-
err.span_label(span, msg);
200-
}
201-
}
202-
}
157+
let (require_msg, require_span) = if sup_origin.span().overlaps(return_sp) {
158+
("...is captured and required to live as long as `'static` here", sup_origin.span())
203159
} else {
204-
err.span_label(
205-
return_sp,
206-
&format!(
207-
"...is captured and required to live as long as `'static` here{}",
208-
if spans.is_empty() { "" } else { "..." },
209-
),
210-
);
160+
("...and is required to live as long as `'static` here", return_sp)
161+
};
162+
163+
for span in &spans {
164+
err.span_label(*span, "...is captured here...");
211165
}
212166

213-
for span in spans {
214-
let msg =
215-
format!("...and is captured here{}", if mention_capture { " too" } else { "" });
216-
if span.overlaps(return_sp) {
217-
err.span_note(*span, &msg);
218-
} else {
219-
err.span_label(*span, &msg);
220-
}
167+
if spans.iter().any(|sp| sp.overlaps(return_sp) || *sp > return_sp) {
168+
err.span_note(require_span, require_msg);
169+
} else {
170+
err.span_label(require_span, require_msg);
221171
}
222172

223173
if let SubregionOrigin::RelateParamBound(_, _, Some(bound)) = sub_origin {
224-
err.span_note(*bound, "`'static` lifetime requirement introduced by this trait bound");
174+
err.span_note(*bound, "`'static` lifetime requirement introduced by this bound");
225175
}
226176

227177
let fn_returns = tcx.return_type_impl_or_dyn_traits(anon_reg_sup.def_id);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0759]: `x` has lifetime `'a` but it needs to satisfy a `'static` lifetime
44
LL | fn baz<'a,'b>(x: &'a u32) -> &'static u32 {
55
| ------- this data with lifetime `'a`...
66
LL | bar(foo, x)
7-
| ----^^^---- ...is captured and required to live as long as `'static` here
7+
| ^^^ - ...is captured and required to live as long as `'static` here
88

99
error: aborting due to previous error
1010

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | fn baz<'a, 'b>(x: Type<'a>) -> Type<'static> {
55
| -------- this data with lifetime `'a`...
66
...
77
LL | bar(foo, x)
8-
| ----^^^---- ...is captured and required to live as long as `'static` here
8+
| ^^^ - ...is captured and required to live as long as `'static` here
99

1010
error: aborting due to previous error
1111

src/test/ui/async-await/issues/issue-62097.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ error[E0759]: `self` has an anonymous lifetime `'_` but it needs to satisfy a `'
44
LL | pub async fn run_dummy_fn(&self) {
55
| ^^^^^ this data with an anonymous lifetime `'_`...
66
LL | foo(|| self.bar()).await;
7-
| --- ...is required to live as long as `'static` here...
7+
| ------------------------ ...is captured here...
88
|
9-
note: ...and is captured here
9+
note: ...and is required to live as long as `'static` here
1010
--> $DIR/issue-62097.rs:13:9
1111
|
1212
LL | foo(|| self.bar()).await;
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^
14-
note: `'static` lifetime requirement introduced by this trait bound
13+
| ^^^
14+
note: `'static` lifetime requirement introduced by this bound
1515
--> $DIR/issue-62097.rs:4:19
1616
|
1717
LL | F: FnOnce() + 'static

src/test/ui/async-await/issues/issue-72312.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// edition:2018
22
fn require_static<T: 'static>(val: T) -> T {
3-
//~^ NOTE 'static` lifetime requirement introduced by this trait bound
3+
//~^ NOTE 'static` lifetime requirement introduced by this bound
44
val
55
}
66

@@ -10,8 +10,8 @@ impl Problem {
1010
pub async fn start(&self) { //~ ERROR E0759
1111
//~^ NOTE this data with an anonymous lifetime `'_`
1212
//~| NOTE in this expansion of desugaring of `async` block or function
13-
require_static(async move { //~ NOTE ...is required to live as long as `'static` here
14-
&self; //~ NOTE ...and is captured here
13+
require_static(async move { //~ NOTE ...and is required to live as long as `'static` here
14+
&self; //~ NOTE ...is captured here...
1515
});
1616
}
1717
}

src/test/ui/async-await/issues/issue-72312.stderr

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ error[E0759]: `self` has an anonymous lifetime `'_` but it needs to satisfy a `'
44
LL | pub async fn start(&self) {
55
| ^^^^^ this data with an anonymous lifetime `'_`...
66
...
7-
LL | require_static(async move {
8-
| -------------- ...is required to live as long as `'static` here...
97
LL | &self;
10-
| ----- ...and is captured here
8+
| ----- ...is captured here...
9+
|
10+
note: ...and is required to live as long as `'static` here
11+
--> $DIR/issue-72312.rs:13:9
1112
|
12-
note: `'static` lifetime requirement introduced by this trait bound
13+
LL | require_static(async move {
14+
| ^^^^^^^^^^^^^^
15+
note: `'static` lifetime requirement introduced by this bound
1316
--> $DIR/issue-72312.rs:2:22
1417
|
1518
LL | fn require_static<T: 'static>(val: T) -> T {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ error[E0759]: `x` has an anonymous lifetime `'_` but it needs to satisfy a `'sta
8080
--> $DIR/must_outlive_least_region_or_bound.rs:24:65
8181
|
8282
LL | fn elided5(x: &i32) -> (Box<dyn Debug>, impl Debug) { (Box::new(x), x) }
83-
| ---- this data with an anonymous lifetime `'_`... ^ ...is captured here, requiring it to live as long as `'static`
83+
| ---- this data with an anonymous lifetime `'_`... ^ ...is captured and required to live as long as `'static` here
8484
|
8585
help: to declare that the trait object captures data from argument `x`, you can add an explicit `'_` lifetime bound
8686
|
@@ -136,7 +136,7 @@ error[E0759]: `x` has an anonymous lifetime `'_` but it needs to satisfy a `'sta
136136
--> $DIR/must_outlive_least_region_or_bound.rs:16:50
137137
|
138138
LL | fn elided3(x: &i32) -> Box<dyn Debug> { Box::new(x) }
139-
| ---- ^ ...is captured here, requiring it to live as long as `'static`
139+
| ---- ^ ...is captured and required to live as long as `'static` here
140140
| |
141141
| this data with an anonymous lifetime `'_`...
142142
|
@@ -149,7 +149,7 @@ error[E0759]: `x` has lifetime `'a` but it needs to satisfy a `'static` lifetime
149149
--> $DIR/must_outlive_least_region_or_bound.rs:18:59
150150
|
151151
LL | fn explicit3<'a>(x: &'a i32) -> Box<dyn Debug> { Box::new(x) }
152-
| ------- ^ ...is captured here, requiring it to live as long as `'static`
152+
| ------- ^ ...is captured and required to live as long as `'static` here
153153
| |
154154
| this data with lifetime `'a`...
155155
|
@@ -162,7 +162,7 @@ error[E0759]: `x` has an anonymous lifetime `'_` but it needs to satisfy a `'sta
162162
--> $DIR/must_outlive_least_region_or_bound.rs:20:60
163163
|
164164
LL | fn elided4(x: &i32) -> Box<dyn Debug + 'static> { Box::new(x) }
165-
| ---- ^ ...is captured here, requiring it to live as long as `'static`
165+
| ---- ^ ...is captured and required to live as long as `'static` here
166166
| |
167167
| this data with an anonymous lifetime `'_`...
168168
|
@@ -179,7 +179,7 @@ error[E0759]: `x` has lifetime `'a` but it needs to satisfy a `'static` lifetime
179179
--> $DIR/must_outlive_least_region_or_bound.rs:22:69
180180
|
181181
LL | fn explicit4<'a>(x: &'a i32) -> Box<dyn Debug + 'static> { Box::new(x) }
182-
| ------- this data with lifetime `'a`... ^ ...is captured here, requiring it to live as long as `'static`
182+
| ------- this data with lifetime `'a`... ^ ...is captured and required to live as long as `'static` here
183183
|
184184
help: consider changing the trait object's explicit `'static` bound to the lifetime of argument `x`
185185
|

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0759]: `value` has an anonymous lifetime `'_` but it needs to satisfy a `
44
LL | fn foo<T: Any>(value: &T) -> Box<dyn Any> {
55
| -- this data with an anonymous lifetime `'_`...
66
LL | Box::new(value) as Box<dyn Any>
7-
| ^^^^^ ...is captured here, requiring it to live as long as `'static`
7+
| ^^^^^ ...is captured and required to live as long as `'static` here
88
|
99
help: to declare that the trait object captures data from argument `value`, you can add an explicit `'_` lifetime bound
1010
|

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0759]: `v` has an anonymous lifetime `'_` but it needs to satisfy a `'sta
44
LL | fn a(v: &[u8]) -> Box<dyn Foo + 'static> {
55
| ----- this data with an anonymous lifetime `'_`...
66
LL | let x: Box<dyn Foo + 'static> = Box::new(v);
7-
| ^ ...is captured here, requiring it to live as long as `'static`
7+
| ^ ...is captured and required to live as long as `'static` here
88
|
99
help: consider changing the trait object's explicit `'static` bound to the lifetime of argument `v`
1010
|
@@ -21,7 +21,7 @@ error[E0759]: `v` has an anonymous lifetime `'_` but it needs to satisfy a `'sta
2121
LL | fn b(v: &[u8]) -> Box<dyn Foo + 'static> {
2222
| ----- this data with an anonymous lifetime `'_`...
2323
LL | Box::new(v)
24-
| ^ ...is captured here, requiring it to live as long as `'static`
24+
| ^ ...is captured and required to live as long as `'static` here
2525
|
2626
help: consider changing the trait object's explicit `'static` bound to the lifetime of argument `v`
2727
|
@@ -39,7 +39,7 @@ LL | fn c(v: &[u8]) -> Box<dyn Foo> {
3939
| ----- this data with an anonymous lifetime `'_`...
4040
...
4141
LL | Box::new(v)
42-
| ^ ...is captured here, requiring it to live as long as `'static`
42+
| ^ ...is captured and required to live as long as `'static` here
4343
|
4444
help: to declare that the trait object captures data from argument `v`, you can add an explicit `'_` lifetime bound
4545
|

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0759]: `v` has lifetime `'a` but it needs to satisfy a `'static` 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::new(B(&*v)) as Box<dyn X>
7-
| ^^^ ...is captured here, requiring it to live as long as `'static`
7+
| ^^^ ...is captured and required to live as long as `'static` here
88
|
99
help: consider changing the trait object's explicit `'static` bound to the lifetime of argument `v`
1010
|

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0759]: `v` has lifetime `'a` but it needs to satisfy a `'static` 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::new(B(&*v)) as Box<dyn X>
7-
| ^^^ ...is captured here, requiring it to live as long as `'static`
7+
| ^^^ ...is captured and required to live as long as `'static` here
88
|
99
help: consider changing the trait object's explicit `'static` bound to the lifetime of argument `v`
1010
|

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | fn static_proc(x: &isize) -> Box<dyn FnMut() -> (isize) + 'static> {
55
| ------ this data with an anonymous lifetime `'_`...
66
LL | // This is illegal, because the region bound on `proc` is 'static.
77
LL | Box::new(move || { *x })
8-
| ^^^^^^^^^^^^^^ ...is captured here, requiring it to live as long as `'static`
8+
| ^^^^^^^^^^^^^^ ...is captured and required to live as long as `'static` here
99
|
1010
help: consider changing the trait object's explicit `'static` bound to the lifetime of argument `x`
1111
|

src/test/ui/traits/trait-upcasting/type-checking-test-4.stderr

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,29 @@ LL | let y = x as &dyn Bar<'_, '_>;
3939
| ...is captured here...
4040
LL |
4141
LL | y.get_b() // ERROR
42-
| --------- ...is required to live as long as `'static` here...
42+
| - ...is captured here...
4343
|
44-
note: ...and is captured here too
44+
note: ...and is required to live as long as `'static` here
4545
--> $DIR/type-checking-test-4.rs:29:5
4646
|
4747
LL | y.get_b() // ERROR
48-
| ^
48+
| ^^^^^^^^^
4949

5050
error[E0759]: `x` has lifetime `'a` but it needs to satisfy a `'static` lifetime requirement
5151
--> $DIR/type-checking-test-4.rs:33:5
5252
|
5353
LL | fn test_wrong4<'a>(x: &dyn Foo<'a>) -> Option<&'static u32> {
5454
| ------------ this data with lifetime `'a`...
5555
LL | <_ as Bar>::get_b(x) // ERROR
56-
| ^^^^^^^^^^^^^^^^^ ...is captured here, requiring it to live as long as `'static`
56+
| ^^^^^^^^^^^^^^^^^ ...is captured and required to live as long as `'static` here
5757

5858
error[E0759]: `x` has lifetime `'a` but it needs to satisfy a `'static` lifetime requirement
5959
--> $DIR/type-checking-test-4.rs:38:15
6060
|
6161
LL | fn test_wrong5<'a>(x: &dyn Foo<'a>) -> Option<&'static u32> {
6262
| ------------ this data with lifetime `'a`...
6363
LL | <_ as Bar<'_, '_>>::get_b(x) // ERROR
64-
| ----------^^---------------- ...is captured and required to live as long as `'static` here
64+
| ----------^^------------- ...is captured and required to live as long as `'static` here
6565

6666
error[E0759]: `x` has lifetime `'a` but it needs to satisfy a `'static` lifetime requirement
6767
--> $DIR/type-checking-test-4.rs:43:27
@@ -74,16 +74,16 @@ LL | let y = x as &dyn Bar<'_, '_>;
7474
| ...is captured here...
7575
LL |
7676
LL | y.get_b(); // ERROR
77-
| - ...and is captured here too
77+
| - ...is captured here...
7878
LL | let z = y;
7979
LL | z.get_b() // ERROR
80-
| --------- ...is required to live as long as `'static` here...
80+
| - ...is captured here...
8181
|
82-
note: ...and is captured here too
82+
note: ...and is required to live as long as `'static` here
8383
--> $DIR/type-checking-test-4.rs:47:5
8484
|
8585
LL | z.get_b() // ERROR
86-
| ^
86+
| ^^^^^^^^^
8787

8888
error: aborting due to 6 previous errors
8989

src/test/ui/underscore-lifetime/dyn-trait-underscore.stderr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ LL | fn a<T>(items: &[T]) -> Box<dyn Iterator<Item=&T>> {
55
| ---- this data with an anonymous lifetime `'_`...
66
LL | // ^^^^^^^^^^^^^^^^^^^^^ bound *here* defaults to `'static`
77
LL | Box::new(items.iter())
8-
| ---------------^^^^--- ...is captured and required to live as long as `'static` here
8+
| ----- ^^^^
9+
| |
10+
| ...is captured and required to live as long as `'static` here
911
|
1012
help: to declare that the trait object captures data from argument `items`, you can add an explicit `'_` lifetime bound
1113
|

0 commit comments

Comments
 (0)