From ef24de24a82fe681581cc130f342363c47c0969a Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 7 Jun 2024 07:48:48 +0200 Subject: Merging upstream version 1.75.0+dfsg1. Signed-off-by: Daniel Baumann --- compiler/rustc_hir_typeck/src/check.rs | 61 +++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 24 deletions(-) (limited to 'compiler/rustc_hir_typeck/src/check.rs') diff --git a/compiler/rustc_hir_typeck/src/check.rs b/compiler/rustc_hir_typeck/src/check.rs index 1fa0ec173..b8a265d49 100644 --- a/compiler/rustc_hir_typeck/src/check.rs +++ b/compiler/rustc_hir_typeck/src/check.rs @@ -2,8 +2,8 @@ use std::cell::RefCell; use crate::coercion::CoerceMany; use crate::gather_locals::GatherLocalsVisitor; +use crate::CoroutineTypes; use crate::FnCtxt; -use crate::GeneratorTypes; use rustc_hir as hir; use rustc_hir::def::DefKind; use rustc_hir::intravisit::Visitor; @@ -31,9 +31,9 @@ pub(super) fn check_fn<'a, 'tcx>( decl: &'tcx hir::FnDecl<'tcx>, fn_def_id: LocalDefId, body: &'tcx hir::Body<'tcx>, - can_be_generator: Option, + can_be_coroutine: Option, params_can_be_unsized: bool, -) -> Option> { +) -> Option> { let fn_id = fcx.tcx.hir().local_def_id_to_hir_id(fn_def_id); let tcx = fcx.tcx; @@ -55,18 +55,23 @@ pub(super) fn check_fn<'a, 'tcx>( fn_maybe_err(tcx, span, fn_sig.abi); - if let Some(kind) = body.generator_kind && can_be_generator.is_some() { - let yield_ty = if kind == hir::GeneratorKind::Gen { - let yield_ty = fcx - .next_ty_var(TypeVariableOrigin { kind: TypeVariableOriginKind::TypeInference, span }); - fcx.require_type_is_sized(yield_ty, span, traits::SizedYieldType); - yield_ty - } else { - Ty::new_unit(tcx,) + if let Some(kind) = body.coroutine_kind + && can_be_coroutine.is_some() + { + let yield_ty = match kind { + hir::CoroutineKind::Gen(..) | hir::CoroutineKind::Coroutine => { + let yield_ty = fcx.next_ty_var(TypeVariableOrigin { + kind: TypeVariableOriginKind::TypeInference, + span, + }); + fcx.require_type_is_sized(yield_ty, span, traits::SizedYieldType); + yield_ty + } + hir::CoroutineKind::Async(..) => Ty::new_unit(tcx), }; - // Resume type defaults to `()` if the generator has no argument. - let resume_ty = fn_sig.inputs().get(0).copied().unwrap_or_else(|| Ty::new_unit(tcx,)); + // Resume type defaults to `()` if the coroutine has no argument. + let resume_ty = fn_sig.inputs().get(0).copied().unwrap_or_else(|| Ty::new_unit(tcx)); fcx.resume_yield_tys = Some((resume_ty, yield_ty)); } @@ -113,28 +118,34 @@ pub(super) fn check_fn<'a, 'tcx>( fcx.typeck_results.borrow_mut().liberated_fn_sigs_mut().insert(fn_id, fn_sig); - fcx.require_type_is_sized(declared_ret_ty, decl.output.span(), traits::SizedReturnType); + let return_or_body_span = match decl.output { + hir::FnRetTy::DefaultReturn(_) => body.value.span, + hir::FnRetTy::Return(ty) => ty.span, + }; + fcx.require_type_is_sized(declared_ret_ty, return_or_body_span, traits::SizedReturnType); fcx.check_return_expr(&body.value, false); - // We insert the deferred_generator_interiors entry after visiting the body. - // This ensures that all nested generators appear before the entry of this generator. - // resolve_generator_interiors relies on this property. - let gen_ty = if let (Some(_), Some(gen_kind)) = (can_be_generator, body.generator_kind) { + // We insert the deferred_coroutine_interiors entry after visiting the body. + // This ensures that all nested coroutines appear before the entry of this coroutine. + // resolve_coroutine_interiors relies on this property. + let coroutine_ty = if let (Some(_), Some(coroutine_kind)) = + (can_be_coroutine, body.coroutine_kind) + { let interior = fcx .next_ty_var(TypeVariableOrigin { kind: TypeVariableOriginKind::MiscVariable, span }); - fcx.deferred_generator_interiors.borrow_mut().push(( + fcx.deferred_coroutine_interiors.borrow_mut().push(( fn_def_id, body.id(), interior, - gen_kind, + coroutine_kind, )); let (resume_ty, yield_ty) = fcx.resume_yield_tys.unwrap(); - Some(GeneratorTypes { + Some(CoroutineTypes { resume_ty, yield_ty, interior, - movability: can_be_generator.unwrap(), + movability: can_be_coroutine.unwrap(), }) } else { None @@ -169,11 +180,13 @@ pub(super) fn check_fn<'a, 'tcx>( check_panic_info_fn(tcx, panic_impl_did.expect_local(), fn_sig); } - if let Some(lang_start_defid) = tcx.lang_items().start_fn() && lang_start_defid == fn_def_id.to_def_id() { + if let Some(lang_start_defid) = tcx.lang_items().start_fn() + && lang_start_defid == fn_def_id.to_def_id() + { check_lang_start_fn(tcx, fn_sig, fn_def_id); } - gen_ty + coroutine_ty } fn check_panic_info_fn(tcx: TyCtxt<'_>, fn_id: LocalDefId, fn_sig: ty::FnSig<'_>) { -- cgit v1.2.3