Skip to content

Commit c5313fe

Browse files
committed
Point at multiple outlives requirements instead of just the first one
``` error[E0716]: temporary value dropped while borrowed --> $DIR/multiple-sources-for-outlives-requirement.rs:5:38 | LL | fn foo<'b>() { | -- lifetime `'b` defined here LL | outlives_indir::<'_, 'b, _>(&mut 1u32); | ---------------------------------^^^^-- temporary value is freed at the end of this statement | | | | | creates a temporary value which is freed while still in use | argument requires that borrow lasts for `'b` | note: requirements that the value outlives `'b` introduced here --> $DIR/multiple-sources-for-outlives-requirement.rs:1:23 | LL | fn outlives_indir<'a: 'b, 'b, T: 'a>(_x: T) {} | ^^ ^^ ```
1 parent 58f5260 commit c5313fe

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -417,18 +417,21 @@ impl<'tcx> BorrowExplanation<'tcx> {
417417
self.add_object_lifetime_default_note(tcx, err, unsize_ty);
418418
}
419419

420-
if let Some(pred) = path
420+
let mut preds = path
421421
.iter()
422422
.filter_map(|constraint| match constraint.category {
423423
ConstraintCategory::Predicate(pred) if !pred.is_dummy() => Some(pred),
424424
_ => None,
425425
})
426-
.next()
427-
{
426+
.collect::<Vec<Span>>();
427+
preds.sort();
428+
preds.dedup();
429+
if !preds.is_empty() {
430+
let s = if preds.len() == 1 { "" } else { "s" };
428431
err.span_note(
429-
pred,
432+
preds,
430433
format!(
431-
"requirement that the value outlives `{region_name}` introduced here"
434+
"requirement{s} that the value outlives `{region_name}` introduced here"
432435
),
433436
);
434437
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn outlives_indir<'a: 'b, 'b, T: 'a>(_x: T) {}
2+
//~^ NOTE: requirements that the value outlives `'b` introduced here
3+
4+
fn foo<'b>() { //~ NOTE: lifetime `'b` defined here
5+
outlives_indir::<'_, 'b, _>(&mut 1u32); //~ ERROR: temporary value dropped while borrowed
6+
//~^ NOTE: argument requires that borrow lasts for `'b`
7+
//~| NOTE: creates a temporary value which is freed while still in use
8+
//~| NOTE: temporary value is freed at the end of this statement
9+
}
10+
11+
fn main() {}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0716]: temporary value dropped while borrowed
2+
--> $DIR/multiple-sources-for-outlives-requirement.rs:5:38
3+
|
4+
LL | fn foo<'b>() {
5+
| -- lifetime `'b` defined here
6+
LL | outlives_indir::<'_, 'b, _>(&mut 1u32);
7+
| ---------------------------------^^^^-- temporary value is freed at the end of this statement
8+
| | |
9+
| | creates a temporary value which is freed while still in use
10+
| argument requires that borrow lasts for `'b`
11+
|
12+
note: requirements that the value outlives `'b` introduced here
13+
--> $DIR/multiple-sources-for-outlives-requirement.rs:1:23
14+
|
15+
LL | fn outlives_indir<'a: 'b, 'b, T: 'a>(_x: T) {}
16+
| ^^ ^^
17+
18+
error: aborting due to 1 previous error
19+
20+
For more information about this error, try `rustc --explain E0716`.

0 commit comments

Comments
 (0)