Skip to content

Commit 4dff9b4

Browse files
authored
Rollup merge of rust-lang#115882 - aliemjay:diag-name-region-1, r=compiler-errors
improve the suggestion of `generic_bound_failure` - Fixes rust-lang#115375 - suggest the bound in the correct scope: trait or impl header vs assoc item. See `tests/ui/suggestions/lifetimes/type-param-bound-scope.rs` - don't suggest a lifetime name that conflicts with the other late-bound regions of the function: ```rust type Inv<'a> = *mut &'a (); fn check_bound<'a, T: 'a>(_: T, _: Inv<'a>) {} fn test<'a, T>(_: &'a str, t: T, lt: Inv<'_>) { // suggests a new name `'a` check_bound(t, lt); //~ ERROR } ```
2 parents bf9a1c8 + a883063 commit 4dff9b4

File tree

82 files changed

+1172
-737
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+1172
-737
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+198-310
Large diffs are not rendered by default.

compiler/rustc_middle/src/ty/context.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -1057,24 +1057,29 @@ impl<'tcx> TyCtxt<'tcx> {
10571057
}
10581058

10591059
/// Returns the `DefId` and the `BoundRegionKind` corresponding to the given region.
1060-
pub fn is_suitable_region(self, region: Region<'tcx>) -> Option<FreeRegionInfo> {
1061-
let (suitable_region_binding_scope, bound_region) = match *region {
1062-
ty::ReFree(ref free_region) => {
1063-
(free_region.scope.expect_local(), free_region.bound_region)
1060+
pub fn is_suitable_region(self, mut region: Region<'tcx>) -> Option<FreeRegionInfo> {
1061+
let (suitable_region_binding_scope, bound_region) = loop {
1062+
let def_id = match region.kind() {
1063+
ty::ReFree(fr) => fr.bound_region.get_id()?.as_local()?,
1064+
ty::ReEarlyBound(ebr) => ebr.def_id.expect_local(),
1065+
_ => return None, // not a free region
1066+
};
1067+
let scope = self.local_parent(def_id);
1068+
if self.def_kind(scope) == DefKind::OpaqueTy {
1069+
// Lifetime params of opaque types are synthetic and thus irrelevant to
1070+
// diagnostics. Map them back to their origin!
1071+
region = self.map_rpit_lifetime_to_fn_lifetime(def_id);
1072+
continue;
10641073
}
1065-
ty::ReEarlyBound(ref ebr) => (
1066-
self.local_parent(ebr.def_id.expect_local()),
1067-
ty::BoundRegionKind::BrNamed(ebr.def_id, ebr.name),
1068-
),
1069-
_ => return None, // not a free region
1074+
break (scope, ty::BrNamed(def_id.into(), self.item_name(def_id.into())));
10701075
};
10711076

10721077
let is_impl_item = match self.hir().find_by_def_id(suitable_region_binding_scope) {
10731078
Some(Node::Item(..) | Node::TraitItem(..)) => false,
10741079
Some(Node::ImplItem(..)) => {
10751080
self.is_bound_region_in_impl_item(suitable_region_binding_scope)
10761081
}
1077-
_ => return None,
1082+
_ => false,
10781083
};
10791084

10801085
Some(FreeRegionInfo {

tests/ui/associated-inherent-types/regionck-1.stderr

+8-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ error[E0309]: the parameter type `T` may not live long enough
22
--> $DIR/regionck-1.rs:9:30
33
|
44
LL | type NoTyOutliv<'a, T> = &'a T;
5-
| ^^^^^- help: consider adding a where clause: `where T: 'a`
6-
| |
7-
| ...so that the reference type `&'a T` does not outlive the data it points at
5+
| -- ^^^^^ ...so that the reference type `&'a T` does not outlive the data it points at
6+
| |
7+
| the parameter type `T` must be valid for the lifetime `'a` as defined here...
8+
|
9+
help: consider adding an explicit lifetime bound
10+
|
11+
LL | type NoTyOutliv<'a, T: 'a> = &'a T;
12+
| ++++
813

914
error[E0491]: in type `&'a &'b ()`, reference has a longer lifetime than the data it references
1015
--> $DIR/regionck-1.rs:10:31

tests/ui/async-await/in-trait/async-generics-and-bounds.stderr

+14-20
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,29 @@ error[E0311]: the parameter type `U` may not live long enough
22
--> $DIR/async-generics-and-bounds.rs:12:5
33
|
44
LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
| | |
7+
| | the parameter type `U` must be valid for the anonymous lifetime as defined here...
8+
| ...so that the reference type `&(T, U)` does not outlive the data it points at
69
|
7-
note: the parameter type `U` must be valid for the anonymous lifetime as defined here...
8-
--> $DIR/async-generics-and-bounds.rs:12:18
10+
help: consider adding an explicit lifetime bound
911
|
10-
LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
11-
| ^
12-
note: ...so that the reference type `&(T, U)` does not outlive the data it points at
13-
--> $DIR/async-generics-and-bounds.rs:12:5
14-
|
15-
LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
16-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
LL | async fn foo<'a>(&'a self) -> &'a (T, U) where T: Debug + Sized, U: Hash, U: 'a;
13+
| ++++ ++ ++ +++++++
1714

1815
error[E0311]: the parameter type `T` may not live long enough
1916
--> $DIR/async-generics-and-bounds.rs:12:5
2017
|
2118
LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
22-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
19+
| ^^^^^^^^^^^^^-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
20+
| | |
21+
| | the parameter type `T` must be valid for the anonymous lifetime as defined here...
22+
| ...so that the reference type `&(T, U)` does not outlive the data it points at
2323
|
24-
note: the parameter type `T` must be valid for the anonymous lifetime as defined here...
25-
--> $DIR/async-generics-and-bounds.rs:12:18
24+
help: consider adding an explicit lifetime bound
2625
|
27-
LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
28-
| ^
29-
note: ...so that the reference type `&(T, U)` does not outlive the data it points at
30-
--> $DIR/async-generics-and-bounds.rs:12:5
31-
|
32-
LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
33-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
26+
LL | async fn foo<'a>(&'a self) -> &'a (T, U) where T: Debug + Sized, U: Hash, T: 'a;
27+
| ++++ ++ ++ +++++++
3428

3529
error: aborting due to 2 previous errors
3630

tests/ui/async-await/in-trait/async-generics.stderr

+14-20
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,29 @@ error[E0311]: the parameter type `U` may not live long enough
22
--> $DIR/async-generics.rs:9:5
33
|
44
LL | async fn foo(&self) -> &(T, U);
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^-^^^^^^^^^^^^^^^^^
6+
| | |
7+
| | the parameter type `U` must be valid for the anonymous lifetime as defined here...
8+
| ...so that the reference type `&(T, U)` does not outlive the data it points at
69
|
7-
note: the parameter type `U` must be valid for the anonymous lifetime as defined here...
8-
--> $DIR/async-generics.rs:9:18
10+
help: consider adding an explicit lifetime bound
911
|
10-
LL | async fn foo(&self) -> &(T, U);
11-
| ^
12-
note: ...so that the reference type `&(T, U)` does not outlive the data it points at
13-
--> $DIR/async-generics.rs:9:5
14-
|
15-
LL | async fn foo(&self) -> &(T, U);
16-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
LL | async fn foo<'a>(&'a self) -> &'a (T, U) where U: 'a;
13+
| ++++ ++ ++ +++++++++++
1714

1815
error[E0311]: the parameter type `T` may not live long enough
1916
--> $DIR/async-generics.rs:9:5
2017
|
2118
LL | async fn foo(&self) -> &(T, U);
22-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
19+
| ^^^^^^^^^^^^^-^^^^^^^^^^^^^^^^^
20+
| | |
21+
| | the parameter type `T` must be valid for the anonymous lifetime as defined here...
22+
| ...so that the reference type `&(T, U)` does not outlive the data it points at
2323
|
24-
note: the parameter type `T` must be valid for the anonymous lifetime as defined here...
25-
--> $DIR/async-generics.rs:9:18
24+
help: consider adding an explicit lifetime bound
2625
|
27-
LL | async fn foo(&self) -> &(T, U);
28-
| ^
29-
note: ...so that the reference type `&(T, U)` does not outlive the data it points at
30-
--> $DIR/async-generics.rs:9:5
31-
|
32-
LL | async fn foo(&self) -> &(T, U);
33-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
26+
LL | async fn foo<'a>(&'a self) -> &'a (T, U) where T: 'a;
27+
| ++++ ++ ++ +++++++++++
3428

3529
error: aborting due to 2 previous errors
3630

tests/ui/builtin-superkinds/builtin-superkinds-self-type.stderr

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ 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-
| ^^^ ...so that the type `T` will meet its required lifetime bounds...
5+
| ^^^
6+
| |
7+
| the parameter type `T` must be valid for the static lifetime...
8+
| ...so that the type `T` will meet its required lifetime bounds...
69
|
710
note: ...that is required by this bound
811
--> $DIR/builtin-superkinds-self-type.rs:6:24
912
|
1013
LL | trait Foo : Sized+Sync+'static {
1114
| ^^^^^^^
12-
help: consider adding an explicit lifetime bound...
15+
help: consider adding an explicit lifetime bound
1316
|
1417
LL | impl <T: Sync + 'static> Foo for T { }
1518
| +++++++++

tests/ui/coercion/issue-53475.stderr

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ error[E0310]: the parameter type `T` may not live long enough
22
--> $DIR/issue-53475.rs:10:1
33
|
44
LL | impl<T> CoerceUnsized<Foo<dyn Any>> for Foo<T> {}
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| the parameter type `T` must be valid for the static lifetime...
8+
| ...so that the type `T` will meet its required lifetime bounds
69
|
7-
help: consider adding an explicit lifetime bound...
10+
help: consider adding an explicit lifetime bound
811
|
912
LL | impl<T: 'static> CoerceUnsized<Foo<dyn Any>> for Foo<T> {}
1013
| +++++++++

tests/ui/consts/issue-102117.stderr

+10-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ error[E0310]: the parameter type `T` may not live long enough
22
--> $DIR/issue-102117.rs:19:26
33
|
44
LL | type_id: TypeId::of::<T>(),
5-
| ^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
5+
| ^^^^^^^^^^^^^^^^^
6+
| |
7+
| the parameter type `T` must be valid for the static lifetime...
8+
| ...so that the type `T` will meet its required lifetime bounds
69
|
7-
help: consider adding an explicit lifetime bound...
10+
help: consider adding an explicit lifetime bound
811
|
912
LL | pub fn new<T: 'static>() -> &'static Self {
1013
| +++++++++
@@ -13,10 +16,13 @@ error[E0310]: the parameter type `T` may not live long enough
1316
--> $DIR/issue-102117.rs:19:26
1417
|
1518
LL | type_id: TypeId::of::<T>(),
16-
| ^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
19+
| ^^^^^^^^^^^^^^^^^
20+
| |
21+
| the parameter type `T` must be valid for the static lifetime...
22+
| ...so that the type `T` will meet its required lifetime bounds
1723
|
1824
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
19-
help: consider adding an explicit lifetime bound...
25+
help: consider adding an explicit lifetime bound
2026
|
2127
LL | pub fn new<T: 'static>() -> &'static Self {
2228
| +++++++++

tests/ui/error-codes/E0311.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#![allow(warnings)]
44

5-
fn no_restriction<'a, T: 'a>(x: &'a ()) -> &() {
5+
fn no_restriction<'a, T: 'a>(x: &'a ()) -> &'a () {
66
with_restriction::<T>(x) //~ ERROR E0311
77
}
88

tests/ui/error-codes/E0311.stderr

+6-14
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
error[E0311]: the parameter type `T` may not live long enough
22
--> $DIR/E0311.rs:6:5
33
|
4-
LL | with_restriction::<T>(x)
5-
| ^^^^^^^^^^^^^^^^^^^^^
6-
|
7-
note: the parameter type `T` must be valid for the anonymous lifetime defined here...
8-
--> $DIR/E0311.rs:5:25
9-
|
104
LL | fn no_restriction<T>(x: &()) -> &() {
11-
| ^^^
12-
note: ...so that the type `T` will meet its required lifetime bounds
13-
--> $DIR/E0311.rs:6:5
14-
|
5+
| --- the parameter type `T` must be valid for the anonymous lifetime defined here...
156
LL | with_restriction::<T>(x)
16-
| ^^^^^^^^^^^^^^^^^^^^^
17-
help: consider adding an explicit lifetime bound...
7+
| ^^^^^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
8+
|
9+
help: consider adding an explicit lifetime bound
1810
|
19-
LL | fn no_restriction<'a, T: 'a>(x: &'a ()) -> &() {
20-
| +++ ++++ ++
11+
LL | fn no_restriction<'a, T: 'a>(x: &'a ()) -> &'a () {
12+
| +++ ++++ ++ ++
2113

2214
error: aborting due to previous error
2315

tests/ui/fn/implied-bounds-unnorm-associated-type-5.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ error[E0309]: the parameter type `T` may not live long enough
22
--> $DIR/implied-bounds-unnorm-associated-type-5.rs:6:13
33
|
44
LL | impl<'a, T> Trait<'a> for T {
5-
| ^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds...
5+
| -- ^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds...
6+
| |
7+
| the parameter type `T` must be valid for the lifetime `'a` as defined here...
68
|
79
note: ...that is required by this bound
810
--> $DIR/implied-bounds-unnorm-associated-type-5.rs:1:18
911
|
1012
LL | trait Trait<'a>: 'a {
1113
| ^^
12-
help: consider adding an explicit lifetime bound...
14+
help: consider adding an explicit lifetime bound
1315
|
1416
LL | impl<'a, T: 'a> Trait<'a> for T {
1517
| ++++

tests/ui/generic-associated-types/issue-84931.stderr

+8-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ error[E0309]: the parameter type `T` may not live long enough
22
--> $DIR/issue-84931.rs:14:21
33
|
44
LL | type Item<'a> = &'a mut T;
5-
| ^^^^^^^^^- help: consider adding a where clause: `where T: 'a`
6-
| |
7-
| ...so that the reference type `&'a mut T` does not outlive the data it points at
5+
| -- ^^^^^^^^^ ...so that the reference type `&'a mut T` does not outlive the data it points at
6+
| |
7+
| the parameter type `T` must be valid for the lifetime `'a` as defined here...
8+
|
9+
help: consider adding an explicit lifetime bound
10+
|
11+
LL | type Item<'a> = &'a mut T where T: 'a;
12+
| +++++++++++
813

914
error: aborting due to previous error
1015

tests/ui/impl-trait/must_outlive_least_region_or_bound.stderr

+5-2
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,12 @@ error[E0310]: the parameter type `T` may not live long enough
117117
--> $DIR/must_outlive_least_region_or_bound.rs:43:5
118118
|
119119
LL | x
120-
| ^ ...so that the type `T` will meet its required lifetime bounds
120+
| ^
121+
| |
122+
| the parameter type `T` must be valid for the static lifetime...
123+
| ...so that the type `T` will meet its required lifetime bounds
121124
|
122-
help: consider adding an explicit lifetime bound...
125+
help: consider adding an explicit lifetime bound
123126
|
124127
LL | fn ty_param_wont_outlive_static<T:Debug + 'static>(x: T) -> impl Debug + 'static {
125128
| +++++++++

tests/ui/impl-trait/type_parameters_captured.stderr

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ error[E0310]: the parameter type `T` may not live long enough
22
--> $DIR/type_parameters_captured.rs:8:5
33
|
44
LL | x
5-
| ^ ...so that the type `T` will meet its required lifetime bounds
5+
| ^
6+
| |
7+
| the parameter type `T` must be valid for the static lifetime...
8+
| ...so that the type `T` will meet its required lifetime bounds
69
|
7-
help: consider adding an explicit lifetime bound...
10+
help: consider adding an explicit lifetime bound
811
|
912
LL | fn foo<T: 'static>(x: T) -> impl Any + 'static {
1013
| +++++++++

tests/ui/impl-trait/unactionable_diagnostic.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn foo<'x, P>(
1414
}
1515

1616
pub fn bar<'t, T: 't>(
17-
//~^ HELP: consider adding an explicit lifetime bound...
17+
//~^ HELP: consider adding an explicit lifetime bound
1818
post: T,
1919
x: &'t Foo,
2020
) -> &'t impl Trait {

tests/ui/impl-trait/unactionable_diagnostic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn foo<'x, P>(
1414
}
1515

1616
pub fn bar<'t, T>(
17-
//~^ HELP: consider adding an explicit lifetime bound...
17+
//~^ HELP: consider adding an explicit lifetime bound
1818
post: T,
1919
x: &'t Foo,
2020
) -> &'t impl Trait {

tests/ui/impl-trait/unactionable_diagnostic.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
error[E0309]: the parameter type `T` may not live long enough
22
--> $DIR/unactionable_diagnostic.rs:21:5
33
|
4+
LL | pub fn bar<'t, T>(
5+
| -- the parameter type `T` must be valid for the lifetime `'t` as defined here...
6+
...
47
LL | foo(post, x)
58
| ^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
69
|
7-
help: consider adding an explicit lifetime bound...
10+
help: consider adding an explicit lifetime bound
811
|
912
LL | pub fn bar<'t, T: 't>(
1013
| ++++

0 commit comments

Comments
 (0)