Simplify checking for GeneratorKind::Async

Adds a helper method around `generator_kind` that makes matching async constructs simpler.
This commit is contained in:
Arpad Borsos 2022-11-26 21:33:12 +01:00
parent 8a09420ac4
commit 2db0dc3297
No known key found for this signature in database
GPG Key ID: FC7BCA77824B3298
5 changed files with 10 additions and 22 deletions

View File

@ -514,12 +514,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
span: *span, span: *span,
ty_err: match output_ty.kind() { ty_err: match output_ty.kind() {
ty::Closure(_, _) => FnMutReturnTypeErr::ReturnClosure { span: *span }, ty::Closure(_, _) => FnMutReturnTypeErr::ReturnClosure { span: *span },
ty::Generator(def, ..) ty::Generator(def, ..) if self.infcx.tcx.generator_is_async(*def) => {
if matches!(
self.infcx.tcx.generator_kind(def),
Some(hir::GeneratorKind::Async(_))
) =>
{
FnMutReturnTypeErr::ReturnAsyncBlock { span: *span } FnMutReturnTypeErr::ReturnAsyncBlock { span: *span }
} }
_ => FnMutReturnTypeErr::ReturnRef { span: *span }, _ => FnMutReturnTypeErr::ReturnRef { span: *span },

View File

@ -322,10 +322,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
ty::Closure(..) => Some(MustUsePath::Closure(span)), ty::Closure(..) => Some(MustUsePath::Closure(span)),
ty::Generator(def_id, ..) => { ty::Generator(def_id, ..) => {
// async fn should be treated as "implementor of `Future`" // async fn should be treated as "implementor of `Future`"
let must_use = if matches!( let must_use = if cx.tcx.generator_is_async(def_id) {
cx.tcx.generator_kind(def_id),
Some(hir::GeneratorKind::Async(..))
) {
let def_id = cx.tcx.lang_items().future_trait().unwrap(); let def_id = cx.tcx.lang_items().future_trait().unwrap();
is_def_must_use(cx, def_id, span) is_def_must_use(cx, def_id, span)
.map(|inner| MustUsePath::Opaque(Box::new(inner))) .map(|inner| MustUsePath::Opaque(Box::new(inner)))

View File

@ -1360,6 +1360,11 @@ impl<'tcx> TyCtxt<'tcx> {
self.diagnostic_items(did.krate).name_to_id.get(&name) == Some(&did) self.diagnostic_items(did.krate).name_to_id.get(&name) == Some(&did)
} }
/// Returns `true` if the node pointed to by `def_id` is a generator for an async construct.
pub fn generator_is_async(self, def_id: DefId) -> bool {
matches!(self.generator_kind(def_id), Some(hir::GeneratorKind::Async(_)))
}
pub fn stability(self) -> &'tcx stability::Index { pub fn stability(self) -> &'tcx stability::Index {
self.stability_index(()) self.stability_index(())
} }

View File

@ -1988,11 +1988,6 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
.as_local() .as_local()
.and_then(|def_id| hir.maybe_body_owned_by(def_id)) .and_then(|def_id| hir.maybe_body_owned_by(def_id))
.map(|body_id| hir.body(body_id)); .map(|body_id| hir.body(body_id));
let is_async = self
.tcx
.generator_kind(generator_did)
.map(|generator_kind| matches!(generator_kind, hir::GeneratorKind::Async(..)))
.unwrap_or(false);
let mut visitor = AwaitsVisitor::default(); let mut visitor = AwaitsVisitor::default();
if let Some(body) = generator_body { if let Some(body) = generator_body {
visitor.visit_body(body); visitor.visit_body(body);
@ -2069,6 +2064,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
debug!(?interior_or_upvar_span); debug!(?interior_or_upvar_span);
if let Some(interior_or_upvar_span) = interior_or_upvar_span { if let Some(interior_or_upvar_span) = interior_or_upvar_span {
let is_async = self.tcx.generator_is_async(generator_did);
let typeck_results = match generator_data { let typeck_results = match generator_data {
GeneratorData::Local(typeck_results) => Some(typeck_results), GeneratorData::Local(typeck_results) => Some(typeck_results),
GeneratorData::Foreign(_) => None, GeneratorData::Foreign(_) => None,
@ -2641,10 +2637,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
if is_future if is_future
&& obligated_types.last().map_or(false, |ty| match ty.kind() { && obligated_types.last().map_or(false, |ty| match ty.kind() {
ty::Generator(last_def_id, ..) => { ty::Generator(last_def_id, ..) => {
matches!( tcx.generator_is_async(*last_def_id)
tcx.generator_kind(last_def_id),
Some(GeneratorKind::Async(..))
)
} }
_ => false, _ => false,
}) })

View File

@ -430,9 +430,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
) { ) {
let self_ty = obligation.self_ty().skip_binder(); let self_ty = obligation.self_ty().skip_binder();
if let ty::Generator(did, ..) = self_ty.kind() { if let ty::Generator(did, ..) = self_ty.kind() {
if let Some(rustc_hir::GeneratorKind::Async(_generator_kind)) = if self.tcx().generator_is_async(*did) {
self.tcx().generator_kind(did)
{
debug!(?self_ty, ?obligation, "assemble_future_candidates",); debug!(?self_ty, ?obligation, "assemble_future_candidates",);
candidates.vec.push(FutureCandidate); candidates.vec.push(FutureCandidate);