Skip to content

Commit d0bd69a

Browse files
committed
review comments
1 parent e1e52eb commit d0bd69a

9 files changed

+54
-105
lines changed

src/librustc/middle/resolve_lifetime.rs

Lines changed: 42 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,22 +2235,46 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
22352235
};
22362236

22372237
let mut err = report_missing_lifetime_specifiers(self.tcx.sess, span, lifetime_refs.len());
2238+
let mut add_label = true;
22382239

22392240
if let Some(params) = error {
22402241
if lifetime_refs.len() == 1 {
2241-
self.report_elision_failure(&mut err, params, span);
2242+
add_label = add_label && self.report_elision_failure(&mut err, params, span);
22422243
}
22432244
}
2245+
if add_label {
2246+
add_missing_lifetime_specifiers_label(&mut err, span, lifetime_refs.len());
2247+
}
22442248

22452249
err.emit();
22462250
}
22472251

2252+
fn suggest_lifetime(&self, db: &mut DiagnosticBuilder<'_>, span: Span, msg: &str) -> bool {
2253+
match self.tcx.sess.source_map().span_to_snippet(span) {
2254+
Ok(ref snippet) => {
2255+
let (sugg, applicability) = if &snippet[..] == "&" {
2256+
("&'static ".to_owned(), Applicability::MachineApplicable)
2257+
} else if snippet == "'_" {
2258+
("'static".to_owned(), Applicability::MachineApplicable)
2259+
} else {
2260+
(format!("{} + 'static", snippet), Applicability::MaybeIncorrect)
2261+
};
2262+
db.span_suggestion_with_applicability(span, msg, sugg, applicability);
2263+
false
2264+
}
2265+
Err(_) => {
2266+
db.help(msg);
2267+
true
2268+
}
2269+
}
2270+
}
2271+
22482272
fn report_elision_failure(
22492273
&mut self,
22502274
db: &mut DiagnosticBuilder<'_>,
22512275
params: &[ElisionFailureInfo],
22522276
span: Span,
2253-
) {
2277+
) -> bool {
22542278
let mut m = String::new();
22552279
let len = params.len();
22562280

@@ -2305,29 +2329,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
23052329
"this function's return type contains a borrowed value, but \
23062330
there is no value for it to be borrowed from"
23072331
);
2308-
let msg = "consider giving it a 'static lifetime";
2309-
match self.tcx.sess.source_map().span_to_snippet(span) {
2310-
Ok(ref snippet) if snippet == "&" => db.span_suggestion_with_applicability(
2311-
span,
2312-
msg,
2313-
"&'static ".to_owned(),
2314-
Applicability::MachineApplicable,
2315-
),
2316-
Ok(ref snippet)
2317-
if snippet == "'_" => db.span_suggestion_with_applicability(
2318-
span,
2319-
msg,
2320-
"'static".to_owned(),
2321-
Applicability::MachineApplicable,
2322-
),
2323-
Ok(ref snippet) => db.span_suggestion_with_applicability(
2324-
span,
2325-
msg,
2326-
format!("{} + 'static", snippet),
2327-
Applicability::MaybeIncorrect,
2328-
),
2329-
Err(_) => db.help(msg),
2330-
};
2332+
self.suggest_lifetime(db, span, "consider giving it a 'static lifetime")
23312333
} else if elided_len == 0 {
23322334
help!(
23332335
db,
@@ -2336,42 +2338,23 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
23362338
the arguments"
23372339
);
23382340
let msg = "consider giving it an explicit bounded or 'static lifetime";
2339-
match self.tcx.sess.source_map().span_to_snippet(span) {
2340-
Ok(ref snippet) if snippet == "&" => db.span_suggestion_with_applicability(
2341-
span,
2342-
msg,
2343-
"&'static ".to_owned(),
2344-
Applicability::MachineApplicable,
2345-
),
2346-
Ok(ref snippet)
2347-
if snippet == "'_" => db.span_suggestion_with_applicability(
2348-
span,
2349-
msg,
2350-
"'static".to_owned(),
2351-
Applicability::MachineApplicable,
2352-
),
2353-
Ok(ref snippet) => db.span_suggestion_with_applicability(
2354-
span,
2355-
msg,
2356-
format!("{} + 'static", snippet),
2357-
Applicability::MaybeIncorrect,
2358-
),
2359-
Err(_) => db.help(msg),
2360-
};
2341+
self.suggest_lifetime(db, span, msg)
23612342
} else if elided_len == 1 {
23622343
help!(
23632344
db,
23642345
"this function's return type contains a borrowed value, but \
23652346
the signature does not say which {} it is borrowed from",
23662347
m
23672348
);
2349+
true
23682350
} else {
23692351
help!(
23702352
db,
23712353
"this function's return type contains a borrowed value, but \
23722354
the signature does not say whether it is borrowed from {}",
23732355
m
23742356
);
2357+
true
23752358
}
23762359
}
23772360

@@ -2785,26 +2768,28 @@ fn insert_late_bound_lifetimes(
27852768
}
27862769
}
27872770

2788-
pub fn report_missing_lifetime_specifiers(
2771+
fn report_missing_lifetime_specifiers(
27892772
sess: &Session,
27902773
span: Span,
27912774
count: usize,
27922775
) -> DiagnosticBuilder<'_> {
2793-
let mut err = struct_span_err!(
2776+
struct_span_err!(
27942777
sess,
27952778
span,
27962779
E0106,
27972780
"missing lifetime specifier{}",
27982781
if count > 1 { "s" } else { "" }
2799-
);
2782+
)
2783+
}
28002784

2801-
let msg: Cow<'static, str> = if count > 1 {
2802-
format!("expected {} lifetime parameters", count).into()
2785+
fn add_missing_lifetime_specifiers_label(
2786+
err: &mut DiagnosticBuilder<'_>,
2787+
span: Span,
2788+
count: usize,
2789+
) {
2790+
if count > 1 {
2791+
err.span_label(span, format!("expected {} lifetime parameters", count));
28032792
} else {
2804-
"expected lifetime parameter".into()
2793+
err.span_label(span, "expected lifetime parameter");
28052794
};
2806-
2807-
err.span_label(span, msg);
2808-
2809-
err
28102795
}

src/test/ui/associated-types/bound-lifetime-in-binding-only.elision.stderr

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ error[E0106]: missing lifetime specifier
22
--> $DIR/bound-lifetime-in-binding-only.rs:62:23
33
|
44
LL | fn elision<T: Fn() -> &i32>() {
5-
| ^
6-
| |
7-
| expected lifetime parameter
8-
| help: consider giving it a 'static lifetime: `&'static`
5+
| ^ help: consider giving it a 'static lifetime: `&'static`
96
|
107
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
118

src/test/ui/associated-types/bound-lifetime-in-return-only.elision.stderr

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ error[E0106]: missing lifetime specifier
22
--> $DIR/bound-lifetime-in-return-only.rs:44:23
33
|
44
LL | fn elision(_: fn() -> &i32) {
5-
| ^
6-
| |
7-
| expected lifetime parameter
8-
| help: consider giving it a 'static lifetime: `&'static`
5+
| ^ help: consider giving it a 'static lifetime: `&'static`
96
|
107
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
118

src/test/ui/foreign-fn-return-lifetime.stderr

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ error[E0106]: missing lifetime specifier
22
--> $DIR/foreign-fn-return-lifetime.rs:15:19
33
|
44
LL | pub fn f() -> &u8; //~ ERROR missing lifetime specifier
5-
| ^
6-
| |
7-
| expected lifetime parameter
8-
| help: consider giving it a 'static lifetime: `&'static`
5+
| ^ help: consider giving it a 'static lifetime: `&'static`
96
|
107
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
118

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ error[E0106]: missing lifetime specifier
22
--> $DIR/issue-13497.rs:12:5
33
|
44
LL | &str //~ ERROR missing lifetime specifier
5-
| ^
6-
| |
7-
| expected lifetime parameter
8-
| help: consider giving it a 'static lifetime: `&'static`
5+
| ^ help: consider giving it a 'static lifetime: `&'static`
96
|
107
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
118

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,15 @@ error[E0106]: missing lifetime specifier
1010
--> $DIR/issue-26638.rs:14:40
1111
|
1212
LL | fn parse_type_2(iter: fn(&u8)->&u8) -> &str { iter() }
13-
| ^
14-
| |
15-
| expected lifetime parameter
16-
| help: consider giving it an explicit bounded or 'static lifetime: `&'static`
13+
| ^ help: consider giving it an explicit bounded or 'static lifetime: `&'static`
1714
|
1815
= help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments
1916

2017
error[E0106]: missing lifetime specifier
2118
--> $DIR/issue-26638.rs:17:22
2219
|
2320
LL | fn parse_type_3() -> &str { unimplemented!() }
24-
| ^
25-
| |
26-
| expected lifetime parameter
27-
| help: consider giving it a 'static lifetime: `&'static`
21+
| ^ help: consider giving it a 'static lifetime: `&'static`
2822
|
2923
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
3024

src/test/ui/lifetimes/lifetime-elision-return-type-requires-explicit-lifetime.stderr

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ error[E0106]: missing lifetime specifier
22
--> $DIR/lifetime-elision-return-type-requires-explicit-lifetime.rs:12:11
33
|
44
LL | fn f() -> &isize { //~ ERROR missing lifetime specifier
5-
| ^
6-
| |
7-
| expected lifetime parameter
8-
| help: consider giving it a 'static lifetime: `&'static`
5+
| ^ help: consider giving it a 'static lifetime: `&'static`
96
|
107
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
118

@@ -29,32 +26,23 @@ error[E0106]: missing lifetime specifier
2926
--> $DIR/lifetime-elision-return-type-requires-explicit-lifetime.rs:31:20
3027
|
3128
LL | fn i(_x: isize) -> &isize { //~ ERROR missing lifetime specifier
32-
| ^
33-
| |
34-
| expected lifetime parameter
35-
| help: consider giving it an explicit bounded or 'static lifetime: `&'static`
29+
| ^ help: consider giving it an explicit bounded or 'static lifetime: `&'static`
3630
|
3731
= help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments
3832

3933
error[E0106]: missing lifetime specifier
4034
--> $DIR/lifetime-elision-return-type-requires-explicit-lifetime.rs:44:24
4135
|
4236
LL | fn j(_x: StaticStr) -> &isize { //~ ERROR missing lifetime specifier
43-
| ^
44-
| |
45-
| expected lifetime parameter
46-
| help: consider giving it an explicit bounded or 'static lifetime: `&'static`
37+
| ^ help: consider giving it an explicit bounded or 'static lifetime: `&'static`
4738
|
4839
= help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments
4940

5041
error[E0106]: missing lifetime specifier
5142
--> $DIR/lifetime-elision-return-type-requires-explicit-lifetime.rs:50:49
5243
|
5344
LL | fn k<'a, T: WithLifetime<'a>>(_x: T::Output) -> &isize {
54-
| ^
55-
| |
56-
| expected lifetime parameter
57-
| help: consider giving it an explicit bounded or 'static lifetime: `&'static`
45+
| ^ help: consider giving it an explicit bounded or 'static lifetime: `&'static`
5846
|
5947
= help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments
6048

src/test/ui/lifetimes/lifetime-elision-return-type-trait.stderr

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ error[E0106]: missing lifetime specifier
66
--> $DIR/lifetime-elision-return-type-trait.rs:8:44
77
|
88
LL | fn foo() -> impl Future<Item=(), Error=Box<Error>> {
9-
| ^^^^^
10-
| |
11-
| expected lifetime parameter
12-
| help: consider giving it a 'static lifetime: `Error + 'static`
9+
| ^^^^^ help: consider giving it a 'static lifetime: `Error + 'static`
1310
|
1411
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
1512

src/test/ui/underscore-lifetime/underscore-lifetime-binders.stderr

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ error[E0106]: missing lifetime specifier
2626
--> $DIR/underscore-lifetime-binders.rs:24:29
2727
|
2828
LL | fn meh() -> Box<for<'_> Meh<'_>> //~ ERROR cannot be used here
29-
| ^^
30-
| |
31-
| expected lifetime parameter
32-
| help: consider giving it a 'static lifetime: `'static`
29+
| ^^ help: consider giving it a 'static lifetime: `'static`
3330
|
3431
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
3532

0 commit comments

Comments
 (0)