Skip to content

Commit 7fef162

Browse files
author
Nick Hamann
committed
Only print parameters with elided lifetimes in elision error messages.
When displaying the function parameters for a lifetime elision error message, this changes it to first filter out the parameters that don't have elided lifetimes. Fixes #30255.
1 parent 0667ae9 commit 7fef162

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

src/librustc_typeck/astconv.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,24 @@ fn report_elision_failure(
215215
{
216216
let mut m = String::new();
217217
let len = params.len();
218-
let mut any_lifetimes = false;
219218

220-
for (i, info) in params.into_iter().enumerate() {
219+
let elided_params: Vec<_> = params.into_iter()
220+
.filter(|info| info.lifetime_count > 0)
221+
.collect();
222+
223+
let elided_len = elided_params.len();
224+
225+
let any_lifetimes = if elided_len > 0 {
226+
true
227+
} else {
228+
false
229+
};
230+
231+
for (i, info) in elided_params.into_iter().enumerate() {
221232
let ElisionFailureInfo {
222233
name, lifetime_count: n, have_bound_regions
223234
} = info;
224235

225-
any_lifetimes = any_lifetimes || (n > 0);
226-
227236
let help_name = if name.is_empty() {
228237
format!("argument {}", i + 1)
229238
} else {
@@ -237,13 +246,14 @@ fn report_elision_failure(
237246
if have_bound_regions { "free " } else { "" } )
238247
})[..]);
239248

240-
if len == 2 && i == 0 {
249+
if elided_len == 2 && i == 0 {
241250
m.push_str(" or ");
242-
} else if i + 2 == len {
251+
} else if i + 2 == elided_len {
243252
m.push_str(", or ");
244-
} else if i + 1 != len {
253+
} else if i != elided_len - 1 {
245254
m.push_str(", ");
246255
}
256+
247257
}
248258

249259
if len == 0 {
@@ -260,7 +270,7 @@ fn report_elision_failure(
260270
help!(db,
261271
"consider giving it an explicit bounded or 'static \
262272
lifetime");
263-
} else if len == 1 {
273+
} else if elided_len == 1 {
264274
help!(db,
265275
"this function's return type contains a borrowed value, but \
266276
the signature does not say which {} it is borrowed from",

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
//
11+
// Test that lifetime elision error messages correctly omit parameters
12+
// with no elided lifetimes
13+
14+
struct S<'a> {
15+
field: &'a i32,
16+
}
17+
18+
fn f(a: &S, b: i32) -> &i32 {
19+
//~^ ERROR missing lifetime specifier [E0106]
20+
//~^^ HELP does not say which one of `a`'s 2 elided lifetimes it is borrowed from
21+
panic!();
22+
}
23+
24+
fn g(a: &S, b: bool, c: &i32) -> &i32 {
25+
//~^ ERROR missing lifetime specifier [E0106]
26+
//~^^ HELP does not say whether it is borrowed from one of `a`'s 2 elided lifetimes or `c`
27+
panic!();
28+
}
29+
30+
fn h(a: &bool, b: bool, c: &S, d: &i32) -> &i32 {
31+
//~^ ERROR missing lifetime specifier [E0106]
32+
//~^^ HELP does not say whether it is borrowed from `a`, one of `c`'s 2 elided lifetimes, or `d`
33+
panic!();
34+
}
35+

0 commit comments

Comments
 (0)