Skip to content

Commit b052d76

Browse files
committed
Address review comments from #98259
It got merged so fast I didn't have time to make changes xD
1 parent 1deca04 commit b052d76

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`.
@@ -2224,6 +2224,20 @@ impl<'tcx> TyCtxt<'tcx> {
22242224
})
22252225
}
22262226

2227+
/// Given a `ty`, return whether it's an `impl Future<...>`.
2228+
pub fn ty_is_opaque_future(self, ty: Ty<'_>) -> bool {
2229+
let ty::Opaque(def_id, _) = ty.kind() else { return false };
2230+
let future_trait = self.lang_items().future_trait().unwrap();
2231+
2232+
self.explicit_item_bounds(def_id).iter().any(|(predicate, _)| {
2233+
let ty::PredicateKind::Trait(trait_predicate) = predicate.kind().skip_binder() else {
2234+
return false;
2235+
};
2236+
trait_predicate.trait_ref.def_id == future_trait
2237+
&& trait_predicate.polarity == ImplPolarity::Positive
2238+
})
2239+
}
2240+
22272241
/// Computes the def-ids of the transitive supertraits of `trait_def_id`. This (intentionally)
22282242
/// does not compute the full elaborated super-predicates but just the set of def-ids. It is used
22292243
/// 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::{
@@ -2406,19 +2405,6 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
24062405
}
24072406
};
24082407

2409-
let future_trait = self.tcx.lang_items().future_trait().unwrap();
2410-
let opaque_ty_is_future = |def_id| {
2411-
self.tcx.explicit_item_bounds(def_id).iter().any(|(predicate, _)| {
2412-
if let ty::PredicateKind::Trait(trait_predicate) =
2413-
predicate.kind().skip_binder()
2414-
{
2415-
trait_predicate.trait_ref.def_id == future_trait
2416-
} else {
2417-
false
2418-
}
2419-
})
2420-
};
2421-
24222408
let from_generator = tcx.lang_items().from_generator_fn().unwrap();
24232409

24242410
// Don't print the tuple of capture types
@@ -2444,13 +2430,13 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
24442430

24452431
// If the previous type is `from_generator`, this is the future generated by the body of an async function.
24462432
// Avoid printing it twice (it was already printed in the `ty::Generator` arm below).
2447-
let is_future = opaque_ty_is_future(def_id);
2433+
let is_future = tcx.ty_is_opaque_future(ty);
24482434
debug!(
24492435
?obligated_types,
24502436
?is_future,
24512437
"note_obligation_cause_code: check for async fn"
24522438
);
2453-
if opaque_ty_is_future(def_id)
2439+
if is_future
24542440
&& obligated_types.last().map_or(false, |ty| match ty.kind() {
24552441
ty::Opaque(last_def_id, _) => {
24562442
tcx.parent(*last_def_id) == from_generator
@@ -2475,15 +2461,11 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
24752461
}
24762462
err.note(msg.trim_end_matches(", "))
24772463
}
2478-
ty::Generator(def_id, _, movability) => {
2464+
ty::Generator(def_id, _, _) => {
24792465
let sp = self.tcx.def_span(def_id);
24802466

24812467
// Special-case this to say "async block" instead of `[static generator]`.
2482-
let kind = if *movability == Movability::Static {
2483-
"async block"
2484-
} else {
2485-
"generator"
2486-
};
2468+
let kind = tcx.generator_kind(def_id).unwrap();
24872469
err.span_note(
24882470
sp,
24892471
&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)