Skip to content

Commit 326a2bc

Browse files
committed
best_blame_constraint: avoid blaming constraints from MIR generated by desugaring
1 parent ff793a7 commit 326a2bc

File tree

7 files changed

+36
-33
lines changed

7 files changed

+36
-33
lines changed

compiler/rustc_borrowck/src/region_infer/mod.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use rustc_middle::ty::fold::fold_regions;
2222
use rustc_middle::ty::{self, RegionVid, Ty, TyCtxt, TypeFoldable, UniverseIndex};
2323
use rustc_mir_dataflow::points::DenseLocationMap;
2424
use rustc_span::Span;
25+
use rustc_span::hygiene::DesugaringKind;
2526
use tracing::{debug, instrument, trace};
2627

2728
use crate::BorrowckInferCtxt;
@@ -2022,7 +2023,13 @@ impl<'tcx> RegionInferenceContext<'tcx> {
20222023
| ConstraintCategory::BoringNoLocation
20232024
| ConstraintCategory::Internal
20242025
| ConstraintCategory::Predicate(_)
2025-
)
2026+
) && constraint.span.desugaring_kind().is_none_or(|kind| {
2027+
// Try to avoid blaming constraints from desugarings, since they may not clearly
2028+
// clearly match what users have written. As an exception, allow blaming returns
2029+
// generated by `?` desugaring, since the correspondence is fairly clear.
2030+
kind == DesugaringKind::QuestionMark
2031+
&& matches!(constraint.category, ConstraintCategory::Return(_))
2032+
})
20262033
};
20272034

20282035
let best_choice = if blame_source {

tests/ui/async-await/issue-76547.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ impl<'a> Future for ListFut<'a> {
1717
}
1818

1919
async fn fut(bufs: &mut [&mut [u8]]) {
20-
//~^ ERROR lifetime may not live long enough
2120
ListFut(bufs).await
21+
//~^ ERROR lifetime may not live long enough
2222
}
2323

2424
pub struct ListFut2<'a>(&'a mut [&'a mut [u8]]);
@@ -31,8 +31,8 @@ impl<'a> Future for ListFut2<'a> {
3131
}
3232

3333
async fn fut2(bufs: &mut [&mut [u8]]) -> i32 {
34-
//~^ ERROR lifetime may not live long enough
3534
ListFut2(bufs).await
35+
//~^ ERROR lifetime may not live long enough
3636
}
3737

3838
fn main() {}

tests/ui/async-await/issue-76547.stderr

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
error: lifetime may not live long enough
2-
--> $DIR/issue-76547.rs:19:14
2+
--> $DIR/issue-76547.rs:20:13
33
|
44
LL | async fn fut(bufs: &mut [&mut [u8]]) {
5-
| ^^^^ - - let's call the lifetime of this reference `'2`
6-
| | |
7-
| | let's call the lifetime of this reference `'1`
8-
| assignment requires that `'1` must outlive `'2`
5+
| - - let's call the lifetime of this reference `'2`
6+
| |
7+
| let's call the lifetime of this reference `'1`
8+
LL | ListFut(bufs).await
9+
| ^^^^ this usage requires that `'1` must outlive `'2`
910
|
1011
help: consider introducing a named lifetime parameter
1112
|
1213
LL | async fn fut<'a>(bufs: &'a mut [&'a mut [u8]]) {
1314
| ++++ ++ ++
1415

1516
error: lifetime may not live long enough
16-
--> $DIR/issue-76547.rs:33:15
17+
--> $DIR/issue-76547.rs:34:14
1718
|
1819
LL | async fn fut2(bufs: &mut [&mut [u8]]) -> i32 {
19-
| ^^^^ - - let's call the lifetime of this reference `'2`
20-
| | |
21-
| | let's call the lifetime of this reference `'1`
22-
| assignment requires that `'1` must outlive `'2`
20+
| - - let's call the lifetime of this reference `'2`
21+
| |
22+
| let's call the lifetime of this reference `'1`
23+
LL | ListFut2(bufs).await
24+
| ^^^^ this usage requires that `'1` must outlive `'2`
2325
|
2426
help: consider introducing a named lifetime parameter
2527
|

tests/ui/async-await/issues/issue-63388-1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ impl Xyz {
1111
&'a self, foo: &dyn Foo
1212
) -> &dyn Foo //~ WARNING elided lifetime has a name
1313
{
14-
//~^ ERROR explicit lifetime required in the type of `foo` [E0621]
1514
foo
15+
//~^ ERROR explicit lifetime required in the type of `foo` [E0621]
1616
}
1717
}
1818

tests/ui/async-await/issues/issue-63388-1.stderr

+6-9
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@ LL | ) -> &dyn Foo
1010
= note: `#[warn(elided_named_lifetimes)]` on by default
1111

1212
error[E0621]: explicit lifetime required in the type of `foo`
13-
--> $DIR/issue-63388-1.rs:13:5
13+
--> $DIR/issue-63388-1.rs:14:9
1414
|
15-
LL | &'a self, foo: &dyn Foo
16-
| -------- help: add explicit lifetime `'a` to the type of `foo`: `&'a (dyn Foo + 'a)`
17-
LL | ) -> &dyn Foo
18-
LL | / {
19-
LL | |
20-
LL | | foo
21-
LL | | }
22-
| |_____^ lifetime `'a` required
15+
LL | &'a self, foo: &dyn Foo
16+
| -------- help: add explicit lifetime `'a` to the type of `foo`: `&'a (dyn Foo + 'a)`
17+
...
18+
LL | foo
19+
| ^^^ lifetime `'a` required
2320

2421
error: aborting due to 1 previous error; 1 warning emitted
2522

tests/ui/async-await/issues/issue-63388-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ impl Xyz {
1111
foo: &dyn Foo, bar: &'a dyn Foo
1212
) -> &dyn Foo //~ ERROR missing lifetime specifier
1313
{
14-
//~^ ERROR explicit lifetime required in the type of `foo` [E0621]
1514
foo
15+
//~^ ERROR explicit lifetime required in the type of `foo` [E0621]
1616
}
1717
}
1818

tests/ui/async-await/issues/issue-63388-2.stderr

+6-9
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,13 @@ LL | ) -> &'a dyn Foo
1313
| ++
1414

1515
error[E0621]: explicit lifetime required in the type of `foo`
16-
--> $DIR/issue-63388-2.rs:13:5
16+
--> $DIR/issue-63388-2.rs:14:9
1717
|
18-
LL | foo: &dyn Foo, bar: &'a dyn Foo
19-
| -------- help: add explicit lifetime `'a` to the type of `foo`: `&'a (dyn Foo + 'a)`
20-
LL | ) -> &dyn Foo
21-
LL | / {
22-
LL | |
23-
LL | | foo
24-
LL | | }
25-
| |_____^ lifetime `'a` required
18+
LL | foo: &dyn Foo, bar: &'a dyn Foo
19+
| -------- help: add explicit lifetime `'a` to the type of `foo`: `&'a (dyn Foo + 'a)`
20+
...
21+
LL | foo
22+
| ^^^ lifetime `'a` required
2623

2724
error: aborting due to 2 previous errors
2825

0 commit comments

Comments
 (0)