From a1a48c42b646c0d3171897aa4b8087ec7446ef92 Mon Sep 17 00:00:00 2001 From: Jack Fransham Date: Sun, 29 Nov 2015 14:23:16 +0000 Subject: [PATCH] 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 7de262dfa5b9..ec1d04dbd933 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",