Skip to content

Commit 14661ae

Browse files
committed
Rollup merge of #33708 - nham:zero-elided-lifetimes, r=sanxiyn
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.
2 parents 355d9f9 + a50c82b commit 14661ae

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

src/librustc_typeck/astconv.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,18 @@ 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+
for (i, info) in elided_params.into_iter().enumerate() {
221226
let ElisionFailureInfo {
222227
name, lifetime_count: n, have_bound_regions
223228
} = info;
224229

225-
any_lifetimes = any_lifetimes || (n > 0);
226-
227230
let help_name = if name.is_empty() {
228231
format!("argument {}", i + 1)
229232
} else {
@@ -237,13 +240,14 @@ fn report_elision_failure(
237240
if have_bound_regions { "free " } else { "" } )
238241
})[..]);
239242

240-
if len == 2 && i == 0 {
243+
if elided_len == 2 && i == 0 {
241244
m.push_str(" or ");
242-
} else if i + 2 == len {
245+
} else if i + 2 == elided_len {
243246
m.push_str(", or ");
244-
} else if i + 1 != len {
247+
} else if i != elided_len - 1 {
245248
m.push_str(", ");
246249
}
250+
247251
}
248252

249253
if len == 0 {
@@ -252,15 +256,15 @@ fn report_elision_failure(
252256
there is no value for it to be borrowed from");
253257
help!(db,
254258
"consider giving it a 'static lifetime");
255-
} else if !any_lifetimes {
259+
} else if elided_len == 0 {
256260
help!(db,
257261
"this function's return type contains a borrowed value with \
258262
an elided lifetime, but the lifetime cannot be derived from \
259263
the arguments");
260264
help!(db,
261265
"consider giving it an explicit bounded or 'static \
262266
lifetime");
263-
} else if len == 1 {
267+
} else if elided_len == 1 {
264268
help!(db,
265269
"this function's return type contains a borrowed value, but \
266270
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)