Skip to content

Commit 667a546

Browse files
Rollup merge of #98365 - jyn514:improve-obligation-errors-review-comments, r=eholk
Address review comments from #98259 It got approved so fast I didn't have time to make changes xD r? ``@eholk``
2 parents e749ba2 + b052d76 commit 667a546

File tree

7 files changed

+27
-31
lines changed

7 files changed

+27
-31
lines changed

compiler/rustc_middle/src/ty/context.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ use std::mem;
7474
use std::ops::{Bound, Deref};
7575
use std::sync::Arc;
7676

77-
use super::RvalueScopes;
77+
use super::{ImplPolarity, RvalueScopes};
7878

7979
pub trait OnDiskCache<'tcx>: rustc_data_structures::sync::Sync {
8080
/// Creates a new `OnDiskCache` instance from the serialized data in `data`.
@@ -2230,6 +2230,20 @@ impl<'tcx> TyCtxt<'tcx> {
22302230
})
22312231
}
22322232

2233+
/// Given a `ty`, return whether it's an `impl Future<...>`.
2234+
pub fn ty_is_opaque_future(self, ty: Ty<'_>) -> bool {
2235+
let ty::Opaque(def_id, _) = ty.kind() else { return false };
2236+
let future_trait = self.lang_items().future_trait().unwrap();
2237+
2238+
self.explicit_item_bounds(def_id).iter().any(|(predicate, _)| {
2239+
let ty::PredicateKind::Trait(trait_predicate) = predicate.kind().skip_binder() else {
2240+
return false;
2241+
};
2242+
trait_predicate.trait_ref.def_id == future_trait
2243+
&& trait_predicate.polarity == ImplPolarity::Positive
2244+
})
2245+
}
2246+
22332247
/// Computes the def-ids of the transitive supertraits of `trait_def_id`. This (intentionally)
22342248
/// does not compute the full elaborated super-predicates but just the set of def-ids. It is used
22352249
/// to identify which traits may define a given associated type to help avoid cycle errors.

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+4-22
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use crate::infer::InferCtxt;
88
use crate::traits::normalize_to;
99

1010
use hir::HirId;
11-
use rustc_ast::Movability;
1211
use rustc_data_structures::fx::FxHashSet;
1312
use rustc_data_structures::stack::ensure_sufficient_stack;
1413
use rustc_errors::{
@@ -2395,19 +2394,6 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
23952394
}
23962395
};
23972396

2398-
let future_trait = self.tcx.lang_items().future_trait().unwrap();
2399-
let opaque_ty_is_future = |def_id| {
2400-
self.tcx.explicit_item_bounds(def_id).iter().any(|(predicate, _)| {
2401-
if let ty::PredicateKind::Trait(trait_predicate) =
2402-
predicate.kind().skip_binder()
2403-
{
2404-
trait_predicate.trait_ref.def_id == future_trait
2405-
} else {
2406-
false
2407-
}
2408-
})
2409-
};
2410-
24112397
let from_generator = tcx.lang_items().from_generator_fn().unwrap();
24122398

24132399
// Don't print the tuple of capture types
@@ -2433,13 +2419,13 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
24332419

24342420
// If the previous type is `from_generator`, this is the future generated by the body of an async function.
24352421
// Avoid printing it twice (it was already printed in the `ty::Generator` arm below).
2436-
let is_future = opaque_ty_is_future(def_id);
2422+
let is_future = tcx.ty_is_opaque_future(ty);
24372423
debug!(
24382424
?obligated_types,
24392425
?is_future,
24402426
"note_obligation_cause_code: check for async fn"
24412427
);
2442-
if opaque_ty_is_future(def_id)
2428+
if is_future
24432429
&& obligated_types.last().map_or(false, |ty| match ty.kind() {
24442430
ty::Opaque(last_def_id, _) => {
24452431
tcx.parent(*last_def_id) == from_generator
@@ -2464,15 +2450,11 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
24642450
}
24652451
err.note(msg.trim_end_matches(", "))
24662452
}
2467-
ty::Generator(def_id, _, movability) => {
2453+
ty::Generator(def_id, _, _) => {
24682454
let sp = self.tcx.def_span(def_id);
24692455

24702456
// Special-case this to say "async block" instead of `[static generator]`.
2471-
let kind = if *movability == Movability::Static {
2472-
"async block"
2473-
} else {
2474-
"generator"
2475-
};
2457+
let kind = tcx.generator_kind(def_id).unwrap();
24762458
err.span_note(
24772459
sp,
24782460
&format!("required because it's used within this {}", kind),

src/test/ui/async-await/issue-68112.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ LL | require_send(send_fut);
4242
|
4343
= help: the trait `Sync` is not implemented for `RefCell<i32>`
4444
= note: required because of the requirements on the impl of `Send` for `Arc<RefCell<i32>>`
45-
note: required because it's used within this async block
45+
note: required because it's used within this `async fn` body
4646
--> $DIR/issue-68112.rs:47:31
4747
|
4848
LL | async fn ready2<T>(t: T) -> T { t }
@@ -53,7 +53,7 @@ note: required because it appears within the type `impl Future<Output = Arc<RefC
5353
LL | fn make_non_send_future2() -> impl Future<Output = Arc<RefCell<i32>>> {
5454
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5555
= note: required because it captures the following types: `ResumeTy`, `impl Future<Output = Arc<RefCell<i32>>>`, `()`, `i32`, `Ready<i32>`
56-
note: required because it's used within this async block
56+
note: required because it's used within this `async` block
5757
--> $DIR/issue-68112.rs:55:26
5858
|
5959
LL | let send_fut = async {

src/test/ui/async-await/issue-70935-complex-spans.drop_tracking.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ LL | baz(|| async{
1414
LL | | foo(tx.clone());
1515
LL | | }).await;
1616
| |_________^
17-
note: required because it's used within this async block
17+
note: required because it's used within this `async fn` body
1818
--> $DIR/issue-70935-complex-spans.rs:9:67
1919
|
2020
LL | async fn baz<T>(_c: impl FnMut() -> T) where T: Future<Output=()> {
@@ -23,7 +23,7 @@ LL | |
2323
LL | | }
2424
| |_^
2525
= note: required because it captures the following types: `ResumeTy`, `impl Future<Output = ()>`, `()`
26-
note: required because it's used within this async block
26+
note: required because it's used within this `async` block
2727
--> $DIR/issue-70935-complex-spans.rs:23:16
2828
|
2929
LL | async move {

src/test/ui/async-await/issue-70935-complex-spans.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use std::future::Future;
88

99
async fn baz<T>(_c: impl FnMut() -> T) where T: Future<Output=()> {
10-
//[drop_tracking]~^ within this async block
10+
//[drop_tracking]~^ within this `async fn` body
1111
}
1212

1313
fn foo(tx: std::sync::mpsc::Sender<i32>) -> impl Future + Send {
@@ -21,7 +21,7 @@ fn foo(tx: std::sync::mpsc::Sender<i32>) -> impl Future + Send {
2121
//[drop_tracking]~| NOTE: in this expansion
2222
//[drop_tracking]~| NOTE: in this expansion
2323
async move {
24-
//[drop_tracking]~^ within this async block
24+
//[drop_tracking]~^ within this `async` block
2525
baz(|| async{ //[drop_tracking]~ NOTE: used within this closure
2626
foo(tx.clone());
2727
}).await;

src/test/ui/async-await/partial-drop-partial-reinit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl Drop for NotSend {
2626
impl !Send for NotSend {}
2727

2828
async fn foo() {
29-
//~^ NOTE used within this async block
29+
//~^ NOTE used within this `async fn` body
3030
//~| NOTE within this `impl Future
3131
let mut x = (NotSend {},);
3232
drop(x.0);

src/test/ui/async-await/partial-drop-partial-reinit.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ LL | async fn foo() {
1212
= help: within `impl Future<Output = ()>`, the trait `Send` is not implemented for `NotSend`
1313
= note: required because it appears within the type `(NotSend,)`
1414
= note: required because it captures the following types: `ResumeTy`, `(NotSend,)`, `impl Future<Output = ()>`, `()`
15-
note: required because it's used within this async block
15+
note: required because it's used within this `async fn` body
1616
--> $DIR/partial-drop-partial-reinit.rs:28:16
1717
|
1818
LL | async fn foo() {

0 commit comments

Comments
 (0)