Skip to content

Better errors when rustc cannot derive lifetimes #30102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/librustc_typeck/astconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -229,17 +233,26 @@ fn report_elision_failure(
m.push_str(", ");
}
}
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",
m);
} else if len == 0 {

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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the error message different between this case and the previous one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking the previous message implied that there were no arguments to draw from, and I was hoping to make the message as explicit as possible; I think if they were made the same they would be slightly more confusing. I don't feel strongly on this or anything, I'd be happy to change it so they have the same message if that's the consensus.

Also yeah, I'll do the tests.

"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",
m);
} else {
fileline_help!(tcx.sess, default_span,
"this function's return type contains a borrowed value, but \
Expand Down
6 changes: 5 additions & 1 deletion src/test/compile-fail/issue-26638.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ fn parse_type(iter: Box<Iterator<Item=&str>+'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() {}