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_middle::middle::region::{self, Scope, ScopeData, YieldData};
17 use rustc_middle::ty::{self, RvalueScopes, Ty, TyCtxt, TypeVisitable};
18 use rustc_span::symbol::sym;
23 struct InteriorVisitor<'a, 'tcx> {
24 fcx: &'a FnCtxt<'a, 'tcx>,
25 region_scope_tree: &'a region::ScopeTree,
26 types: FxIndexSet<ty::GeneratorInteriorTypeCause<'tcx>>,
27 rvalue_scopes: &'a RvalueScopes,
29 kind: hir::GeneratorKind,
30 prev_unresolved_span: Option<Span>,
31 linted_values: HirIdSet,
32 drop_ranges: DropRanges,
35 impl<'a, 'tcx> InteriorVisitor<'a, 'tcx> {
40 scope: Option<region::Scope>,
41 expr: Option<&'tcx Expr<'tcx>>,
44 use rustc_span::DUMMY_SP;
46 let ty = self.fcx.resolve_vars_if_possible(ty);
49 "attempting to record type ty={:?}; hir_id={:?}; scope={:?}; expr={:?}; source_span={:?}; expr_count={:?}",
50 ty, hir_id, scope, expr, source_span, self.expr_count,
53 let live_across_yield = scope
55 self.region_scope_tree.yield_in_scope(s).and_then(|yield_data| {
56 // If we are recording an expression that is the last yield
57 // in the scope, or that has a postorder CFG index larger
58 // than the one of all of the yields, then its value can't
59 // be storage-live (and therefore live) at any of the yields.
61 // See the mega-comment at `yield_in_scope` for a proof.
67 "comparing counts yield: {} self: {}, source_span = {:?}",
68 yield_data.expr_and_pat_count, self.expr_count, source_span
71 if self.fcx.sess().opts.unstable_opts.drop_tracking
74 .is_dropped_at(hir_id, yield_data.expr_and_pat_count)
76 debug!("value is dropped at yield point; not recording");
80 // If it is a borrowing happening in the guard,
81 // it needs to be recorded regardless because they
82 // do live across this yield point.
83 yield_data.expr_and_pat_count >= self.expr_count
89 Some(YieldData { span: DUMMY_SP, expr_and_pat_count: 0, source: self.kind.into() })
92 if let Some(yield_data) = live_across_yield {
94 "type in expr = {:?}, scope = {:?}, type = {:?}, count = {}, yield_span = {:?}",
95 expr, scope, ty, self.expr_count, yield_data.span
98 if let Some((unresolved_type, unresolved_type_span)) =
99 self.fcx.unresolved_type_vars(&ty)
101 // If unresolved type isn't a ty_var then unresolved_type_span is None
103 .prev_unresolved_span
104 .unwrap_or_else(|| unresolved_type_span.unwrap_or(source_span));
106 // If we encounter an int/float variable, then inference fallback didn't
107 // finish due to some other error. Don't emit spurious additional errors.
108 if let ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(_)) =
109 unresolved_type.kind()
114 .delay_span_bug(span, &format!("Encountered var {:?}", unresolved_type));
117 "the type is part of the {} because of this {}",
118 self.kind, yield_data.source
122 .need_type_info_err_in_generator(self.kind, span, unresolved_type)
123 .span_note(yield_data.span, &*note)
127 // Insert the type into the ordered set.
128 let scope_span = scope.map(|s| s.span(self.fcx.tcx, self.region_scope_tree));
130 if !self.linted_values.contains(&hir_id) {
131 check_must_not_suspend_ty(
138 yield_span: yield_data.span,
143 self.linted_values.insert(hir_id);
146 self.types.insert(ty::GeneratorInteriorTypeCause {
150 yield_span: yield_data.span,
151 expr: expr.map(|e| e.hir_id),
156 "no type in expr = {:?}, count = {:?}, span = {:?}",
161 if let Some((unresolved_type, unresolved_type_span)) =
162 self.fcx.unresolved_type_vars(&ty)
165 "remained unresolved_type = {:?}, unresolved_type_span: {:?}",
166 unresolved_type, unresolved_type_span
168 self.prev_unresolved_span = unresolved_type_span;
174 pub fn resolve_interior<'a, 'tcx>(
175 fcx: &'a FnCtxt<'a, 'tcx>,
177 body_id: hir::BodyId,
179 kind: hir::GeneratorKind,
181 let body = fcx.tcx.hir().body(body_id);
182 let typeck_results = fcx.inh.typeck_results.borrow();
183 let mut visitor = InteriorVisitor {
185 types: FxIndexSet::default(),
186 region_scope_tree: fcx.tcx.region_scope_tree(def_id),
187 rvalue_scopes: &typeck_results.rvalue_scopes,
190 prev_unresolved_span: None,
191 linted_values: <_>::default(),
192 drop_ranges: drop_ranges::compute_drop_ranges(fcx, def_id, body),
194 intravisit::walk_body(&mut visitor, body);
196 // Check that we visited the same amount of expressions as the RegionResolutionVisitor
197 let region_expr_count = fcx.tcx.region_scope_tree(def_id).body_expr_count(body_id).unwrap();
198 assert_eq!(region_expr_count, visitor.expr_count);
200 // The types are already kept in insertion order.
201 let types = visitor.types;
203 // The types in the generator interior contain lifetimes local to the generator itself,
204 // which should not be exposed outside of the generator. Therefore, we replace these
205 // lifetimes with existentially-bound lifetimes, which reflect the exact value of the
206 // lifetimes not being known by users.
208 // These lifetimes are used in auto trait impl checking (for example,
209 // if a Sync generator contains an &'α T, we need to check whether &'α T: Sync),
210 // so knowledge of the exact relationships between them isn't particularly important.
212 debug!("types in generator {:?}, span = {:?}", types, body.value.span);
215 let mut captured_tys = FxHashSet::default();
216 let type_causes: Vec<_> = types
218 .filter_map(|mut cause| {
219 // Erase regions and canonicalize late-bound regions to deduplicate as many types as we
221 let ty = fcx.normalize_associated_types_in(cause.span, cause.ty);
222 let erased = fcx.tcx.erase_regions(ty);
223 if captured_tys.insert(erased) {
224 // Replace all regions inside the generator interior with late bound regions.
225 // Note that each region slot in the types gets a new fresh late bound region,
226 // which means that none of the regions inside relate to any other, even if
227 // typeck had previously found constraints that would cause them to be related.
228 let folded = fcx.tcx.fold_regions(erased, |_, current_depth| {
229 let br = ty::BoundRegion {
230 var: ty::BoundVar::from_u32(counter),
231 kind: ty::BrAnon(counter),
233 let r = fcx.tcx.mk_region(ty::ReLateBound(current_depth, br));
246 // Extract type components to build the witness type.
247 let type_list = fcx.tcx.mk_type_list(type_causes.iter().map(|cause| cause.ty));
248 let bound_vars = fcx.tcx.mk_bound_variable_kinds(
249 (0..counter).map(|i| ty::BoundVariableKind::Region(ty::BrAnon(i))),
252 fcx.tcx.mk_generator_witness(ty::Binder::bind_with_vars(type_list, bound_vars.clone()));
254 drop(typeck_results);
255 // Store the generator types and spans into the typeck results for this generator.
256 fcx.inh.typeck_results.borrow_mut().generator_interior_types =
257 ty::Binder::bind_with_vars(type_causes, bound_vars);
260 "types in generator after region replacement {:?}, span = {:?}",
261 witness, body.value.span
264 // Unify the type variable inside the generator with the new witness
265 match fcx.at(&fcx.misc(body.value.span), fcx.param_env).eq(interior, witness) {
266 Ok(ok) => fcx.register_infer_ok_obligations(ok),
267 _ => bug!("failed to relate {interior} and {witness}"),
271 // This visitor has to have the same visit_expr calls as RegionResolutionVisitor in
272 // librustc_middle/middle/region.rs since `expr_count` is compared against the results
274 impl<'a, 'tcx> Visitor<'tcx> for InteriorVisitor<'a, 'tcx> {
275 fn visit_arm(&mut self, arm: &'tcx Arm<'tcx>) {
276 let Arm { guard, pat, body, .. } = arm;
278 if let Some(ref g) = guard {
280 // If there is a guard, we need to count all variables bound in the pattern as
281 // borrowed for the entire guard body, regardless of whether they are accessed.
282 // We do this by walking the pattern bindings and recording `&T` for any `x: T`
285 struct ArmPatCollector<'a, 'b, 'tcx> {
286 interior_visitor: &'a mut InteriorVisitor<'b, 'tcx>,
290 impl<'a, 'b, 'tcx> Visitor<'tcx> for ArmPatCollector<'a, 'b, 'tcx> {
291 fn visit_pat(&mut self, pat: &'tcx Pat<'tcx>) {
292 intravisit::walk_pat(self, pat);
293 if let PatKind::Binding(_, id, ident, ..) = pat.kind {
295 self.interior_visitor.fcx.typeck_results.borrow().node_type(id);
296 let tcx = self.interior_visitor.fcx.tcx;
298 // Use `ReErased` as `resolve_interior` is going to replace all the
300 tcx.mk_region(ty::ReErased),
301 ty::TypeAndMut { ty, mutbl: hir::Mutability::Not },
303 self.interior_visitor.record(
315 interior_visitor: self,
316 scope: Scope { id: g.body().hir_id.local_id, data: ScopeData::Node },
322 Guard::If(ref e) => {
325 Guard::IfLet(ref l) => {
326 self.visit_let_expr(l);
330 self.visit_expr(body);
333 fn visit_pat(&mut self, pat: &'tcx Pat<'tcx>) {
334 intravisit::walk_pat(self, pat);
336 self.expr_count += 1;
338 if let PatKind::Binding(..) = pat.kind {
339 let scope = self.region_scope_tree.var_scope(pat.hir_id.local_id).unwrap();
340 let ty = self.fcx.typeck_results.borrow().pat_ty(pat);
341 self.record(ty, pat.hir_id, Some(scope), None, pat.span);
345 fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
347 ExprKind::Call(callee, args) => match &callee.kind {
348 ExprKind::Path(qpath) => {
349 let res = self.fcx.typeck_results.borrow().qpath_res(qpath, callee.hir_id);
351 // Direct calls never need to keep the callee `ty::FnDef`
352 // ZST in a temporary, so skip its type, just in case it
353 // can significantly complicate the generator type.
355 DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(_, CtorKind::Fn),
358 // NOTE(eddyb) this assumes a path expression has
359 // no nested expressions to keep track of.
360 self.expr_count += 1;
362 // Record the rest of the call expression normally.
364 self.visit_expr(arg);
367 _ => intravisit::walk_expr(self, expr),
370 _ => intravisit::walk_expr(self, expr),
372 _ => intravisit::walk_expr(self, expr),
375 self.expr_count += 1;
377 debug!("is_borrowed_temporary: {:?}", self.drop_ranges.is_borrowed_temporary(expr));
379 let ty = self.fcx.typeck_results.borrow().expr_ty_adjusted_opt(expr);
380 let may_need_drop = |ty: Ty<'tcx>| {
381 // Avoid ICEs in needs_drop.
382 let ty = self.fcx.resolve_vars_if_possible(ty);
383 let ty = self.fcx.tcx.erase_regions(ty);
384 if ty.needs_infer() {
387 ty.needs_drop(self.fcx.tcx, self.fcx.param_env)
390 // Typically, the value produced by an expression is consumed by its parent in some way,
391 // so we only have to check if the parent contains a yield (note that the parent may, for
392 // example, store the value into a local variable, but then we already consider local
393 // variables to be live across their scope).
395 // However, in the case of temporary values, we are going to store the value into a
396 // temporary on the stack that is live for the current temporary scope and then return a
397 // reference to it. That value may be live across the entire temporary scope.
399 // There's another subtlety: if the type has an observable drop, it must be dropped after
400 // the yield, even if it's not borrowed or referenced after the yield. Ideally this would
401 // *only* happen for types with observable drop, not all types which wrap them, but that
402 // doesn't match the behavior of MIR borrowck and causes ICEs. See the FIXME comment in
403 // src/test/ui/generator/drop-tracking-parent-expression.rs.
404 let scope = if self.drop_ranges.is_borrowed_temporary(expr)
405 || ty.map_or(true, |ty| {
406 let needs_drop = may_need_drop(ty);
407 debug!(?needs_drop, ?ty);
410 self.rvalue_scopes.temporary_scope(self.region_scope_tree, expr.hir_id.local_id)
412 let parent_expr = self
416 .parent_iter(expr.hir_id)
417 .find(|(_, node)| matches!(node, hir::Node::Expr(_)))
419 debug!("parent_expr: {:?}", parent_expr);
421 Some(parent) => Some(Scope { id: parent.local_id, data: ScopeData::Node }),
423 self.rvalue_scopes.temporary_scope(self.region_scope_tree, expr.hir_id.local_id)
428 // If there are adjustments, then record the final type --
429 // this is the actual value that is being produced.
430 if let Some(adjusted_ty) = ty {
431 self.record(adjusted_ty, expr.hir_id, scope, Some(expr), expr.span);
434 // Also record the unadjusted type (which is the only type if
435 // there are no adjustments). The reason for this is that the
436 // unadjusted value is sometimes a "temporary" that would wind
437 // up in a MIR temporary.
439 // As an example, consider an expression like `vec![].push(x)`.
440 // Here, the `vec![]` would wind up MIR stored into a
441 // temporary variable `t` which we can borrow to invoke
442 // `<Vec<_>>::push(&mut t, x)`.
444 // Note that an expression can have many adjustments, and we
445 // are just ignoring those intermediate types. This is because
446 // those intermediate values are always linearly "consumed" by
447 // the other adjustments, and hence would never be directly
448 // captured in the MIR.
450 // (Note that this partly relies on the fact that the `Deref`
451 // traits always return references, which means their content
452 // can be reborrowed without needing to spill to a temporary.
453 // If this were not the case, then we could conceivably have
454 // to create intermediate temporaries.)
456 // The type table might not have information for this expression
457 // if it is in a malformed scope. (#66387)
458 if let Some(ty) = self.fcx.typeck_results.borrow().expr_ty_opt(expr) {
459 self.record(ty, expr.hir_id, scope, Some(expr), expr.span);
461 self.fcx.tcx.sess.delay_span_bug(expr.span, "no type for node");
467 struct SuspendCheckData<'a, 'tcx> {
468 expr: Option<&'tcx Expr<'tcx>>,
476 // Returns whether it emitted a diagnostic or not
477 // Note that this fn and the proceeding one are based on the code
478 // for creating must_use diagnostics
480 // Note that this technique was chosen over things like a `Suspend` marker trait
481 // as it is simpler and has precedent in the compiler
482 fn check_must_not_suspend_ty<'tcx>(
483 fcx: &FnCtxt<'_, 'tcx>,
486 data: SuspendCheckData<'_, 'tcx>,
489 // FIXME: should this check `is_ty_uninhabited_from`. This query is not available in this stage
490 // of typeck (before ReVar and RePlaceholder are removed), but may remove noise, like in
492 // || fcx.tcx.is_ty_uninhabited_from(fcx.tcx.parent_module(hir_id).to_def_id(), ty, fcx.param_env)
497 let plural_suffix = pluralize!(data.plural_len);
499 debug!("Checking must_not_suspend for {}", ty);
502 ty::Adt(..) if ty.is_box() => {
503 let boxed_ty = ty.boxed_ty();
504 let descr_pre = &format!("{}boxed ", data.descr_pre);
505 check_must_not_suspend_ty(fcx, boxed_ty, hir_id, SuspendCheckData { descr_pre, ..data })
507 ty::Adt(def, _) => check_must_not_suspend_def(fcx.tcx, def.did(), hir_id, data),
508 // FIXME: support adding the attribute to TAITs
509 ty::Opaque(def, _) => {
510 let mut has_emitted = false;
511 for &(predicate, _) in fcx.tcx.explicit_item_bounds(def) {
512 // We only look at the `DefId`, so it is safe to skip the binder here.
513 if let ty::PredicateKind::Trait(ref poly_trait_predicate) =
514 predicate.kind().skip_binder()
516 let def_id = poly_trait_predicate.trait_ref.def_id;
517 let descr_pre = &format!("{}implementer{} of ", data.descr_pre, plural_suffix);
518 if check_must_not_suspend_def(
522 SuspendCheckData { descr_pre, ..data },
531 ty::Dynamic(binder, _, _) => {
532 let mut has_emitted = false;
533 for predicate in binder.iter() {
534 if let ty::ExistentialPredicate::Trait(ref trait_ref) = predicate.skip_binder() {
535 let def_id = trait_ref.def_id;
536 let descr_post = &format!(" trait object{}{}", plural_suffix, data.descr_post);
537 if check_must_not_suspend_def(
541 SuspendCheckData { descr_post, ..data },
550 ty::Tuple(fields) => {
551 let mut has_emitted = false;
552 let comps = match data.expr.map(|e| &e.kind) {
553 Some(hir::ExprKind::Tup(comps)) => {
554 debug_assert_eq!(comps.len(), fields.len());
559 for (i, ty) in fields.iter().enumerate() {
560 let descr_post = &format!(" in tuple element {i}");
561 let span = comps.and_then(|c| c.get(i)).map(|e| e.span).unwrap_or(data.source_span);
562 if check_must_not_suspend_ty(
568 expr: comps.and_then(|comps| comps.get(i)),
578 ty::Array(ty, len) => {
579 let descr_pre = &format!("{}array{} of ", data.descr_pre, plural_suffix);
580 check_must_not_suspend_ty(
586 plural_len: len.try_eval_usize(fcx.tcx, fcx.param_env).unwrap_or(0) as usize
592 // If drop tracking is enabled, we want to look through references, since the referrent
593 // may not be considered live across the await point.
594 ty::Ref(_region, ty, _mutability) if fcx.sess().opts.unstable_opts.drop_tracking => {
595 let descr_pre = &format!("{}reference{} to ", data.descr_pre, plural_suffix);
596 check_must_not_suspend_ty(fcx, ty, hir_id, SuspendCheckData { descr_pre, ..data })
602 fn check_must_not_suspend_def(
606 data: SuspendCheckData<'_, '_>,
608 if let Some(attr) = tcx.get_attr(def_id, sym::must_not_suspend) {
609 tcx.struct_span_lint_hir(
610 rustc_session::lint::builtin::MUST_NOT_SUSPEND,
615 "{}`{}`{} held across a suspend point, but should not be",
617 tcx.def_path_str(def_id),
622 // add span pointing to the offending yield/await
623 lint.span_label(data.yield_span, "the value is held across this suspend point");
625 // Add optional reason note
626 if let Some(note) = attr.value_str() {
627 // FIXME(guswynn): consider formatting this better
628 lint.span_note(data.source_span, note.as_str());
631 // Add some quick suggestions on what to do
632 // FIXME: can `drop` work as a suggestion here as well?
635 "consider using a block (`{ ... }`) \
636 to shrink the value's scope, ending before the suspend point",