Skip to content

Commit 8f7ee34

Browse files
committed
Tweak type parameter errors to reduce verbosity
1 parent 1d9472b commit 8f7ee34

28 files changed

+78
-374
lines changed

src/librustc_infer/infer/error_reporting/note.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,22 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
1010
err: &mut DiagnosticBuilder<'_>,
1111
origin: &SubregionOrigin<'tcx>,
1212
) {
13+
let mut label_or_note = |span, msg| {
14+
let sub_count = err.children.iter().filter(|d| d.span.is_dummy()).count();
15+
let expanded_sub_count = err.children.iter().filter(|d| !d.span.is_dummy()).count();
16+
let span_is_primary = err.span.primary_spans().iter().all(|&sp| sp == span);
17+
if span_is_primary && sub_count == 0 && expanded_sub_count == 0 {
18+
err.span_label(span, msg);
19+
} else if span_is_primary && expanded_sub_count == 0 {
20+
err.note(msg);
21+
} else {
22+
err.span_note(span, msg);
23+
}
24+
};
1325
match *origin {
1426
infer::Subtype(ref trace) => {
1527
if let Some((expected, found)) = self.values_str(&trace.values) {
16-
err.span_note(
28+
label_or_note(
1729
trace.cause.span,
1830
&format!("...so that the {}", trace.cause.as_requirement_str()),
1931
);
@@ -24,27 +36,27 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
2436
// handling of region checking when type errors are present is
2537
// *terrible*.
2638

27-
err.span_note(
39+
label_or_note(
2840
trace.cause.span,
2941
&format!("...so that {}", trace.cause.as_requirement_str()),
3042
);
3143
}
3244
}
3345
infer::Reborrow(span) => {
34-
err.span_note(span, "...so that reference does not outlive borrowed content");
46+
label_or_note(span, "...so that reference does not outlive borrowed content");
3547
}
3648
infer::ReborrowUpvar(span, ref upvar_id) => {
3749
let var_name = self.tcx.hir().name(upvar_id.var_path.hir_id);
38-
err.span_note(span, &format!("...so that closure can access `{}`", var_name));
50+
label_or_note(span, &format!("...so that closure can access `{}`", var_name));
3951
}
4052
infer::RelateObjectBound(span) => {
41-
err.span_note(span, "...so that it can be closed over into an object");
53+
label_or_note(span, "...so that it can be closed over into an object");
4254
}
4355
infer::CallReturn(span) => {
44-
err.span_note(span, "...so that return value is valid for the call");
56+
label_or_note(span, "...so that return value is valid for the call");
4557
}
4658
infer::DataBorrowed(ty, span) => {
47-
err.span_note(
59+
label_or_note(
4860
span,
4961
&format!(
5062
"...so that the type `{}` is not borrowed for too long",
@@ -53,7 +65,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
5365
);
5466
}
5567
infer::ReferenceOutlivesReferent(ty, span) => {
56-
err.span_note(
68+
label_or_note(
5769
span,
5870
&format!(
5971
"...so that the reference type `{}` does not outlive the data it points at",
@@ -62,7 +74,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
6274
);
6375
}
6476
infer::RelateParamBound(span, t) => {
65-
err.span_note(
77+
label_or_note(
6678
span,
6779
&format!(
6880
"...so that the type `{}` will meet its required lifetime bounds",
@@ -71,13 +83,13 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
7183
);
7284
}
7385
infer::RelateRegionParamBound(span) => {
74-
err.span_note(
86+
label_or_note(
7587
span,
7688
"...so that the declared lifetime parameter bounds are satisfied",
7789
);
7890
}
7991
infer::CompareImplMethodObligation { span, .. } => {
80-
err.span_note(
92+
label_or_note(
8193
span,
8294
"...so that the definition in impl matches the definition from the trait",
8395
);

src/test/ui/builtin-superkinds/builtin-superkinds-self-type.stderr

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@ error[E0310]: the parameter type `T` may not live long enough
22
--> $DIR/builtin-superkinds-self-type.rs:10:16
33
|
44
LL | impl <T: Sync> Foo for T { }
5-
| -- ^^^
5+
| -- ^^^ ...so that the type `T` will meet its required lifetime bounds
66
| |
77
| help: consider adding an explicit lifetime bound...: `T: 'static +`
8-
|
9-
note: ...so that the type `T` will meet its required lifetime bounds
10-
--> $DIR/builtin-superkinds-self-type.rs:10:16
11-
|
12-
LL | impl <T: Sync> Foo for T { }
13-
| ^^^
148

159
error: aborting due to previous error
1610

src/test/ui/feature-gates/feature-gate-infer_static_outlives_requirements.stderr

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@ error[E0310]: the parameter type `U` may not live long enough
44
LL | struct Foo<U> {
55
| - help: consider adding an explicit lifetime bound...: `U: 'static`
66
LL | bar: Bar<U>
7-
| ^^^^^^^^^^^
8-
|
9-
note: ...so that the type `U` will meet its required lifetime bounds
10-
--> $DIR/feature-gate-infer_static_outlives_requirements.rs:5:5
11-
|
12-
LL | bar: Bar<U>
13-
| ^^^^^^^^^^^
7+
| ^^^^^^^^^^^ ...so that the type `U` will meet its required lifetime bounds
148

159
error: aborting due to previous error
1610

src/test/ui/generic-associated-types/impl_bounds.stderr

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ LL | type A<'a> where Self: 'static = (&'a ());
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= help: consider adding an explicit lifetime bound `T: 'static`...
8-
note: ...so that the type `Fooy<T>` will meet its required lifetime bounds
9-
--> $DIR/impl_bounds.rs:15:5
10-
|
11-
LL | type A<'a> where Self: 'static = (&'a ());
12-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8+
= note: ...so that the type `Fooy<T>` will meet its required lifetime bounds
139

1410
error[E0478]: lifetime bound not satisfied
1511
--> $DIR/impl_bounds.rs:17:5

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,9 @@ error[E0310]: the parameter type `T` may not live long enough
6565
--> $DIR/must_outlive_least_region_or_bound.rs:22:51
6666
|
6767
LL | fn ty_param_wont_outlive_static<T:Debug>(x: T) -> impl Debug + 'static {
68-
| -- ^^^^^^^^^^^^^^^^^^^^
68+
| -- ^^^^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
6969
| |
7070
| help: consider adding an explicit lifetime bound...: `T: 'static +`
71-
|
72-
note: ...so that the type `T` will meet its required lifetime bounds
73-
--> $DIR/must_outlive_least_region_or_bound.rs:22:51
74-
|
75-
LL | fn ty_param_wont_outlive_static<T:Debug>(x: T) -> impl Debug + 'static {
76-
| ^^^^^^^^^^^^^^^^^^^^
7771

7872
error: aborting due to 5 previous errors
7973

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@ error[E0310]: the parameter type `T` may not live long enough
22
--> $DIR/type_parameters_captured.rs:7:20
33
|
44
LL | fn foo<T>(x: T) -> impl Any + 'static {
5-
| - ^^^^^^^^^^^^^^^^^^
5+
| - ^^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
66
| |
77
| help: consider adding an explicit lifetime bound...: `T: 'static`
8-
|
9-
note: ...so that the type `T` will meet its required lifetime bounds
10-
--> $DIR/type_parameters_captured.rs:7:20
11-
|
12-
LL | fn foo<T>(x: T) -> impl Any + 'static {
13-
| ^^^^^^^^^^^^^^^^^^
148

159
error: aborting due to previous error
1610

src/test/ui/lifetimes/lifetime-doesnt-live-long-enough.stderr

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,15 @@ error[E0310]: the parameter type `T` may not live long enough
44
LL | struct Foo<T> {
55
| - help: consider adding an explicit lifetime bound...: `T: 'static`
66
LL | foo: &'static T
7-
| ^^^^^^^^^^^^^^^
8-
|
9-
note: ...so that the reference type `&'static T` does not outlive the data it points at
10-
--> $DIR/lifetime-doesnt-live-long-enough.rs:19:5
11-
|
12-
LL | foo: &'static T
13-
| ^^^^^^^^^^^^^^^
7+
| ^^^^^^^^^^^^^^^ ...so that the reference type `&'static T` does not outlive the data it points at
148

159
error[E0309]: the parameter type `K` may not live long enough
1610
--> $DIR/lifetime-doesnt-live-long-enough.rs:24:19
1711
|
1812
LL | trait X<K>: Sized {
1913
| - help: consider adding an explicit lifetime bound...: `K: 'a`
2014
LL | fn foo<'a, L: X<&'a Nested<K>>>();
21-
| ^^^^^^^^^^^^^^^^
22-
|
23-
note: ...so that the reference type `&'a Nested<K>` does not outlive the data it points at
24-
--> $DIR/lifetime-doesnt-live-long-enough.rs:24:19
25-
|
26-
LL | fn foo<'a, L: X<&'a Nested<K>>>();
27-
| ^^^^^^^^^^^^^^^^
15+
| ^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<K>` does not outlive the data it points at
2816

2917
error[E0309]: the parameter type `Self` may not live long enough
3018
--> $DIR/lifetime-doesnt-live-long-enough.rs:28:19
@@ -33,51 +21,31 @@ LL | fn bar<'a, L: X<&'a Nested<Self>>>();
3321
| ^^^^^^^^^^^^^^^^^^^
3422
|
3523
= help: consider adding an explicit lifetime bound `Self: 'a`...
36-
note: ...so that the reference type `&'a Nested<Self>` does not outlive the data it points at
37-
--> $DIR/lifetime-doesnt-live-long-enough.rs:28:19
38-
|
39-
LL | fn bar<'a, L: X<&'a Nested<Self>>>();
40-
| ^^^^^^^^^^^^^^^^^^^
24+
= note: ...so that the reference type `&'a Nested<Self>` does not outlive the data it points at
4125

4226
error[E0309]: the parameter type `L` may not live long enough
4327
--> $DIR/lifetime-doesnt-live-long-enough.rs:32:22
4428
|
4529
LL | fn baz<'a, L, M: X<&'a Nested<L>>>() {
46-
| - ^^^^^^^^^^^^^^^^
30+
| - ^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<L>` does not outlive the data it points at
4731
| |
4832
| help: consider adding an explicit lifetime bound...: `L: 'a`
49-
|
50-
note: ...so that the reference type `&'a Nested<L>` does not outlive the data it points at
51-
--> $DIR/lifetime-doesnt-live-long-enough.rs:32:22
52-
|
53-
LL | fn baz<'a, L, M: X<&'a Nested<L>>>() {
54-
| ^^^^^^^^^^^^^^^^
5533

5634
error[E0309]: the parameter type `K` may not live long enough
5735
--> $DIR/lifetime-doesnt-live-long-enough.rs:41:33
5836
|
5937
LL | impl<K> Nested<K> {
6038
| - help: consider adding an explicit lifetime bound...: `K: 'a`
6139
LL | fn generic_in_parent<'a, L: X<&'a Nested<K>>>() {
62-
| ^^^^^^^^^^^^^^^^
63-
|
64-
note: ...so that the reference type `&'a Nested<K>` does not outlive the data it points at
65-
--> $DIR/lifetime-doesnt-live-long-enough.rs:41:33
66-
|
67-
LL | fn generic_in_parent<'a, L: X<&'a Nested<K>>>() {
68-
| ^^^^^^^^^^^^^^^^
40+
| ^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<K>` does not outlive the data it points at
6941

7042
error[E0309]: the parameter type `M` may not live long enough
7143
--> $DIR/lifetime-doesnt-live-long-enough.rs:44:36
7244
|
7345
LL | fn generic_in_child<'a, 'b, L: X<&'a Nested<M>>, M: 'b>() {
7446
| ^^^^^^^^^^^^^^^^ -- help: consider adding an explicit lifetime bound...: `M: 'a +`
75-
|
76-
note: ...so that the reference type `&'a Nested<M>` does not outlive the data it points at
77-
--> $DIR/lifetime-doesnt-live-long-enough.rs:44:36
78-
|
79-
LL | fn generic_in_child<'a, 'b, L: X<&'a Nested<M>>, M: 'b>() {
80-
| ^^^^^^^^^^^^^^^^
47+
| |
48+
| ...so that the reference type `&'a Nested<M>` does not outlive the data it points at
8149

8250
error: aborting due to 6 previous errors
8351

src/test/ui/nll/ty-outlives/projection-where-clause-env-wrong-bound.stderr

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ LL | bar::<T::Output>()
55
| ^^^^^^^^^^^^^^^^
66
|
77
= help: consider adding an explicit lifetime bound `<T as MyTrait<'a>>::Output: 'a`...
8-
note: ...so that the type `<T as MyTrait<'a>>::Output` will meet its required lifetime bounds
9-
--> $DIR/projection-where-clause-env-wrong-bound.rs:15:5
10-
|
11-
LL | bar::<T::Output>()
12-
| ^^^^^^^^^^^^^^^^
8+
= note: ...so that the type `<T as MyTrait<'a>>::Output` will meet its required lifetime bounds
139

1410
error: aborting due to previous error
1511

src/test/ui/nll/ty-outlives/projection-where-clause-env-wrong-lifetime.stderr

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ LL | bar::<<T as MyTrait<'a>>::Output>()
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= help: consider adding an explicit lifetime bound `<T as MyTrait<'a>>::Output: 'a`...
8-
note: ...so that the type `<T as MyTrait<'a>>::Output` will meet its required lifetime bounds
9-
--> $DIR/projection-where-clause-env-wrong-lifetime.rs:14:5
10-
|
11-
LL | bar::<<T as MyTrait<'a>>::Output>()
12-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8+
= note: ...so that the type `<T as MyTrait<'a>>::Output` will meet its required lifetime bounds
139

1410
error: aborting due to previous error
1511

src/test/ui/regions/regions-close-associated-type-into-object.stderr

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ LL | Box::new(item)
55
| ^^^^^^^^^^^^^^
66
|
77
= help: consider adding an explicit lifetime bound `<T as Iter>::Item: 'static`...
8-
note: ...so that the type `<T as Iter>::Item` will meet its required lifetime bounds
9-
--> $DIR/regions-close-associated-type-into-object.rs:15:5
10-
|
11-
LL | Box::new(item)
12-
| ^^^^^^^^^^^^^^
8+
= note: ...so that the type `<T as Iter>::Item` will meet its required lifetime bounds
139

1410
error[E0310]: the associated type `<T as Iter>::Item` may not live long enough
1511
--> $DIR/regions-close-associated-type-into-object.rs:22:5
@@ -18,11 +14,7 @@ LL | Box::new(item)
1814
| ^^^^^^^^^^^^^^
1915
|
2016
= help: consider adding an explicit lifetime bound `<T as Iter>::Item: 'static`...
21-
note: ...so that the type `std::boxed::Box<<T as Iter>::Item>` will meet its required lifetime bounds
22-
--> $DIR/regions-close-associated-type-into-object.rs:22:5
23-
|
24-
LL | Box::new(item)
25-
| ^^^^^^^^^^^^^^
17+
= note: ...so that the type `std::boxed::Box<<T as Iter>::Item>` will meet its required lifetime bounds
2618

2719
error[E0309]: the associated type `<T as Iter>::Item` may not live long enough
2820
--> $DIR/regions-close-associated-type-into-object.rs:28:5
@@ -31,11 +23,7 @@ LL | Box::new(item)
3123
| ^^^^^^^^^^^^^^
3224
|
3325
= help: consider adding an explicit lifetime bound `<T as Iter>::Item: 'a`...
34-
note: ...so that the type `<T as Iter>::Item` will meet its required lifetime bounds
35-
--> $DIR/regions-close-associated-type-into-object.rs:28:5
36-
|
37-
LL | Box::new(item)
38-
| ^^^^^^^^^^^^^^
26+
= note: ...so that the type `<T as Iter>::Item` will meet its required lifetime bounds
3927

4028
error[E0309]: the associated type `<T as Iter>::Item` may not live long enough
4129
--> $DIR/regions-close-associated-type-into-object.rs:35:5
@@ -44,11 +32,7 @@ LL | Box::new(item)
4432
| ^^^^^^^^^^^^^^
4533
|
4634
= help: consider adding an explicit lifetime bound `<T as Iter>::Item: 'a`...
47-
note: ...so that the type `std::boxed::Box<<T as Iter>::Item>` will meet its required lifetime bounds
48-
--> $DIR/regions-close-associated-type-into-object.rs:35:5
49-
|
50-
LL | Box::new(item)
51-
| ^^^^^^^^^^^^^^
35+
= note: ...so that the type `std::boxed::Box<<T as Iter>::Item>` will meet its required lifetime bounds
5236

5337
error: aborting due to 4 previous errors
5438

0 commit comments

Comments
 (0)