From a1a48c42b646c0d3171897aa4b8087ec7446ef92 Mon Sep 17 00:00:00 2001 From: Jack Fransham Date: Sun, 29 Nov 2015 14:23:16 +0000 Subject: [PATCH 1/3] Better errors when rustc cannot derive lifetimes --- src/librustc_typeck/astconv.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index 7de262dfa5b..ec1d04dbd93 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -203,11 +203,15 @@ fn report_elision_failure( { let mut m = String::new(); let len = params.len(); + let mut any_lifetimes = false; + for (i, info) in params.into_iter().enumerate() { let ElisionFailureInfo { name, lifetime_count: n, have_bound_regions } = info; + any_lifetimes = any_lifetimes || (n > 0); + let help_name = if name.is_empty() { format!("argument {}", i + 1) } else { @@ -229,7 +233,16 @@ fn report_elision_failure( m.push_str(", "); } } - if len == 1 { + + if !any_lifetimes { + fileline_help!(tcx.sess, default_span, + "this function's return type contains a borrowed value with \ + an elided lifetime, but the lifetime cannot be derived from \ + the arguments"); + fileline_help!(tcx.sess, default_span, + "consider giving it an explicit bounded or 'static \ + lifetime"); + } else if len == 1 { fileline_help!(tcx.sess, default_span, "this function's return type contains a borrowed value, but \ the signature does not say which {} it is borrowed from", From 9569c633bfa639b184f95896f0f1df1711c43298 Mon Sep 17 00:00:00 2001 From: Jack Fransham Date: Sun, 29 Nov 2015 14:29:04 +0000 Subject: [PATCH 2/3] Avoid showing no-derivable-lifetime error message when there are no arguments --- src/librustc_typeck/astconv.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index ec1d04dbd93..d8fc76c76f9 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -234,7 +234,13 @@ fn report_elision_failure( } } - if !any_lifetimes { + if len == 0 { + fileline_help!(tcx.sess, default_span, + "this function's return type contains a borrowed value, but \ + there is no value for it to be borrowed from"); + fileline_help!(tcx.sess, default_span, + "consider giving it a 'static lifetime"); + } else if !any_lifetimes { fileline_help!(tcx.sess, default_span, "this function's return type contains a borrowed value with \ an elided lifetime, but the lifetime cannot be derived from \ @@ -247,12 +253,6 @@ fn report_elision_failure( "this function's return type contains a borrowed value, but \ the signature does not say which {} it is borrowed from", m); - } else if len == 0 { - fileline_help!(tcx.sess, default_span, - "this function's return type contains a borrowed value, but \ - there is no value for it to be borrowed from"); - fileline_help!(tcx.sess, default_span, - "consider giving it a 'static lifetime"); } else { fileline_help!(tcx.sess, default_span, "this function's return type contains a borrowed value, but \ From 829e8bf2a51b56681fc16bd71140668a1c93d9b4 Mon Sep 17 00:00:00 2001 From: Jack Fransham Date: Tue, 1 Dec 2015 14:08:00 +0000 Subject: [PATCH 3/3] Update lifetime compile tests --- src/test/compile-fail/issue-26638.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/test/compile-fail/issue-26638.rs b/src/test/compile-fail/issue-26638.rs index edb9ab47fc6..010803bf25b 100644 --- a/src/test/compile-fail/issue-26638.rs +++ b/src/test/compile-fail/issue-26638.rs @@ -14,6 +14,10 @@ fn parse_type(iter: Box+'static>) -> &str { iter.next() } fn parse_type_2(iter: fn(&u8)->&u8) -> &str { iter() } //~^ ERROR missing lifetime specifier [E0106] -//~^^ HELP 0 elided free lifetimes +//~^^ HELP lifetime cannot be derived + +fn parse_type_3() -> &str { unimplemented!() } +//~^ ERROR missing lifetime specifier [E0106] +//~^^ HELP no value for it to be borrowed from fn main() {}