Auto merge of #116081 - compiler-errors:closure-captures-sized, r=cjgillot
Check that closure/generator's interior/capture types are sized check that closure upvars and generator interiors are sized. this check is only necessary when `unsized_fn_params` or `unsized_locals` is enabled, so only check if those are active. Fixes #93622 Fixes #61335 Fixes #68543
This commit is contained in:
commit
3050938abd
17 changed files with 296 additions and 4 deletions
|
@ -18,7 +18,7 @@ use rustc_infer::traits::{Obligation, TraitEngineExt as _};
|
|||
use rustc_lint_defs::builtin::REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS;
|
||||
use rustc_middle::hir::nested_filter;
|
||||
use rustc_middle::middle::stability::EvalResult;
|
||||
use rustc_middle::traits::DefiningAnchor;
|
||||
use rustc_middle::traits::{DefiningAnchor, ObligationCauseCode};
|
||||
use rustc_middle::ty::fold::BottomUpFolder;
|
||||
use rustc_middle::ty::layout::{LayoutError, MAX_SIMD_LANES};
|
||||
use rustc_middle::ty::util::{Discr, IntTypeExt};
|
||||
|
@ -1626,6 +1626,25 @@ pub(super) fn check_generator_obligations(tcx: TyCtxt<'_>, def_id: LocalDefId) {
|
|||
let obligation = Obligation::new(tcx, cause.clone(), param_env, *predicate);
|
||||
fulfillment_cx.register_predicate_obligation(&infcx, obligation);
|
||||
}
|
||||
|
||||
if (tcx.features().unsized_locals || tcx.features().unsized_fn_params)
|
||||
&& let Some(generator) = tcx.mir_generator_witnesses(def_id)
|
||||
{
|
||||
for field_ty in generator.field_tys.iter() {
|
||||
fulfillment_cx.register_bound(
|
||||
&infcx,
|
||||
param_env,
|
||||
field_ty.ty,
|
||||
tcx.require_lang_item(hir::LangItem::Sized, Some(field_ty.source_info.span)),
|
||||
ObligationCause::new(
|
||||
field_ty.source_info.span,
|
||||
def_id,
|
||||
ObligationCauseCode::SizedGeneratorInterior(def_id),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let errors = fulfillment_cx.select_all_or_error(&infcx);
|
||||
debug!(?errors);
|
||||
if !errors.is_empty() {
|
||||
|
|
|
@ -518,8 +518,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
self.select_obligations_where_possible(|_| {});
|
||||
|
||||
let mut generators = self.deferred_generator_interiors.borrow_mut();
|
||||
for (_, body_id, interior, kind) in generators.drain(..) {
|
||||
crate::generator_interior::resolve_interior(self, def_id, body_id, interior, kind);
|
||||
for (generator_def_id, body_id, interior, kind) in generators.drain(..) {
|
||||
crate::generator_interior::resolve_interior(
|
||||
self,
|
||||
def_id,
|
||||
generator_def_id,
|
||||
body_id,
|
||||
interior,
|
||||
kind,
|
||||
);
|
||||
self.select_obligations_where_possible(|_| {});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,12 +9,13 @@ use rustc_data_structures::fx::{FxHashSet, FxIndexSet};
|
|||
use rustc_errors::{pluralize, DelayDm};
|
||||
use rustc_hir as hir;
|
||||
use rustc_hir::def::{CtorKind, DefKind, Res};
|
||||
use rustc_hir::def_id::DefId;
|
||||
use rustc_hir::def_id::{DefId, LocalDefId};
|
||||
use rustc_hir::hir_id::HirIdSet;
|
||||
use rustc_hir::intravisit::{self, Visitor};
|
||||
use rustc_hir::{Arm, Expr, ExprKind, Guard, HirId, Pat, PatKind};
|
||||
use rustc_infer::infer::{DefineOpaqueTypes, RegionVariableOrigin};
|
||||
use rustc_middle::middle::region::{self, Scope, ScopeData, YieldData};
|
||||
use rustc_middle::traits::ObligationCauseCode;
|
||||
use rustc_middle::ty::fold::FnMutDelegate;
|
||||
use rustc_middle::ty::{self, BoundVariableKind, RvalueScopes, Ty, TyCtxt, TypeVisitableExt};
|
||||
use rustc_span::symbol::sym;
|
||||
|
@ -188,6 +189,7 @@ impl<'a, 'tcx> InteriorVisitor<'a, 'tcx> {
|
|||
pub fn resolve_interior<'a, 'tcx>(
|
||||
fcx: &'a FnCtxt<'a, 'tcx>,
|
||||
def_id: DefId,
|
||||
generator_def_id: LocalDefId,
|
||||
body_id: hir::BodyId,
|
||||
interior: Ty<'tcx>,
|
||||
kind: hir::GeneratorKind,
|
||||
|
@ -214,6 +216,16 @@ pub fn resolve_interior<'a, 'tcx>(
|
|||
// The types are already kept in insertion order.
|
||||
let types = visitor.types;
|
||||
|
||||
if fcx.tcx.features().unsized_locals || fcx.tcx.features().unsized_fn_params {
|
||||
for interior_ty in &types {
|
||||
fcx.require_type_is_sized(
|
||||
interior_ty.ty,
|
||||
interior_ty.span,
|
||||
ObligationCauseCode::SizedGeneratorInterior(generator_def_id),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// The types in the generator interior contain lifetimes local to the generator itself,
|
||||
// which should not be exposed outside of the generator. Therefore, we replace these
|
||||
// lifetimes with existentially-bound lifetimes, which reflect the exact value of the
|
||||
|
|
|
@ -41,6 +41,7 @@ use rustc_hir::intravisit::{self, Visitor};
|
|||
use rustc_infer::infer::UpvarRegion;
|
||||
use rustc_middle::hir::place::{Place, PlaceBase, PlaceWithHirId, Projection, ProjectionKind};
|
||||
use rustc_middle::mir::FakeReadCause;
|
||||
use rustc_middle::traits::ObligationCauseCode;
|
||||
use rustc_middle::ty::{
|
||||
self, ClosureSizeProfileData, Ty, TyCtxt, TypeckResults, UpvarArgs, UpvarCapture,
|
||||
};
|
||||
|
@ -295,6 +296,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
let final_upvar_tys = self.final_upvar_tys(closure_def_id);
|
||||
debug!(?closure_hir_id, ?args, ?final_upvar_tys);
|
||||
|
||||
if self.tcx.features().unsized_locals || self.tcx.features().unsized_fn_params {
|
||||
for capture in
|
||||
self.typeck_results.borrow().closure_min_captures_flattened(closure_def_id)
|
||||
{
|
||||
if let UpvarCapture::ByValue = capture.info.capture_kind {
|
||||
self.require_type_is_sized(
|
||||
capture.place.ty(),
|
||||
capture.get_path_span(self.tcx),
|
||||
ObligationCauseCode::SizedClosureCapture(closure_def_id),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Build a tuple (U0..Un) of the final upvar types U0..Un
|
||||
// and unify the upvar tuple type in the closure with it:
|
||||
let final_tupled_upvars_type = Ty::new_tup(self.tcx, &final_upvar_tys);
|
||||
|
|
|
@ -299,6 +299,10 @@ pub enum ObligationCauseCode<'tcx> {
|
|||
SizedYieldType,
|
||||
/// Inline asm operand type must be `Sized`.
|
||||
InlineAsmSized,
|
||||
/// Captured closure type must be `Sized`.
|
||||
SizedClosureCapture(LocalDefId),
|
||||
/// Types live across generator yields must be `Sized`.
|
||||
SizedGeneratorInterior(LocalDefId),
|
||||
/// `[expr; N]` requires `type_of(expr): Copy`.
|
||||
RepeatElementCopy {
|
||||
/// If element is a `const fn` we display a help message suggesting to move the
|
||||
|
|
|
@ -3007,6 +3007,24 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
|
|||
ObligationCauseCode::InlineAsmSized => {
|
||||
err.note("all inline asm arguments must have a statically known size");
|
||||
}
|
||||
ObligationCauseCode::SizedClosureCapture(closure_def_id) => {
|
||||
err.note("all values captured by value by a closure must have a statically known size");
|
||||
let hir::ExprKind::Closure(closure) = self.tcx.hir().get_by_def_id(closure_def_id).expect_expr().kind else {
|
||||
bug!("expected closure in SizedClosureCapture obligation");
|
||||
};
|
||||
if let hir::CaptureBy::Value = closure.capture_clause
|
||||
&& let Some(span) = closure.fn_arg_span
|
||||
{
|
||||
err.span_label(span, "this closure captures all values by move");
|
||||
}
|
||||
}
|
||||
ObligationCauseCode::SizedGeneratorInterior(generator_def_id) => {
|
||||
let what = match self.tcx.generator_kind(generator_def_id) {
|
||||
None | Some(hir::GeneratorKind::Gen) => "yield",
|
||||
Some(hir::GeneratorKind::Async(..)) => "await",
|
||||
};
|
||||
err.note(format!("all values live across `{what}` must have a statically known size"));
|
||||
}
|
||||
ObligationCauseCode::ConstPatternStructural => {
|
||||
err.note("constants used for pattern-matching must derive `PartialEq` and `Eq`");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue