1
Fork 0

best_blame_constraint: avoid blaming constraints from MIR generated by desugaring

This commit is contained in:
dianne 2024-12-13 08:04:15 -08:00
parent 2864906fce
commit 31e4d8175a
7 changed files with 36 additions and 33 deletions

View file

@ -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;
@ -2033,7 +2034,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 {

View file

@ -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]]);
@ -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() {}

View file

@ -1,11 +1,12 @@
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
|
@ -13,13 +14,14 @@ 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
|

View file

@ -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]
}
}

View file

@ -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

View file

@ -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]
}
}

View file

@ -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