Skip to content

Commit 7b0db3e

Browse files
committed
Improve span for consider adding an explicit lifetime bound suggestions under NLL
Because NLL borrowck is run after typeck, `in_progress_typeck_results` was always `None` which was preventing the retrieval of the span to which the suggestion is suppose to add the lifetime bound. We now manually pass the `LocalDefId` owner to `construct_generic_bound_failure` so that under NLL, we give the owner id of the current body.
1 parent 6b4563b commit 7b0db3e

13 files changed

+69
-60
lines changed

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
171171
None,
172172
type_test.generic_kind,
173173
lower_bound_region,
174+
self.body.source.def_id().as_local(),
174175
));
175176
} else {
176177
// FIXME. We should handle this case better. It

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

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
6161
use rustc_errors::{pluralize, struct_span_err, Diagnostic, ErrorGuaranteed};
6262
use rustc_errors::{Applicability, DiagnosticBuilder, DiagnosticStyledString, MultiSpan};
6363
use rustc_hir as hir;
64-
use rustc_hir::def_id::DefId;
64+
use rustc_hir::def_id::{DefId, LocalDefId};
6565
use rustc_hir::lang_items::LangItem;
6666
use rustc_hir::{Item, ItemKind, Node};
6767
use rustc_middle::dep_graph::DepContext;
@@ -2285,7 +2285,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
22852285
bound_kind: GenericKind<'tcx>,
22862286
sub: Region<'tcx>,
22872287
) {
2288-
self.construct_generic_bound_failure(span, origin, bound_kind, sub).emit();
2288+
let owner =
2289+
self.in_progress_typeck_results.map(|typeck_results| typeck_results.borrow().hir_owner);
2290+
self.construct_generic_bound_failure(span, origin, bound_kind, sub, owner).emit();
22892291
}
22902292

22912293
pub fn construct_generic_bound_failure(
@@ -2294,31 +2296,29 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
22942296
origin: Option<SubregionOrigin<'tcx>>,
22952297
bound_kind: GenericKind<'tcx>,
22962298
sub: Region<'tcx>,
2299+
owner: Option<LocalDefId>,
22972300
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
22982301
let hir = self.tcx.hir();
22992302
// Attempt to obtain the span of the parameter so we can
23002303
// suggest adding an explicit lifetime bound to it.
2301-
let generics = self
2302-
.in_progress_typeck_results
2303-
.map(|typeck_results| typeck_results.borrow().hir_owner)
2304-
.map(|owner| {
2305-
let hir_id = hir.local_def_id_to_hir_id(owner);
2306-
let parent_id = hir.get_parent_item(hir_id);
2307-
(
2308-
// Parent item could be a `mod`, so we check the HIR before calling:
2309-
if let Some(Node::Item(Item {
2310-
kind: ItemKind::Trait(..) | ItemKind::Impl { .. },
2311-
..
2312-
})) = hir.find_by_def_id(parent_id)
2313-
{
2314-
Some(self.tcx.generics_of(parent_id))
2315-
} else {
2316-
None
2317-
},
2318-
self.tcx.generics_of(owner.to_def_id()),
2319-
hir.span(hir_id),
2320-
)
2321-
});
2304+
let generics = owner.map(|owner| {
2305+
let hir_id = hir.local_def_id_to_hir_id(owner);
2306+
let parent_id = hir.get_parent_item(hir_id);
2307+
(
2308+
// Parent item could be a `mod`, so we check the HIR before calling:
2309+
if let Some(Node::Item(Item {
2310+
kind: ItemKind::Trait(..) | ItemKind::Impl { .. },
2311+
..
2312+
})) = hir.find_by_def_id(parent_id)
2313+
{
2314+
Some(self.tcx.generics_of(parent_id))
2315+
} else {
2316+
None
2317+
},
2318+
self.tcx.generics_of(owner.to_def_id()),
2319+
hir.span(hir_id),
2320+
)
2321+
});
23222322

23232323
let span = match generics {
23242324
// This is to get around the trait identity obligation, that has a `DUMMY_SP` as signal
@@ -2606,11 +2606,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
26062606
None,
26072607
);
26082608
if let Some(infer::RelateParamBound(_, t, _)) = origin {
2609-
let return_impl_trait = self
2610-
.in_progress_typeck_results
2611-
.map(|typeck_results| typeck_results.borrow().hir_owner)
2612-
.and_then(|owner| self.tcx.return_type_impl_trait(owner))
2613-
.is_some();
2609+
let return_impl_trait =
2610+
owner.and_then(|owner| self.tcx.return_type_impl_trait(owner)).is_some();
26142611
let t = self.resolve_vars_if_possible(t);
26152612
match t.kind() {
26162613
// We've got:

src/test/ui/nll/closure-requirements/propagate-from-trait-match.stderr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ LL | | }
3636
error[E0309]: the parameter type `T` may not live long enough
3737
--> $DIR/propagate-from-trait-match.rs:32:36
3838
|
39+
LL | fn supply<'a, T>(value: T)
40+
| - help: consider adding an explicit lifetime bound...: `T: 'a`
41+
...
3942
LL | establish_relationships(value, |value| {
4043
| ____________________________________^
4144
LL | |
@@ -45,8 +48,6 @@ LL | | // This function call requires that
4548
LL | | require(value);
4649
LL | | });
4750
| |_____^
48-
|
49-
= help: consider adding an explicit lifetime bound `T: 'a`...
5051

5152
error: aborting due to previous error
5253

src/test/ui/nll/ty-outlives/impl-trait-outlives.stderr

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
error[E0309]: the parameter type `T` may not live long enough
22
--> $DIR/impl-trait-outlives.rs:11:5
33
|
4+
LL | fn no_region<'a, T>(x: Box<T>) -> impl Debug + 'a
5+
| - help: consider adding an explicit lifetime bound...: `T: 'a`
6+
...
47
LL | x
58
| ^
6-
|
7-
= help: consider adding an explicit lifetime bound `T: 'a`...
89

910
error[E0309]: the parameter type `T` may not live long enough
1011
--> $DIR/impl-trait-outlives.rs:26:5
1112
|
13+
LL | fn wrong_region<'a, 'b, T>(x: Box<T>) -> impl Debug + 'a
14+
| - help: consider adding an explicit lifetime bound...: `T: 'a`
15+
...
1216
LL | x
1317
| ^
14-
|
15-
= help: consider adding an explicit lifetime bound `T: 'a`...
1618

1719
error: aborting due to 2 previous errors
1820

src/test/ui/nll/ty-outlives/projection-implied-bounds.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
error[E0310]: the parameter type `T` may not live long enough
22
--> $DIR/projection-implied-bounds.rs:30:18
33
|
4+
LL | fn generic2<T: Iterator>(value: T) {
5+
| -- help: consider adding an explicit lifetime bound...: `T: 'static +`
46
LL | twice(value, |value_ref, item| invoke2(value_ref, item));
57
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6-
|
7-
= help: consider adding an explicit lifetime bound `T: 'static`...
88

99
error: aborting due to previous error
1010

src/test/ui/nll/ty-outlives/projection-one-region-closure.stderr

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ LL | | }
3131
error[E0309]: the parameter type `T` may not live long enough
3232
--> $DIR/projection-one-region-closure.rs:45:29
3333
|
34+
LL | fn no_relationships_late<'a, 'b, T>(cell: Cell<&'a ()>, t: T)
35+
| - help: consider adding an explicit lifetime bound...: `T: 'a`
36+
...
3437
LL | with_signature(cell, t, |cell, t| require(cell, t));
3538
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
36-
|
37-
= help: consider adding an explicit lifetime bound `T: 'a`...
3839

3940
error: lifetime may not live long enough
4041
--> $DIR/projection-one-region-closure.rs:45:39
@@ -81,10 +82,11 @@ LL | | }
8182
error[E0309]: the parameter type `T` may not live long enough
8283
--> $DIR/projection-one-region-closure.rs:56:29
8384
|
85+
LL | fn no_relationships_early<'a, 'b, T>(cell: Cell<&'a ()>, t: T)
86+
| - help: consider adding an explicit lifetime bound...: `T: 'a`
87+
...
8488
LL | with_signature(cell, t, |cell, t| require(cell, t));
8589
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
86-
|
87-
= help: consider adding an explicit lifetime bound `T: 'a`...
8890

8991
error: lifetime may not live long enough
9092
--> $DIR/projection-one-region-closure.rs:56:39

src/test/ui/nll/ty-outlives/projection-where-clause-none.stderr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
error[E0309]: the parameter type `T` may not live long enough
22
--> $DIR/projection-where-clause-none.rs:16:5
33
|
4+
LL | fn foo<'a, T>() -> &'a ()
5+
| - help: consider adding an explicit lifetime bound...: `T: 'a`
6+
...
47
LL | bar::<T::Output>()
58
| ^^^^^^^^^^^^^^^^
6-
|
7-
= help: consider adding an explicit lifetime bound `T: 'a`...
89

910
error: aborting due to previous error
1011

src/test/ui/nll/ty-outlives/ty-param-closure-approximate-lower-bound.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ LL | | }
5252
error[E0309]: the parameter type `T` may not live long enough
5353
--> $DIR/ty-param-closure-approximate-lower-bound.rs:29:24
5454
|
55+
LL | fn generic_fail<'a, T>(cell: Cell<&'a ()>, value: T) {
56+
| - help: consider adding an explicit lifetime bound...: `T: 'a`
5557
LL | twice(cell, value, |a, b| invoke(a, b));
5658
| ^^^^^^^^^^^^^^^^^^^
57-
|
58-
= help: consider adding an explicit lifetime bound `T: 'a`...
5959

6060
error: aborting due to previous error
6161

src/test/ui/nll/ty-outlives/ty-param-closure-outlives-from-return-type.stderr

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,20 @@ LL | | }
2929
error[E0309]: the parameter type `T` may not live long enough
3030
--> $DIR/ty-param-closure-outlives-from-return-type.rs:26:23
3131
|
32+
LL | fn no_region<'a, T>(x: Box<T>) -> Box<dyn Debug + 'a>
33+
| - help: consider adding an explicit lifetime bound...: `T: 'a`
34+
...
3235
LL | with_signature(x, |y| y)
3336
| ^^^^^
34-
|
35-
= help: consider adding an explicit lifetime bound `T: 'a`...
3637

3738
error[E0309]: the parameter type `T` may not live long enough
3839
--> $DIR/ty-param-closure-outlives-from-return-type.rs:41:5
3940
|
41+
LL | fn wrong_region<'a, 'b, T>(x: Box<T>) -> Box<Debug + 'a>
42+
| - help: consider adding an explicit lifetime bound...: `T: 'a`
43+
...
4044
LL | x
4145
| ^
42-
|
43-
= help: consider adding an explicit lifetime bound `T: 'a`...
4446

4547
error: aborting due to 2 previous errors
4648

src/test/ui/nll/ty-outlives/ty-param-closure-outlives-from-where-clause.stderr

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ LL | | }
3737
error[E0309]: the parameter type `T` may not live long enough
3838
--> $DIR/ty-param-closure-outlives-from-where-clause.rs:27:26
3939
|
40+
LL | fn no_region<'a, T>(a: Cell<&'a ()>, b: T) {
41+
| - help: consider adding an explicit lifetime bound...: `T: 'a`
4042
LL | with_signature(a, b, |x, y| {
4143
| __________________________^
4244
LL | |
@@ -46,8 +48,6 @@ LL | | // See `correct_region`, which explains the point of this
4648
LL | | require(&x, &y)
4749
LL | | })
4850
| |_____^
49-
|
50-
= help: consider adding an explicit lifetime bound `T: 'a`...
5151

5252
note: external requirements
5353
--> $DIR/ty-param-closure-outlives-from-where-clause.rs:43:26
@@ -121,15 +121,16 @@ LL | | }
121121
error[E0309]: the parameter type `T` may not live long enough
122122
--> $DIR/ty-param-closure-outlives-from-where-clause.rs:64:26
123123
|
124+
LL | fn wrong_region<'a, 'b, T>(a: Cell<&'a ()>, b: T)
125+
| - help: consider adding an explicit lifetime bound...: `T: 'a`
126+
...
124127
LL | with_signature(a, b, |x, y| {
125128
| __________________________^
126129
LL | |
127130
LL | | // See `correct_region`
128131
LL | | require(&x, &y)
129132
LL | | })
130133
| |_____^
131-
|
132-
= help: consider adding an explicit lifetime bound `T: 'a`...
133134

134135
note: external requirements
135136
--> $DIR/ty-param-closure-outlives-from-where-clause.rs:77:26

src/test/ui/nll/ty-outlives/ty-param-fn-body.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
error[E0309]: the parameter type `T` may not live long enough
22
--> $DIR/ty-param-fn-body.rs:19:5
33
|
4+
LL | fn region_static<'a, T>(cell: Cell<&'a usize>, t: T) {
5+
| - help: consider adding an explicit lifetime bound...: `T: 'a`
46
LL | outlives(cell, t)
57
| ^^^^^^^^^^^^^^^^^
6-
|
7-
= help: consider adding an explicit lifetime bound `T: 'a`...
88

99
error: aborting due to previous error
1010

src/test/ui/nll/ty-outlives/ty-param-fn.stderr

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
error[E0309]: the parameter type `T` may not live long enough
22
--> $DIR/ty-param-fn.rs:11:5
33
|
4+
LL | fn no_region<'a, T>(x: Box<T>) -> Box<Debug + 'a>
5+
| - help: consider adding an explicit lifetime bound...: `T: 'a`
6+
...
47
LL | x
58
| ^
6-
|
7-
= help: consider adding an explicit lifetime bound `T: 'a`...
89

910
error[E0309]: the parameter type `T` may not live long enough
1011
--> $DIR/ty-param-fn.rs:26:5
1112
|
13+
LL | fn wrong_region<'a, 'b, T>(x: Box<T>) -> Box<Debug + 'a>
14+
| - help: consider adding an explicit lifetime bound...: `T: 'a`
15+
...
1216
LL | x
1317
| ^
14-
|
15-
= help: consider adding an explicit lifetime bound `T: 'a`...
1618

1719
error: aborting due to 2 previous errors
1820

src/test/ui/type-alias-impl-trait/generic_type_does_not_live_long_enough.nll.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ LL | type WrongGeneric<T> = impl 'static;
1919
error[E0310]: the parameter type `T` may not live long enough
2020
--> $DIR/generic_type_does_not_live_long_enough.rs:18:5
2121
|
22+
LL | fn wrong_generic<T>(t: T) -> WrongGeneric<T> {
23+
| - help: consider adding an explicit lifetime bound...: `T: 'static`
2224
LL | t
2325
| ^
24-
|
25-
= help: consider adding an explicit lifetime bound `T: 'static`...
2626

2727
error: aborting due to 3 previous errors
2828

0 commit comments

Comments
 (0)