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.
7 use rustc_data_structures::fx::{FxHashSet, FxIndexSet};
8 use rustc_errors::pluralize;
10 use rustc_hir::def::{CtorKind, DefKind, Res};
11 use rustc_hir::def_id::DefId;
12 use rustc_hir::hir_id::HirIdSet;
13 use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
14 use rustc_hir::{Arm, Expr, ExprKind, Guard, HirId, Pat, PatKind};
15 use rustc_middle::middle::region::{self, YieldData};
16 use rustc_middle::ty::{self, Ty, TyCtxt};
17 use rustc_span::symbol::sym;
19 use smallvec::SmallVec;
22 struct InteriorVisitor<'a, 'tcx> {
23 fcx: &'a FnCtxt<'a, 'tcx>,
24 types: FxIndexSet<ty::GeneratorInteriorTypeCause<'tcx>>,
25 region_scope_tree: &'tcx region::ScopeTree,
27 kind: hir::GeneratorKind,
28 prev_unresolved_span: Option<Span>,
29 /// Match arm guards have temporary borrows from the pattern bindings.
30 /// In case there is a yield point in a guard with a reference to such bindings,
31 /// such borrows can span across this yield point.
32 /// As such, we need to track these borrows and record them despite of the fact
33 /// that they may succeed the said yield point in the post-order.
34 guard_bindings: SmallVec<[SmallVec<[HirId; 4]>; 1]>,
35 guard_bindings_set: HirIdSet,
38 impl<'a, 'tcx> InteriorVisitor<'a, 'tcx> {
43 scope: Option<region::Scope>,
44 expr: Option<&'tcx Expr<'tcx>>,
46 guard_borrowing_from_pattern: bool,
48 use rustc_span::DUMMY_SP;
51 "generator_interior: attempting to record type {:?} {:?} {:?} {:?}",
52 ty, scope, expr, source_span
55 let live_across_yield = scope
57 self.region_scope_tree.yield_in_scope(s).and_then(|yield_data| {
58 // If we are recording an expression that is the last yield
59 // in the scope, or that has a postorder CFG index larger
60 // than the one of all of the yields, then its value can't
61 // be storage-live (and therefore live) at any of the yields.
63 // See the mega-comment at `yield_in_scope` for a proof.
66 "comparing counts yield: {} self: {}, source_span = {:?}",
67 yield_data.expr_and_pat_count, self.expr_count, source_span
70 // If it is a borrowing happening in the guard,
71 // it needs to be recorded regardless because they
72 // do live across this yield point.
73 if guard_borrowing_from_pattern
74 || yield_data.expr_and_pat_count >= self.expr_count
83 Some(YieldData { span: DUMMY_SP, expr_and_pat_count: 0, source: self.kind.into() })
86 if let Some(yield_data) = live_across_yield {
87 let ty = self.fcx.resolve_vars_if_possible(ty);
89 "type in expr = {:?}, scope = {:?}, type = {:?}, count = {}, yield_span = {:?}",
90 expr, scope, ty, self.expr_count, yield_data.span
93 if let Some((unresolved_type, unresolved_type_span)) =
94 self.fcx.unresolved_type_vars(&ty)
96 // If unresolved type isn't a ty_var then unresolved_type_span is None
99 .unwrap_or_else(|| unresolved_type_span.unwrap_or(source_span));
101 // If we encounter an int/float variable, then inference fallback didn't
102 // finish due to some other error. Don't emit spurious additional errors.
103 if let ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(_)) =
104 unresolved_type.kind()
109 .delay_span_bug(span, &format!("Encountered var {:?}", unresolved_type));
112 "the type is part of the {} because of this {}",
113 self.kind, yield_data.source
117 .need_type_info_err_in_generator(self.kind, span, unresolved_type)
118 .span_note(yield_data.span, &*note)
122 // Insert the type into the ordered set.
123 let scope_span = scope.map(|s| s.span(self.fcx.tcx, self.region_scope_tree));
125 check_must_not_suspend_ty(
132 yield_span: yield_data.span,
138 self.types.insert(ty::GeneratorInteriorTypeCause {
142 yield_span: yield_data.span,
143 expr: expr.map(|e| e.hir_id),
148 "no type in expr = {:?}, count = {:?}, span = {:?}",
153 let ty = self.fcx.resolve_vars_if_possible(ty);
154 if let Some((unresolved_type, unresolved_type_span)) =
155 self.fcx.unresolved_type_vars(&ty)
158 "remained unresolved_type = {:?}, unresolved_type_span: {:?}",
159 unresolved_type, unresolved_type_span
161 self.prev_unresolved_span = unresolved_type_span;
167 pub fn resolve_interior<'a, 'tcx>(
168 fcx: &'a FnCtxt<'a, 'tcx>,
170 body_id: hir::BodyId,
172 kind: hir::GeneratorKind,
174 let body = fcx.tcx.hir().body(body_id);
175 let mut visitor = InteriorVisitor {
177 types: FxIndexSet::default(),
178 region_scope_tree: fcx.tcx.region_scope_tree(def_id),
181 prev_unresolved_span: None,
182 guard_bindings: <_>::default(),
183 guard_bindings_set: <_>::default(),
185 intravisit::walk_body(&mut visitor, body);
187 // Check that we visited the same amount of expressions and the RegionResolutionVisitor
188 let region_expr_count = visitor.region_scope_tree.body_expr_count(body_id).unwrap();
189 assert_eq!(region_expr_count, visitor.expr_count);
191 // The types are already kept in insertion order.
192 let types = visitor.types;
194 // The types in the generator interior contain lifetimes local to the generator itself,
195 // which should not be exposed outside of the generator. Therefore, we replace these
196 // lifetimes with existentially-bound lifetimes, which reflect the exact value of the
197 // lifetimes not being known by users.
199 // These lifetimes are used in auto trait impl checking (for example,
200 // if a Sync generator contains an &'α T, we need to check whether &'α T: Sync),
201 // so knowledge of the exact relationships between them isn't particularly important.
203 debug!("types in generator {:?}, span = {:?}", types, body.value.span);
206 let mut captured_tys = FxHashSet::default();
207 let type_causes: Vec<_> = types
209 .filter_map(|mut cause| {
210 // Erase regions and canonicalize late-bound regions to deduplicate as many types as we
212 let erased = fcx.tcx.erase_regions(cause.ty);
213 if captured_tys.insert(erased) {
214 // Replace all regions inside the generator interior with late bound regions.
215 // Note that each region slot in the types gets a new fresh late bound region,
216 // which means that none of the regions inside relate to any other, even if
217 // typeck had previously found constraints that would cause them to be related.
218 let folded = fcx.tcx.fold_regions(erased, &mut false, |_, current_depth| {
219 let br = ty::BoundRegion {
220 var: ty::BoundVar::from_u32(counter),
221 kind: ty::BrAnon(counter),
223 let r = fcx.tcx.mk_region(ty::ReLateBound(current_depth, br));
236 // Extract type components to build the witness type.
237 let type_list = fcx.tcx.mk_type_list(type_causes.iter().map(|cause| cause.ty));
238 let bound_vars = fcx.tcx.mk_bound_variable_kinds(
239 (0..counter).map(|i| ty::BoundVariableKind::Region(ty::BrAnon(i))),
242 fcx.tcx.mk_generator_witness(ty::Binder::bind_with_vars(type_list, bound_vars.clone()));
244 // Store the generator types and spans into the typeck results for this generator.
245 visitor.fcx.inh.typeck_results.borrow_mut().generator_interior_types =
246 ty::Binder::bind_with_vars(type_causes, bound_vars);
249 "types in generator after region replacement {:?}, span = {:?}",
250 witness, body.value.span
253 // Unify the type variable inside the generator with the new witness
254 match fcx.at(&fcx.misc(body.value.span), fcx.param_env).eq(interior, witness) {
255 Ok(ok) => fcx.register_infer_ok_obligations(ok),
260 // This visitor has to have the same visit_expr calls as RegionResolutionVisitor in
261 // librustc_middle/middle/region.rs since `expr_count` is compared against the results
263 impl<'a, 'tcx> Visitor<'tcx> for InteriorVisitor<'a, 'tcx> {
264 type Map = intravisit::ErasedMap<'tcx>;
266 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
267 NestedVisitorMap::None
270 fn visit_arm(&mut self, arm: &'tcx Arm<'tcx>) {
271 let Arm { guard, pat, body, .. } = arm;
273 if let Some(ref g) = guard {
274 self.guard_bindings.push(<_>::default());
276 guard_bindings_set: &mut self.guard_bindings_set,
280 .expect("should have pushed at least one earlier"),
285 Guard::If(ref e) => {
288 Guard::IfLet(ref pat, ref e) => {
294 let mut scope_var_ids =
295 self.guard_bindings.pop().expect("should have pushed at least one earlier");
296 for var_id in scope_var_ids.drain(..) {
297 self.guard_bindings_set.remove(&var_id);
300 self.visit_expr(body);
303 fn visit_pat(&mut self, pat: &'tcx Pat<'tcx>) {
304 intravisit::walk_pat(self, pat);
306 self.expr_count += 1;
308 if let PatKind::Binding(..) = pat.kind {
309 let scope = self.region_scope_tree.var_scope(pat.hir_id.local_id);
310 let ty = self.fcx.typeck_results.borrow().pat_ty(pat);
311 self.record(ty, pat.hir_id, Some(scope), None, pat.span, false);
315 fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
316 let mut guard_borrowing_from_pattern = false;
318 ExprKind::Call(callee, args) => match &callee.kind {
319 ExprKind::Path(qpath) => {
320 let res = self.fcx.typeck_results.borrow().qpath_res(qpath, callee.hir_id);
322 // Direct calls never need to keep the callee `ty::FnDef`
323 // ZST in a temporary, so skip its type, just in case it
324 // can significantly complicate the generator type.
326 DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(_, CtorKind::Fn),
329 // NOTE(eddyb) this assumes a path expression has
330 // no nested expressions to keep track of.
331 self.expr_count += 1;
333 // Record the rest of the call expression normally.
335 self.visit_expr(arg);
338 _ => intravisit::walk_expr(self, expr),
341 _ => intravisit::walk_expr(self, expr),
343 ExprKind::Path(qpath) => {
344 intravisit::walk_expr(self, expr);
345 let res = self.fcx.typeck_results.borrow().qpath_res(qpath, expr.hir_id);
347 Res::Local(id) if self.guard_bindings_set.contains(&id) => {
348 guard_borrowing_from_pattern = true;
353 _ => intravisit::walk_expr(self, expr),
356 self.expr_count += 1;
358 let scope = self.region_scope_tree.temporary_scope(expr.hir_id.local_id);
360 // If there are adjustments, then record the final type --
361 // this is the actual value that is being produced.
362 if let Some(adjusted_ty) = self.fcx.typeck_results.borrow().expr_ty_adjusted_opt(expr) {
369 guard_borrowing_from_pattern,
373 // Also record the unadjusted type (which is the only type if
374 // there are no adjustments). The reason for this is that the
375 // unadjusted value is sometimes a "temporary" that would wind
376 // up in a MIR temporary.
378 // As an example, consider an expression like `vec![].push(x)`.
379 // Here, the `vec![]` would wind up MIR stored into a
380 // temporary variable `t` which we can borrow to invoke
381 // `<Vec<_>>::push(&mut t, x)`.
383 // Note that an expression can have many adjustments, and we
384 // are just ignoring those intermediate types. This is because
385 // those intermediate values are always linearly "consumed" by
386 // the other adjustments, and hence would never be directly
387 // captured in the MIR.
389 // (Note that this partly relies on the fact that the `Deref`
390 // traits always return references, which means their content
391 // can be reborrowed without needing to spill to a temporary.
392 // If this were not the case, then we could conceivably have
393 // to create intermediate temporaries.)
395 // The type table might not have information for this expression
396 // if it is in a malformed scope. (#66387)
397 if let Some(ty) = self.fcx.typeck_results.borrow().expr_ty_opt(expr) {
398 if guard_borrowing_from_pattern {
399 // Match guards create references to all the bindings in the pattern that are used
400 // in the guard, e.g. `y if is_even(y) => ...` becomes `is_even(*r_y)` where `r_y`
401 // is a reference to `y`, so we must record a reference to the type of the binding.
402 let tcx = self.fcx.tcx;
403 let ref_ty = tcx.mk_ref(
404 // Use `ReErased` as `resolve_interior` is going to replace all the regions anyway.
405 tcx.mk_region(ty::RegionKind::ReErased),
406 ty::TypeAndMut { ty, mutbl: hir::Mutability::Not },
414 guard_borrowing_from_pattern,
423 guard_borrowing_from_pattern,
426 self.fcx.tcx.sess.delay_span_bug(expr.span, "no type for node");
431 struct ArmPatCollector<'a> {
432 guard_bindings_set: &'a mut HirIdSet,
433 guard_bindings: &'a mut SmallVec<[HirId; 4]>,
436 impl<'a, 'tcx> Visitor<'tcx> for ArmPatCollector<'a> {
437 type Map = intravisit::ErasedMap<'tcx>;
439 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
440 NestedVisitorMap::None
443 fn visit_pat(&mut self, pat: &'tcx Pat<'tcx>) {
444 intravisit::walk_pat(self, pat);
445 if let PatKind::Binding(_, id, ..) = pat.kind {
446 self.guard_bindings.push(id);
447 self.guard_bindings_set.insert(id);
453 pub struct SuspendCheckData<'a, 'tcx> {
454 expr: Option<&'tcx Expr<'tcx>>,
462 // Returns whether it emitted a diagnostic or not
463 // Note that this fn and the proceding one are based on the code
464 // for creating must_use diagnostics
465 pub fn check_must_not_suspend_ty<'tcx>(
466 fcx: &FnCtxt<'_, 'tcx>,
469 data: SuspendCheckData<'_, 'tcx>,
472 // FIXME: should this check `is_ty_uninhabited_from`. This query is not available in this stage
473 // of typeck (before ReVar and RePlaceholder are removed), but may remove noise, like in
475 // || fcx.tcx.is_ty_uninhabited_from(fcx.tcx.parent_module(hir_id).to_def_id(), ty, fcx.param_env)
480 let plural_suffix = pluralize!(data.plural_len);
483 ty::Adt(..) if ty.is_box() => {
484 let boxed_ty = ty.boxed_ty();
485 let descr_pre = &format!("{}boxed ", data.descr_pre);
486 check_must_not_suspend_ty(fcx, boxed_ty, hir_id, SuspendCheckData { descr_pre, ..data })
488 ty::Adt(def, _) => check_must_not_suspend_def(fcx.tcx, def.did, hir_id, data),
489 // FIXME: support adding the attribute to TAITs
490 ty::Opaque(def, _) => {
491 let mut has_emitted = false;
492 for &(predicate, _) in fcx.tcx.explicit_item_bounds(def) {
493 // We only look at the `DefId`, so it is safe to skip the binder here.
494 if let ty::PredicateKind::Trait(ref poly_trait_predicate) =
495 predicate.kind().skip_binder()
497 let def_id = poly_trait_predicate.trait_ref.def_id;
498 let descr_pre = &format!("{}implementer{} of ", data.descr_pre, plural_suffix);
499 if check_must_not_suspend_def(
503 SuspendCheckData { descr_pre, ..data },
512 ty::Dynamic(binder, _) => {
513 let mut has_emitted = false;
514 for predicate in binder.iter() {
515 if let ty::ExistentialPredicate::Trait(ref trait_ref) = predicate.skip_binder() {
516 let def_id = trait_ref.def_id;
517 let descr_post = &format!(" trait object{}{}", plural_suffix, data.descr_post);
518 if check_must_not_suspend_def(
522 SuspendCheckData { descr_post, ..data },
531 ty::Tuple(ref tys) => {
532 let mut has_emitted = false;
533 let spans = if let Some(hir::ExprKind::Tup(comps)) = data.expr.map(|e| &e.kind) {
534 debug_assert_eq!(comps.len(), tys.len());
535 comps.iter().map(|e| e.span).collect()
539 for (i, ty) in tys.iter().map(|k| k.expect_ty()).enumerate() {
540 let descr_post = &format!(" in tuple element {}", i);
541 let span = *spans.get(i).unwrap_or(&data.source_span);
542 if check_must_not_suspend_ty(
546 SuspendCheckData { descr_post, source_span: span, ..data },
553 ty::Array(ty, len) => {
554 let descr_pre = &format!("{}array{} of ", data.descr_pre, plural_suffix);
555 check_must_not_suspend_ty(
561 plural_len: len.try_eval_usize(fcx.tcx, fcx.param_env).unwrap_or(0) as usize
571 fn check_must_not_suspend_def(
575 data: SuspendCheckData<'_, '_>,
577 for attr in tcx.get_attrs(def_id).iter() {
578 if attr.has_name(sym::must_not_suspend) {
579 tcx.struct_span_lint_hir(
580 rustc_session::lint::builtin::MUST_NOT_SUSPEND,
585 "{}`{}`{} held across a suspend point, but should not be",
587 tcx.def_path_str(def_id),
590 let mut err = lint.build(&msg);
592 // add span pointing to the offending yield/await
593 err.span_label(data.yield_span, "the value is held across this suspend point");
595 // Add optional reason note
596 if let Some(note) = attr.value_str() {
597 // FIXME(guswynn): consider formatting this better
598 err.span_note(data.source_span, ¬e.as_str());
601 // Add some quick suggestions on what to do
602 // FIXME: can `drop` work as a suggestion here as well?
605 "consider using a block (`{ ... }`) \
606 to shrink the value's scope, ending before the suspend point",