From 326a2bc3c654c3a7428e825d2b021b259abcae1c Mon Sep 17 00:00:00 2001 From: dianne Date: Fri, 13 Dec 2024 08:04:15 -0800 Subject: [PATCH] `best_blame_constraint`: avoid blaming constraints from MIR generated by desugaring --- .../rustc_borrowck/src/region_infer/mod.rs | 9 +++++++- tests/ui/async-await/issue-76547.rs | 4 ++-- tests/ui/async-await/issue-76547.stderr | 22 ++++++++++--------- tests/ui/async-await/issues/issue-63388-1.rs | 2 +- .../async-await/issues/issue-63388-1.stderr | 15 +++++-------- tests/ui/async-await/issues/issue-63388-2.rs | 2 +- .../async-await/issues/issue-63388-2.stderr | 15 +++++-------- 7 files changed, 36 insertions(+), 33 deletions(-) diff --git a/compiler/rustc_borrowck/src/region_infer/mod.rs b/compiler/rustc_borrowck/src/region_infer/mod.rs index 5f5bf323b868a..685ad65292235 100644 --- a/compiler/rustc_borrowck/src/region_infer/mod.rs +++ b/compiler/rustc_borrowck/src/region_infer/mod.rs @@ -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; @@ -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 { diff --git a/tests/ui/async-await/issue-76547.rs b/tests/ui/async-await/issue-76547.rs index 24decf9b5f77e..30a39c8943789 100644 --- a/tests/ui/async-await/issue-76547.rs +++ b/tests/ui/async-await/issue-76547.rs @@ -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() {} diff --git a/tests/ui/async-await/issue-76547.stderr b/tests/ui/async-await/issue-76547.stderr index 73803a14e7a5e..4d96cce824b91 100644 --- a/tests/ui/async-await/issue-76547.stderr +++ b/tests/ui/async-await/issue-76547.stderr @@ -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 | diff --git a/tests/ui/async-await/issues/issue-63388-1.rs b/tests/ui/async-await/issues/issue-63388-1.rs index a6f499ba94e29..acfc64baff97d 100644 --- a/tests/ui/async-await/issues/issue-63388-1.rs +++ b/tests/ui/async-await/issues/issue-63388-1.rs @@ -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] } } diff --git a/tests/ui/async-await/issues/issue-63388-1.stderr b/tests/ui/async-await/issues/issue-63388-1.stderr index ef74bfe32375e..579caa45bc945 100644 --- a/tests/ui/async-await/issues/issue-63388-1.stderr +++ b/tests/ui/async-await/issues/issue-63388-1.stderr @@ -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 diff --git a/tests/ui/async-await/issues/issue-63388-2.rs b/tests/ui/async-await/issues/issue-63388-2.rs index 85718f4112151..8bb5cfa4a594a 100644 --- a/tests/ui/async-await/issues/issue-63388-2.rs +++ b/tests/ui/async-await/issues/issue-63388-2.rs @@ -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] } } diff --git a/tests/ui/async-await/issues/issue-63388-2.stderr b/tests/ui/async-await/issues/issue-63388-2.stderr index e515f227c7ef6..7e3c0a1227de8 100644 --- a/tests/ui/async-await/issues/issue-63388-2.stderr +++ b/tests/ui/async-await/issues/issue-63388-2.stderr @@ -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