1 //! This calculates the types which has storage which lives across a suspension point in a
2 //! generator from the perspective of typeck. The actual types used at runtime
3 //! is calculated in `rustc_mir_transform::generator` and may be a subset of the
4 //! types computed here.
6 use self::drop_ranges::DropRanges;
8 use rustc_data_structures::fx::{FxHashSet, FxIndexSet};
9 use rustc_errors::{pluralize, DelayDm};
11 use rustc_hir::def::{CtorKind, DefKind, Res};
12 use rustc_hir::def_id::DefId;
13 use rustc_hir::hir_id::HirIdSet;
14 use rustc_hir::intravisit::{self, Visitor};
15 use rustc_hir::{Arm, Expr, ExprKind, Guard, HirId, Pat, PatKind};
16 use rustc_infer::infer::RegionVariableOrigin;
17 use rustc_middle::middle::region::{self, Scope, ScopeData, YieldData};
18 use rustc_middle::ty::fold::FnMutDelegate;
19 use rustc_middle::ty::{self, BoundVariableKind, RvalueScopes, Ty, TyCtxt, TypeVisitable};
20 use rustc_span::symbol::sym;
22 use smallvec::{smallvec, SmallVec};
26 struct InteriorVisitor<'a, 'tcx> {
27 fcx: &'a FnCtxt<'a, 'tcx>,
28 region_scope_tree: &'a region::ScopeTree,
29 types: FxIndexSet<ty::GeneratorInteriorTypeCause<'tcx>>,
30 rvalue_scopes: &'a RvalueScopes,
32 kind: hir::GeneratorKind,
33 prev_unresolved_span: Option<Span>,
34 linted_values: HirIdSet,
35 drop_ranges: DropRanges,
38 impl<'a, 'tcx> InteriorVisitor<'a, 'tcx> {
43 scope: Option<region::Scope>,
44 expr: Option<&'tcx Expr<'tcx>>,
47 use rustc_span::DUMMY_SP;
49 let ty = self.fcx.resolve_vars_if_possible(ty);
52 "attempting to record type ty={:?}; hir_id={:?}; scope={:?}; expr={:?}; source_span={:?}; expr_count={:?}",
53 ty, hir_id, scope, expr, source_span, self.expr_count,
56 let live_across_yield = scope
58 self.region_scope_tree.yield_in_scope(s).and_then(|yield_data| {
59 // If we are recording an expression that is the last yield
60 // in the scope, or that has a postorder CFG index larger
61 // than the one of all of the yields, then its value can't
62 // be storage-live (and therefore live) at any of the yields.
64 // See the mega-comment at `yield_in_scope` for a proof.
70 "comparing counts yield: {} self: {}, source_span = {:?}",
71 yield_data.expr_and_pat_count, self.expr_count, source_span
74 if self.fcx.sess().opts.unstable_opts.drop_tracking
77 .is_dropped_at(hir_id, yield_data.expr_and_pat_count)
79 debug!("value is dropped at yield point; not recording");
83 // If it is a borrowing happening in the guard,
84 // it needs to be recorded regardless because they
85 // do live across this yield point.
86 yield_data.expr_and_pat_count >= self.expr_count
92 Some(YieldData { span: DUMMY_SP, expr_and_pat_count: 0, source: self.kind.into() })
95 if let Some(yield_data) = live_across_yield {
97 "type in expr = {:?}, scope = {:?}, type = {:?}, count = {}, yield_span = {:?}",
98 expr, scope, ty, self.expr_count, yield_data.span
101 if let Some((unresolved_term, unresolved_type_span)) =
102 self.fcx.first_unresolved_const_or_ty_var(&ty)
104 // If unresolved type isn't a ty_var then unresolved_type_span is None
106 .prev_unresolved_span
107 .unwrap_or_else(|| unresolved_type_span.unwrap_or(source_span));
109 // If we encounter an int/float variable, then inference fallback didn't
110 // finish due to some other error. Don't emit spurious additional errors.
111 if let Some(unresolved_ty) = unresolved_term.ty()
112 && let ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(_)) = unresolved_ty.kind()
117 .delay_span_bug(span, &format!("Encountered var {:?}", unresolved_term));
120 "the type is part of the {} because of this {}",
126 .need_type_info_err_in_generator(self.kind, span, unresolved_term)
127 .span_note(yield_data.span, &*note)
131 // Insert the type into the ordered set.
132 let scope_span = scope.map(|s| s.span(self.fcx.tcx, self.region_scope_tree));
134 if !self.linted_values.contains(&hir_id) {
135 check_must_not_suspend_ty(
142 yield_span: yield_data.span,
147 self.linted_values.insert(hir_id);
150 self.types.insert(ty::GeneratorInteriorTypeCause {
154 yield_span: yield_data.span,
155 expr: expr.map(|e| e.hir_id),
160 "no type in expr = {:?}, count = {:?}, span = {:?}",
165 if let Some((unresolved_type, unresolved_type_span)) =
166 self.fcx.first_unresolved_const_or_ty_var(&ty)
169 "remained unresolved_type = {:?}, unresolved_type_span: {:?}",
170 unresolved_type, unresolved_type_span
172 self.prev_unresolved_span = unresolved_type_span;
178 pub fn resolve_interior<'a, 'tcx>(
179 fcx: &'a FnCtxt<'a, 'tcx>,
181 body_id: hir::BodyId,
183 kind: hir::GeneratorKind,
185 let body = fcx.tcx.hir().body(body_id);
186 let typeck_results = fcx.inh.typeck_results.borrow();
187 let mut visitor = InteriorVisitor {
189 types: FxIndexSet::default(),
190 region_scope_tree: fcx.tcx.region_scope_tree(def_id),
191 rvalue_scopes: &typeck_results.rvalue_scopes,
194 prev_unresolved_span: None,
195 linted_values: <_>::default(),
196 drop_ranges: drop_ranges::compute_drop_ranges(fcx, def_id, body),
198 intravisit::walk_body(&mut visitor, body);
200 // Check that we visited the same amount of expressions as the RegionResolutionVisitor
201 let region_expr_count = fcx.tcx.region_scope_tree(def_id).body_expr_count(body_id).unwrap();
202 assert_eq!(region_expr_count, visitor.expr_count);
204 // The types are already kept in insertion order.
205 let types = visitor.types;
207 // The types in the generator interior contain lifetimes local to the generator itself,
208 // which should not be exposed outside of the generator. Therefore, we replace these
209 // lifetimes with existentially-bound lifetimes, which reflect the exact value of the
210 // lifetimes not being known by users.
212 // These lifetimes are used in auto trait impl checking (for example,
213 // if a Sync generator contains an &'α T, we need to check whether &'α T: Sync),
214 // so knowledge of the exact relationships between them isn't particularly important.
216 debug!("types in generator {:?}, span = {:?}", types, body.value.span);
218 // We want to deduplicate if the lifetimes are the same modulo some non-informative counter.
219 // So, we need to actually do two passes: first by type to anonymize (preserving information
220 // required for diagnostics), then a second pass over all captured types to reassign disjoint
222 let mut captured_tys = FxHashSet::default();
223 let type_causes: Vec<_> = types
225 .filter_map(|mut cause| {
226 // Replace all regions inside the generator interior with late bound regions.
227 // Note that each region slot in the types gets a new fresh late bound region,
228 // which means that none of the regions inside relate to any other, even if
229 // typeck had previously found constraints that would cause them to be related.
232 let mut mk_bound_region = |span| {
233 let kind = ty::BrAnon(counter, span);
234 let var = ty::BoundVar::from_u32(counter);
236 ty::BoundRegion { var, kind }
238 let ty = fcx.normalize(cause.span, cause.ty);
239 let ty = fcx.tcx.fold_regions(ty, |region, current_depth| {
240 let br = match region.kind() {
242 let origin = fcx.region_var_origin(vid);
244 RegionVariableOrigin::EarlyBoundRegion(span, _) => {
245 mk_bound_region(Some(span))
247 _ => mk_bound_region(None),
250 // FIXME: these should use `BrNamed`
251 ty::ReEarlyBound(region) => {
252 mk_bound_region(Some(fcx.tcx.def_span(region.def_id)))
254 ty::ReLateBound(_, ty::BoundRegion { kind, .. })
255 | ty::ReFree(ty::FreeRegion { bound_region: kind, .. }) => match kind {
256 ty::BoundRegionKind::BrAnon(_, span) => mk_bound_region(span),
257 ty::BoundRegionKind::BrNamed(def_id, _) => {
258 mk_bound_region(Some(fcx.tcx.def_span(def_id)))
260 ty::BoundRegionKind::BrEnv => mk_bound_region(None),
262 _ => mk_bound_region(None),
264 let r = fcx.tcx.mk_region(ty::ReLateBound(current_depth, br));
267 if captured_tys.insert(ty) {
276 let mut bound_vars: SmallVec<[BoundVariableKind; 4]> = smallvec![];
278 // Optimization: If there is only one captured type, then we don't actually
279 // need to fold and reindex (since the first type doesn't change).
280 let type_causes = if captured_tys.len() > 0 {
281 // Optimization: Use `replace_escaping_bound_vars_uncached` instead of
282 // `fold_regions`, since we only have late bound regions, and it skips
283 // types without bound regions.
284 fcx.tcx.replace_escaping_bound_vars_uncached(
288 let kind = match br.kind {
289 ty::BrAnon(_, span) => ty::BrAnon(counter, span),
292 let var = ty::BoundVar::from_usize(bound_vars.len());
293 bound_vars.push(ty::BoundVariableKind::Region(kind));
295 fcx.tcx.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BoundRegion { var, kind }))
297 types: &mut |b| bug!("unexpected bound ty in binder: {b:?}"),
298 consts: &mut |b, ty| bug!("unexpected bound ct in binder: {b:?} {ty}"),
305 // Extract type components to build the witness type.
306 let type_list = fcx.tcx.mk_type_list(type_causes.iter().map(|cause| cause.ty));
307 let bound_vars = fcx.tcx.mk_bound_variable_kinds(bound_vars.iter());
309 fcx.tcx.mk_generator_witness(ty::Binder::bind_with_vars(type_list, bound_vars.clone()));
311 drop(typeck_results);
312 // Store the generator types and spans into the typeck results for this generator.
313 fcx.inh.typeck_results.borrow_mut().generator_interior_types =
314 ty::Binder::bind_with_vars(type_causes, bound_vars);
317 "types in generator after region replacement {:?}, span = {:?}",
318 witness, body.value.span
321 // Unify the type variable inside the generator with the new witness
322 match fcx.at(&fcx.misc(body.value.span), fcx.param_env).eq(interior, witness) {
323 Ok(ok) => fcx.register_infer_ok_obligations(ok),
324 _ => bug!("failed to relate {interior} and {witness}"),
328 // This visitor has to have the same visit_expr calls as RegionResolutionVisitor in
329 // librustc_middle/middle/region.rs since `expr_count` is compared against the results
331 impl<'a, 'tcx> Visitor<'tcx> for InteriorVisitor<'a, 'tcx> {
332 fn visit_arm(&mut self, arm: &'tcx Arm<'tcx>) {
333 let Arm { guard, pat, body, .. } = arm;
335 if let Some(ref g) = guard {
337 // If there is a guard, we need to count all variables bound in the pattern as
338 // borrowed for the entire guard body, regardless of whether they are accessed.
339 // We do this by walking the pattern bindings and recording `&T` for any `x: T`
342 struct ArmPatCollector<'a, 'b, 'tcx> {
343 interior_visitor: &'a mut InteriorVisitor<'b, 'tcx>,
347 impl<'a, 'b, 'tcx> Visitor<'tcx> for ArmPatCollector<'a, 'b, 'tcx> {
348 fn visit_pat(&mut self, pat: &'tcx Pat<'tcx>) {
349 intravisit::walk_pat(self, pat);
350 if let PatKind::Binding(_, id, ident, ..) = pat.kind {
352 self.interior_visitor.fcx.typeck_results.borrow().node_type(id);
353 let tcx = self.interior_visitor.fcx.tcx;
355 // Use `ReErased` as `resolve_interior` is going to replace all the
357 tcx.mk_region(ty::ReErased),
358 ty::TypeAndMut { ty, mutbl: hir::Mutability::Not },
360 self.interior_visitor.record(
372 interior_visitor: self,
373 scope: Scope { id: g.body().hir_id.local_id, data: ScopeData::Node },
379 Guard::If(ref e) => {
382 Guard::IfLet(ref l) => {
383 self.visit_let_expr(l);
387 self.visit_expr(body);
390 fn visit_pat(&mut self, pat: &'tcx Pat<'tcx>) {
391 intravisit::walk_pat(self, pat);
393 self.expr_count += 1;
395 if let PatKind::Binding(..) = pat.kind {
396 let scope = self.region_scope_tree.var_scope(pat.hir_id.local_id).unwrap();
397 let ty = self.fcx.typeck_results.borrow().pat_ty(pat);
398 self.record(ty, pat.hir_id, Some(scope), None, pat.span);
402 fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
404 ExprKind::Call(callee, args) => match &callee.kind {
405 ExprKind::Path(qpath) => {
406 let res = self.fcx.typeck_results.borrow().qpath_res(qpath, callee.hir_id);
408 // Direct calls never need to keep the callee `ty::FnDef`
409 // ZST in a temporary, so skip its type, just in case it
410 // can significantly complicate the generator type.
412 DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(_, CtorKind::Fn),
415 // NOTE(eddyb) this assumes a path expression has
416 // no nested expressions to keep track of.
417 self.expr_count += 1;
419 // Record the rest of the call expression normally.
421 self.visit_expr(arg);
424 _ => intravisit::walk_expr(self, expr),
427 _ => intravisit::walk_expr(self, expr),
429 _ => intravisit::walk_expr(self, expr),
432 self.expr_count += 1;
434 debug!("is_borrowed_temporary: {:?}", self.drop_ranges.is_borrowed_temporary(expr));
436 let ty = self.fcx.typeck_results.borrow().expr_ty_adjusted_opt(expr);
438 // Typically, the value produced by an expression is consumed by its parent in some way,
439 // so we only have to check if the parent contains a yield (note that the parent may, for
440 // example, store the value into a local variable, but then we already consider local
441 // variables to be live across their scope).
443 // However, in the case of temporary values, we are going to store the value into a
444 // temporary on the stack that is live for the current temporary scope and then return a
445 // reference to it. That value may be live across the entire temporary scope.
447 // There's another subtlety: if the type has an observable drop, it must be dropped after
448 // the yield, even if it's not borrowed or referenced after the yield. Ideally this would
449 // *only* happen for types with observable drop, not all types which wrap them, but that
450 // doesn't match the behavior of MIR borrowck and causes ICEs. See the FIXME comment in
451 // tests/ui/generator/drop-tracking-parent-expression.rs.
452 let scope = if self.drop_ranges.is_borrowed_temporary(expr)
453 || ty.map_or(true, |ty| {
454 // Avoid ICEs in needs_drop.
455 let ty = self.fcx.resolve_vars_if_possible(ty);
456 let ty = self.fcx.tcx.erase_regions(ty);
457 if ty.needs_infer() {
461 .delay_span_bug(expr.span, &format!("inference variables in {ty}"));
464 ty.needs_drop(self.fcx.tcx, self.fcx.param_env)
467 self.rvalue_scopes.temporary_scope(self.region_scope_tree, expr.hir_id.local_id)
469 let parent_expr = self
473 .parent_iter(expr.hir_id)
474 .find(|(_, node)| matches!(node, hir::Node::Expr(_)))
476 debug!("parent_expr: {:?}", parent_expr);
478 Some(parent) => Some(Scope { id: parent.local_id, data: ScopeData::Node }),
480 self.rvalue_scopes.temporary_scope(self.region_scope_tree, expr.hir_id.local_id)
485 // If there are adjustments, then record the final type --
486 // this is the actual value that is being produced.
487 if let Some(adjusted_ty) = ty {
488 self.record(adjusted_ty, expr.hir_id, scope, Some(expr), expr.span);
491 // Also record the unadjusted type (which is the only type if
492 // there are no adjustments). The reason for this is that the
493 // unadjusted value is sometimes a "temporary" that would wind
494 // up in a MIR temporary.
496 // As an example, consider an expression like `vec![].push(x)`.
497 // Here, the `vec![]` would wind up MIR stored into a
498 // temporary variable `t` which we can borrow to invoke
499 // `<Vec<_>>::push(&mut t, x)`.
501 // Note that an expression can have many adjustments, and we
502 // are just ignoring those intermediate types. This is because
503 // those intermediate values are always linearly "consumed" by
504 // the other adjustments, and hence would never be directly
505 // captured in the MIR.
507 // (Note that this partly relies on the fact that the `Deref`
508 // traits always return references, which means their content
509 // can be reborrowed without needing to spill to a temporary.
510 // If this were not the case, then we could conceivably have
511 // to create intermediate temporaries.)
513 // The type table might not have information for this expression
514 // if it is in a malformed scope. (#66387)
515 if let Some(ty) = self.fcx.typeck_results.borrow().expr_ty_opt(expr) {
516 self.record(ty, expr.hir_id, scope, Some(expr), expr.span);
518 self.fcx.tcx.sess.delay_span_bug(expr.span, "no type for node");
524 struct SuspendCheckData<'a, 'tcx> {
525 expr: Option<&'tcx Expr<'tcx>>,
533 // Returns whether it emitted a diagnostic or not
534 // Note that this fn and the proceeding one are based on the code
535 // for creating must_use diagnostics
537 // Note that this technique was chosen over things like a `Suspend` marker trait
538 // as it is simpler and has precedent in the compiler
539 fn check_must_not_suspend_ty<'tcx>(
540 fcx: &FnCtxt<'_, 'tcx>,
543 data: SuspendCheckData<'_, 'tcx>,
546 // FIXME: should this check `Ty::is_inhabited_from`. This query is not available in this stage
547 // of typeck (before ReVar and RePlaceholder are removed), but may remove noise, like in
549 // || !ty.is_inhabited_from(fcx.tcx, fcx.tcx.parent_module(hir_id).to_def_id(), fcx.param_env)
554 let plural_suffix = pluralize!(data.plural_len);
556 debug!("Checking must_not_suspend for {}", ty);
559 ty::Adt(..) if ty.is_box() => {
560 let boxed_ty = ty.boxed_ty();
561 let descr_pre = &format!("{}boxed ", data.descr_pre);
562 check_must_not_suspend_ty(fcx, boxed_ty, hir_id, SuspendCheckData { descr_pre, ..data })
564 ty::Adt(def, _) => check_must_not_suspend_def(fcx.tcx, def.did(), hir_id, data),
565 // FIXME: support adding the attribute to TAITs
566 ty::Alias(ty::Opaque, ty::AliasTy { def_id: def, .. }) => {
567 let mut has_emitted = false;
568 for &(predicate, _) in fcx.tcx.explicit_item_bounds(def) {
569 // We only look at the `DefId`, so it is safe to skip the binder here.
570 if let ty::PredicateKind::Clause(ty::Clause::Trait(ref poly_trait_predicate)) =
571 predicate.kind().skip_binder()
573 let def_id = poly_trait_predicate.trait_ref.def_id;
574 let descr_pre = &format!("{}implementer{} of ", data.descr_pre, plural_suffix);
575 if check_must_not_suspend_def(
579 SuspendCheckData { descr_pre, ..data },
588 ty::Dynamic(binder, _, _) => {
589 let mut has_emitted = false;
590 for predicate in binder.iter() {
591 if let ty::ExistentialPredicate::Trait(ref trait_ref) = predicate.skip_binder() {
592 let def_id = trait_ref.def_id;
593 let descr_post = &format!(" trait object{}{}", plural_suffix, data.descr_post);
594 if check_must_not_suspend_def(
598 SuspendCheckData { descr_post, ..data },
607 ty::Tuple(fields) => {
608 let mut has_emitted = false;
609 let comps = match data.expr.map(|e| &e.kind) {
610 Some(hir::ExprKind::Tup(comps)) if comps.len() == fields.len() => Some(comps),
613 for (i, ty) in fields.iter().enumerate() {
614 let descr_post = &format!(" in tuple element {i}");
615 let span = comps.and_then(|c| c.get(i)).map(|e| e.span).unwrap_or(data.source_span);
616 if check_must_not_suspend_ty(
622 expr: comps.and_then(|comps| comps.get(i)),
632 ty::Array(ty, len) => {
633 let descr_pre = &format!("{}array{} of ", data.descr_pre, plural_suffix);
634 check_must_not_suspend_ty(
640 plural_len: len.try_eval_usize(fcx.tcx, fcx.param_env).unwrap_or(0) as usize
646 // If drop tracking is enabled, we want to look through references, since the referrent
647 // may not be considered live across the await point.
648 ty::Ref(_region, ty, _mutability) if fcx.sess().opts.unstable_opts.drop_tracking => {
649 let descr_pre = &format!("{}reference{} to ", data.descr_pre, plural_suffix);
650 check_must_not_suspend_ty(fcx, ty, hir_id, SuspendCheckData { descr_pre, ..data })
656 fn check_must_not_suspend_def(
660 data: SuspendCheckData<'_, '_>,
662 if let Some(attr) = tcx.get_attr(def_id, sym::must_not_suspend) {
663 tcx.struct_span_lint_hir(
664 rustc_session::lint::builtin::MUST_NOT_SUSPEND,
669 "{}`{}`{} held across a suspend point, but should not be",
671 tcx.def_path_str(def_id),
676 // add span pointing to the offending yield/await
677 lint.span_label(data.yield_span, "the value is held across this suspend point");
679 // Add optional reason note
680 if let Some(note) = attr.value_str() {
681 // FIXME(guswynn): consider formatting this better
682 lint.span_note(data.source_span, note.as_str());
685 // Add some quick suggestions on what to do
686 // FIXME: can `drop` work as a suggestion here as well?
689 "consider using a block (`{ ... }`) \
690 to shrink the value's scope, ending before the suspend point",