about summary refs log tree commit diff
diff options
context:
space:
mode:
authordianne <diannes.gm@gmail.com>2024-12-13 08:04:15 -0800
committerdianne <diannes.gm@gmail.com>2025-01-06 16:12:11 -0800
commit31e4d8175a3ef5d10f2319e98a20ca6c81614487 (patch)
tree3240464dadeb4f4e80902bd8e56bd10c701053b8
parent2864906fce4e9252eddb96f2d09e9e2f113a9d4b (diff)
downloadrust-31e4d8175a3ef5d10f2319e98a20ca6c81614487.tar.gz
rust-31e4d8175a3ef5d10f2319e98a20ca6c81614487.zip
`best_blame_constraint`: avoid blaming constraints from MIR generated by desugaring
-rw-r--r--compiler/rustc_borrowck/src/region_infer/mod.rs9
-rw-r--r--tests/ui/async-await/issue-76547.rs4
-rw-r--r--tests/ui/async-await/issue-76547.stderr22
-rw-r--r--tests/ui/async-await/issues/issue-63388-1.rs2
-rw-r--r--tests/ui/async-await/issues/issue-63388-1.stderr15
-rw-r--r--tests/ui/async-await/issues/issue-63388-2.rs2
-rw-r--r--tests/ui/async-await/issues/issue-63388-2.stderr15
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 7ec692d36b1..4ceb2fcd8d7 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;
@@ -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 {
diff --git a/tests/ui/async-await/issue-76547.rs b/tests/ui/async-await/issue-76547.rs
index 24decf9b5f7..30a39c89437 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 73803a14e7a..4d96cce824b 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 a6f499ba94e..acfc64baff9 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 ef74bfe3237..579caa45bc9 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 85718f41121..8bb5cfa4a59 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 e515f227c7e..7e3c0a1227d 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