Skip to content

Commit d75f861

Browse files
committed
Auto merge of #30102 - jFransham:feature/better-lifetime-errors, r=Manishearth
Fixes #30086
2 parents 427514f + 829e8bf commit d75f861

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/librustc_typeck/astconv.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,15 @@ fn report_elision_failure(
203203
{
204204
let mut m = String::new();
205205
let len = params.len();
206+
let mut any_lifetimes = false;
207+
206208
for (i, info) in params.into_iter().enumerate() {
207209
let ElisionFailureInfo {
208210
name, lifetime_count: n, have_bound_regions
209211
} = info;
210212

213+
any_lifetimes = any_lifetimes || (n > 0);
214+
211215
let help_name = if name.is_empty() {
212216
format!("argument {}", i + 1)
213217
} else {
@@ -229,17 +233,26 @@ fn report_elision_failure(
229233
m.push_str(", ");
230234
}
231235
}
232-
if len == 1 {
233-
fileline_help!(tcx.sess, default_span,
234-
"this function's return type contains a borrowed value, but \
235-
the signature does not say which {} it is borrowed from",
236-
m);
237-
} else if len == 0 {
236+
237+
if len == 0 {
238238
fileline_help!(tcx.sess, default_span,
239239
"this function's return type contains a borrowed value, but \
240240
there is no value for it to be borrowed from");
241241
fileline_help!(tcx.sess, default_span,
242242
"consider giving it a 'static lifetime");
243+
} else if !any_lifetimes {
244+
fileline_help!(tcx.sess, default_span,
245+
"this function's return type contains a borrowed value with \
246+
an elided lifetime, but the lifetime cannot be derived from \
247+
the arguments");
248+
fileline_help!(tcx.sess, default_span,
249+
"consider giving it an explicit bounded or 'static \
250+
lifetime");
251+
} else if len == 1 {
252+
fileline_help!(tcx.sess, default_span,
253+
"this function's return type contains a borrowed value, but \
254+
the signature does not say which {} it is borrowed from",
255+
m);
243256
} else {
244257
fileline_help!(tcx.sess, default_span,
245258
"this function's return type contains a borrowed value, but \

src/test/compile-fail/issue-26638.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ fn parse_type(iter: Box<Iterator<Item=&str>+'static>) -> &str { iter.next() }
1414

1515
fn parse_type_2(iter: fn(&u8)->&u8) -> &str { iter() }
1616
//~^ ERROR missing lifetime specifier [E0106]
17-
//~^^ HELP 0 elided free lifetimes
17+
//~^^ HELP lifetime cannot be derived
18+
19+
fn parse_type_3() -> &str { unimplemented!() }
20+
//~^ ERROR missing lifetime specifier [E0106]
21+
//~^^ HELP no value for it to be borrowed from
1822

1923
fn main() {}

0 commit comments

Comments
 (0)