Skip to content

Commit

Permalink
best_blame_constraint: avoid blaming constraints from MIR generated…
Browse files Browse the repository at this point in the history
… by desugaring
  • Loading branch information
dianne committed Dec 14, 2024
1 parent ff793a7 commit 326a2bc
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 33 deletions.
9 changes: 8 additions & 1 deletion compiler/rustc_borrowck/src/region_infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use rustc_middle::ty::fold::fold_regions;
use rustc_middle::ty::{self, RegionVid, Ty, TyCtxt, TypeFoldable, UniverseIndex};
use rustc_mir_dataflow::points::DenseLocationMap;
use rustc_span::Span;
use rustc_span::hygiene::DesugaringKind;
use tracing::{debug, instrument, trace};

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

let best_choice = if blame_source {
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/async-await/issue-76547.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ impl<'a> Future for ListFut<'a> {
}

async fn fut(bufs: &mut [&mut [u8]]) {
//~^ ERROR lifetime may not live long enough
ListFut(bufs).await
//~^ ERROR lifetime may not live long enough
}

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

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

fn main() {}
22 changes: 12 additions & 10 deletions tests/ui/async-await/issue-76547.stderr
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
error: lifetime may not live long enough
--> $DIR/issue-76547.rs:19:14
--> $DIR/issue-76547.rs:20:13
|
LL | async fn fut(bufs: &mut [&mut [u8]]) {
| ^^^^ - - let's call the lifetime of this reference `'2`
| | |
| | let's call the lifetime of this reference `'1`
| assignment requires that `'1` must outlive `'2`
| - - let's call the lifetime of this reference `'2`
| |
| let's call the lifetime of this reference `'1`
LL | ListFut(bufs).await
| ^^^^ this usage requires that `'1` must outlive `'2`
|
help: consider introducing a named lifetime parameter
|
LL | async fn fut<'a>(bufs: &'a mut [&'a mut [u8]]) {
| ++++ ++ ++

error: lifetime may not live long enough
--> $DIR/issue-76547.rs:33:15
--> $DIR/issue-76547.rs:34:14
|
LL | async fn fut2(bufs: &mut [&mut [u8]]) -> i32 {
| ^^^^ - - let's call the lifetime of this reference `'2`
| | |
| | let's call the lifetime of this reference `'1`
| assignment requires that `'1` must outlive `'2`
| - - let's call the lifetime of this reference `'2`
| |
| let's call the lifetime of this reference `'1`
LL | ListFut2(bufs).await
| ^^^^ this usage requires that `'1` must outlive `'2`
|
help: consider introducing a named lifetime parameter
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/async-await/issues/issue-63388-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ impl Xyz {
&'a self, foo: &dyn Foo
) -> &dyn Foo //~ WARNING elided lifetime has a name
{
//~^ ERROR explicit lifetime required in the type of `foo` [E0621]
foo
//~^ ERROR explicit lifetime required in the type of `foo` [E0621]
}
}

Expand Down
15 changes: 6 additions & 9 deletions tests/ui/async-await/issues/issue-63388-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@ LL | ) -> &dyn Foo
= note: `#[warn(elided_named_lifetimes)]` on by default

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

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

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/async-await/issues/issue-63388-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ impl Xyz {
foo: &dyn Foo, bar: &'a dyn Foo
) -> &dyn Foo //~ ERROR missing lifetime specifier
{
//~^ ERROR explicit lifetime required in the type of `foo` [E0621]
foo
//~^ ERROR explicit lifetime required in the type of `foo` [E0621]
}
}

Expand Down
15 changes: 6 additions & 9 deletions tests/ui/async-await/issues/issue-63388-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@ LL | ) -> &'a dyn Foo
| ++

error[E0621]: explicit lifetime required in the type of `foo`
--> $DIR/issue-63388-2.rs:13:5
--> $DIR/issue-63388-2.rs:14:9
|
LL | foo: &dyn Foo, bar: &'a dyn Foo
| -------- help: add explicit lifetime `'a` to the type of `foo`: `&'a (dyn Foo + 'a)`
LL | ) -> &dyn Foo
LL | / {
LL | |
LL | | foo
LL | | }
| |_____^ lifetime `'a` required
LL | foo: &dyn Foo, bar: &'a dyn Foo
| -------- help: add explicit lifetime `'a` to the type of `foo`: `&'a (dyn Foo + 'a)`
...
LL | foo
| ^^^ lifetime `'a` required

error: aborting due to 2 previous errors

Expand Down

0 comments on commit 326a2bc

Please sign in to comment.