Skip to content

Commit c81605c

Browse files
authored
Rollup merge of #104951 - Swatinem:async-kind, r=compiler-errors
Simplify checking for `GeneratorKind::Async` Adds a helper method around `generator_kind` that makes matching async constructs simpler.
2 parents 8bcb473 + 2db0dc3 commit c81605c

File tree

5 files changed

+10
-22
lines changed

5 files changed

+10
-22
lines changed

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -514,12 +514,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
514514
span: *span,
515515
ty_err: match output_ty.kind() {
516516
ty::Closure(_, _) => FnMutReturnTypeErr::ReturnClosure { span: *span },
517-
ty::Generator(def, ..)
518-
if matches!(
519-
self.infcx.tcx.generator_kind(def),
520-
Some(hir::GeneratorKind::Async(_))
521-
) =>
522-
{
517+
ty::Generator(def, ..) if self.infcx.tcx.generator_is_async(*def) => {
523518
FnMutReturnTypeErr::ReturnAsyncBlock { span: *span }
524519
}
525520
_ => FnMutReturnTypeErr::ReturnRef { span: *span },

compiler/rustc_lint/src/unused.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
322322
ty::Closure(..) => Some(MustUsePath::Closure(span)),
323323
ty::Generator(def_id, ..) => {
324324
// async fn should be treated as "implementor of `Future`"
325-
let must_use = if matches!(
326-
cx.tcx.generator_kind(def_id),
327-
Some(hir::GeneratorKind::Async(..))
328-
) {
325+
let must_use = if cx.tcx.generator_is_async(def_id) {
329326
let def_id = cx.tcx.lang_items().future_trait().unwrap();
330327
is_def_must_use(cx, def_id, span)
331328
.map(|inner| MustUsePath::Opaque(Box::new(inner)))

compiler/rustc_middle/src/ty/context.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,11 @@ impl<'tcx> TyCtxt<'tcx> {
13611361
self.diagnostic_items(did.krate).name_to_id.get(&name) == Some(&did)
13621362
}
13631363

1364+
/// Returns `true` if the node pointed to by `def_id` is a generator for an async construct.
1365+
pub fn generator_is_async(self, def_id: DefId) -> bool {
1366+
matches!(self.generator_kind(def_id), Some(hir::GeneratorKind::Async(_)))
1367+
}
1368+
13641369
pub fn stability(self) -> &'tcx stability::Index {
13651370
self.stability_index(())
13661371
}

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

+2-9
Original file line numberDiff line numberDiff line change
@@ -1988,11 +1988,6 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
19881988
.as_local()
19891989
.and_then(|def_id| hir.maybe_body_owned_by(def_id))
19901990
.map(|body_id| hir.body(body_id));
1991-
let is_async = self
1992-
.tcx
1993-
.generator_kind(generator_did)
1994-
.map(|generator_kind| matches!(generator_kind, hir::GeneratorKind::Async(..)))
1995-
.unwrap_or(false);
19961991
let mut visitor = AwaitsVisitor::default();
19971992
if let Some(body) = generator_body {
19981993
visitor.visit_body(body);
@@ -2069,6 +2064,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
20692064

20702065
debug!(?interior_or_upvar_span);
20712066
if let Some(interior_or_upvar_span) = interior_or_upvar_span {
2067+
let is_async = self.tcx.generator_is_async(generator_did);
20722068
let typeck_results = match generator_data {
20732069
GeneratorData::Local(typeck_results) => Some(typeck_results),
20742070
GeneratorData::Foreign(_) => None,
@@ -2641,10 +2637,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
26412637
if is_future
26422638
&& obligated_types.last().map_or(false, |ty| match ty.kind() {
26432639
ty::Generator(last_def_id, ..) => {
2644-
matches!(
2645-
tcx.generator_kind(last_def_id),
2646-
Some(GeneratorKind::Async(..))
2647-
)
2640+
tcx.generator_is_async(*last_def_id)
26482641
}
26492642
_ => false,
26502643
})

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
430430
) {
431431
let self_ty = obligation.self_ty().skip_binder();
432432
if let ty::Generator(did, ..) = self_ty.kind() {
433-
if let Some(rustc_hir::GeneratorKind::Async(_generator_kind)) =
434-
self.tcx().generator_kind(did)
435-
{
433+
if self.tcx().generator_is_async(*did) {
436434
debug!(?self_ty, ?obligation, "assemble_future_candidates",);
437435

438436
candidates.vec.push(FutureCandidate);

0 commit comments

Comments
 (0)