Skip to content

Commit 0ea9b58

Browse files
committed
Suggest adding lifetime to struct field
1 parent f923476 commit 0ea9b58

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

src/librustc/middle/resolve_lifetime.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -2306,7 +2306,13 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
23062306

23072307
Scope::Root => break None,
23082308

2309-
Scope::Binder { s, .. } => {
2309+
Scope::Binder { s, ref lifetimes, .. } => {
2310+
// collect named lifetimes for suggestions
2311+
for name in lifetimes.keys() {
2312+
if let hir::ParamName::Plain(name) = name {
2313+
lifetime_names.insert(*name);
2314+
}
2315+
}
23102316
late_depth += 1;
23112317
scope = s;
23122318
}
@@ -2323,6 +2329,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
23232329
Elide::Exact(l) => l.shifted(late_depth),
23242330
Elide::Error(ref e) => {
23252331
if let Scope::Binder { ref lifetimes, .. } = s {
2332+
// collect named lifetimes for suggestions
23262333
for name in lifetimes.keys() {
23272334
if let hir::ParamName::Plain(name) = name {
23282335
lifetime_names.insert(*name);

src/test/ui/suggestions/return-without-lifetime.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
struct Thing<'a>(&'a ());
2+
struct Foo<'a>(&usize);
3+
//~^ ERROR missing lifetime specifier
24

35
fn func1<'a>(_arg: &'a Thing) -> &() { unimplemented!() }
46
//~^ ERROR missing lifetime specifier
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
error[E0106]: missing lifetime specifier
2-
--> $DIR/return-without-lifetime.rs:3:34
2+
--> $DIR/return-without-lifetime.rs:2:16
3+
|
4+
LL | struct Foo<'a>(&usize);
5+
| ^ help: consider using the named lifetime: `&'a`
6+
7+
error[E0106]: missing lifetime specifier
8+
--> $DIR/return-without-lifetime.rs:5:34
39
|
410
LL | fn func1<'a>(_arg: &'a Thing) -> &() { unimplemented!() }
511
| ^ help: consider using the named lifetime: `&'a`
612
|
713
= help: this function's return type contains a borrowed value, but the signature does not say which one of `_arg`'s 2 lifetimes it is borrowed from
814

915
error[E0106]: missing lifetime specifier
10-
--> $DIR/return-without-lifetime.rs:5:35
16+
--> $DIR/return-without-lifetime.rs:7:35
1117
|
1218
LL | fn func2<'a>(_arg: &Thing<'a>) -> &() { unimplemented!() }
1319
| ^ help: consider using the named lifetime: `&'a`
1420
|
1521
= help: this function's return type contains a borrowed value, but the signature does not say which one of `_arg`'s 2 lifetimes it is borrowed from
1622

17-
error: aborting due to 2 previous errors
23+
error: aborting due to 3 previous errors
1824

1925
For more information about this error, try `rustc --explain E0106`.

0 commit comments

Comments
 (0)