]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_hir_typeck/src/generator_interior/mod.rs
Rollup merge of #106886 - dtolnay:fastinstall, r=Mark-Simulacrum
[rust.git] / compiler / rustc_hir_typeck / src / generator_interior / mod.rs
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.
5
6 use self::drop_ranges::DropRanges;
7 use super::FnCtxt;
8 use rustc_data_structures::fx::{FxHashSet, FxIndexSet};
9 use rustc_errors::{pluralize, DelayDm};
10 use rustc_hir as hir;
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;
21 use rustc_span::Span;
22 use smallvec::{smallvec, SmallVec};
23
24 mod drop_ranges;
25
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,
31     expr_count: usize,
32     kind: hir::GeneratorKind,
33     prev_unresolved_span: Option<Span>,
34     linted_values: HirIdSet,
35     drop_ranges: DropRanges,
36 }
37
38 impl<'a, 'tcx> InteriorVisitor<'a, 'tcx> {
39     fn record(
40         &mut self,
41         ty: Ty<'tcx>,
42         hir_id: HirId,
43         scope: Option<region::Scope>,
44         expr: Option<&'tcx Expr<'tcx>>,
45         source_span: Span,
46     ) {
47         use rustc_span::DUMMY_SP;
48
49         let ty = self.fcx.resolve_vars_if_possible(ty);
50
51         debug!(
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,
54         );
55
56         let live_across_yield = scope
57             .map(|s| {
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.
63                     //
64                     // See the mega-comment at `yield_in_scope` for a proof.
65
66                     yield_data
67                         .iter()
68                         .find(|yield_data| {
69                             debug!(
70                                 "comparing counts yield: {} self: {}, source_span = {:?}",
71                                 yield_data.expr_and_pat_count, self.expr_count, source_span
72                             );
73
74                             if self
75                                 .is_dropped_at_yield_location(hir_id, yield_data.expr_and_pat_count)
76                             {
77                                 debug!("value is dropped at yield point; not recording");
78                                 return false;
79                             }
80
81                             // If it is a borrowing happening in the guard,
82                             // it needs to be recorded regardless because they
83                             // do live across this yield point.
84                             yield_data.expr_and_pat_count >= self.expr_count
85                         })
86                         .cloned()
87                 })
88             })
89             .unwrap_or_else(|| {
90                 Some(YieldData { span: DUMMY_SP, expr_and_pat_count: 0, source: self.kind.into() })
91             });
92
93         if let Some(yield_data) = live_across_yield {
94             debug!(
95                 "type in expr = {:?}, scope = {:?}, type = {:?}, count = {}, yield_span = {:?}",
96                 expr, scope, ty, self.expr_count, yield_data.span
97             );
98
99             if let Some((unresolved_term, unresolved_type_span)) =
100                 self.fcx.first_unresolved_const_or_ty_var(&ty)
101             {
102                 // If unresolved type isn't a ty_var then unresolved_type_span is None
103                 let span = self
104                     .prev_unresolved_span
105                     .unwrap_or_else(|| unresolved_type_span.unwrap_or(source_span));
106
107                 // If we encounter an int/float variable, then inference fallback didn't
108                 // finish due to some other error. Don't emit spurious additional errors.
109                 if let Some(unresolved_ty) = unresolved_term.ty()
110                     && let ty::Infer(ty::InferTy::IntVar(_) | ty::InferTy::FloatVar(_)) = unresolved_ty.kind()
111                 {
112                     self.fcx
113                         .tcx
114                         .sess
115                         .delay_span_bug(span, &format!("Encountered var {:?}", unresolved_term));
116                 } else {
117                     let note = format!(
118                         "the type is part of the {} because of this {}",
119                         self.kind.descr(),
120                         yield_data.source
121                     );
122
123                     self.fcx
124                         .need_type_info_err_in_generator(self.kind, span, unresolved_term)
125                         .span_note(yield_data.span, &*note)
126                         .emit();
127                 }
128             } else {
129                 // Insert the type into the ordered set.
130                 let scope_span = scope.map(|s| s.span(self.fcx.tcx, self.region_scope_tree));
131
132                 if !self.linted_values.contains(&hir_id) {
133                     check_must_not_suspend_ty(
134                         self.fcx,
135                         ty,
136                         hir_id,
137                         SuspendCheckData {
138                             expr,
139                             source_span,
140                             yield_span: yield_data.span,
141                             plural_len: 1,
142                             ..Default::default()
143                         },
144                     );
145                     self.linted_values.insert(hir_id);
146                 }
147
148                 self.types.insert(ty::GeneratorInteriorTypeCause {
149                     span: source_span,
150                     ty,
151                     scope_span,
152                     yield_span: yield_data.span,
153                     expr: expr.map(|e| e.hir_id),
154                 });
155             }
156         } else {
157             debug!(
158                 "no type in expr = {:?}, count = {:?}, span = {:?}",
159                 expr,
160                 self.expr_count,
161                 expr.map(|e| e.span)
162             );
163             if let Some((unresolved_type, unresolved_type_span)) =
164                 self.fcx.first_unresolved_const_or_ty_var(&ty)
165             {
166                 debug!(
167                     "remained unresolved_type = {:?}, unresolved_type_span: {:?}",
168                     unresolved_type, unresolved_type_span
169                 );
170                 self.prev_unresolved_span = unresolved_type_span;
171             }
172         }
173     }
174
175     /// If drop tracking is enabled, consult drop_ranges to see if a value is
176     /// known to be dropped at a yield point and therefore can be omitted from
177     /// the generator witness.
178     fn is_dropped_at_yield_location(&self, value_hir_id: HirId, yield_location: usize) -> bool {
179         // short-circuit if drop tracking is not enabled.
180         if !self.fcx.sess().opts.unstable_opts.drop_tracking {
181             return false;
182         }
183
184         self.drop_ranges.is_dropped_at(value_hir_id, yield_location)
185     }
186 }
187
188 pub fn resolve_interior<'a, 'tcx>(
189     fcx: &'a FnCtxt<'a, 'tcx>,
190     def_id: DefId,
191     body_id: hir::BodyId,
192     interior: Ty<'tcx>,
193     kind: hir::GeneratorKind,
194 ) {
195     let body = fcx.tcx.hir().body(body_id);
196     let typeck_results = fcx.inh.typeck_results.borrow();
197     let mut visitor = InteriorVisitor {
198         fcx,
199         types: FxIndexSet::default(),
200         region_scope_tree: fcx.tcx.region_scope_tree(def_id),
201         rvalue_scopes: &typeck_results.rvalue_scopes,
202         expr_count: 0,
203         kind,
204         prev_unresolved_span: None,
205         linted_values: <_>::default(),
206         drop_ranges: drop_ranges::compute_drop_ranges(fcx, def_id, body),
207     };
208     intravisit::walk_body(&mut visitor, body);
209
210     // Check that we visited the same amount of expressions as the RegionResolutionVisitor
211     let region_expr_count = fcx.tcx.region_scope_tree(def_id).body_expr_count(body_id).unwrap();
212     assert_eq!(region_expr_count, visitor.expr_count);
213
214     // The types are already kept in insertion order.
215     let types = visitor.types;
216
217     // The types in the generator interior contain lifetimes local to the generator itself,
218     // which should not be exposed outside of the generator. Therefore, we replace these
219     // lifetimes with existentially-bound lifetimes, which reflect the exact value of the
220     // lifetimes not being known by users.
221     //
222     // These lifetimes are used in auto trait impl checking (for example,
223     // if a Sync generator contains an &'α T, we need to check whether &'α T: Sync),
224     // so knowledge of the exact relationships between them isn't particularly important.
225
226     debug!("types in generator {:?}, span = {:?}", types, body.value.span);
227
228     // We want to deduplicate if the lifetimes are the same modulo some non-informative counter.
229     // So, we need to actually do two passes: first by type to anonymize (preserving information
230     // required for diagnostics), then a second pass over all captured types to reassign disjoint
231     // region indices.
232     let mut captured_tys = FxHashSet::default();
233     let type_causes: Vec<_> = types
234         .into_iter()
235         .filter_map(|mut cause| {
236             // Replace all regions inside the generator interior with late bound regions.
237             // Note that each region slot in the types gets a new fresh late bound region,
238             // which means that none of the regions inside relate to any other, even if
239             // typeck had previously found constraints that would cause them to be related.
240
241             let mut counter = 0;
242             let mut mk_bound_region = |span| {
243                 let kind = ty::BrAnon(counter, span);
244                 let var = ty::BoundVar::from_u32(counter);
245                 counter += 1;
246                 ty::BoundRegion { var, kind }
247             };
248             let ty = fcx.normalize(cause.span, cause.ty);
249             let ty = fcx.tcx.fold_regions(ty, |region, current_depth| {
250                 let br = match region.kind() {
251                     ty::ReVar(vid) => {
252                         let origin = fcx.region_var_origin(vid);
253                         match origin {
254                             RegionVariableOrigin::EarlyBoundRegion(span, _) => {
255                                 mk_bound_region(Some(span))
256                             }
257                             _ => mk_bound_region(None),
258                         }
259                     }
260                     // FIXME: these should use `BrNamed`
261                     ty::ReEarlyBound(region) => {
262                         mk_bound_region(Some(fcx.tcx.def_span(region.def_id)))
263                     }
264                     ty::ReLateBound(_, ty::BoundRegion { kind, .. })
265                     | ty::ReFree(ty::FreeRegion { bound_region: kind, .. }) => match kind {
266                         ty::BoundRegionKind::BrAnon(_, span) => mk_bound_region(span),
267                         ty::BoundRegionKind::BrNamed(def_id, _) => {
268                             mk_bound_region(Some(fcx.tcx.def_span(def_id)))
269                         }
270                         ty::BoundRegionKind::BrEnv => mk_bound_region(None),
271                     },
272                     _ => mk_bound_region(None),
273                 };
274                 let r = fcx.tcx.mk_region(ty::ReLateBound(current_depth, br));
275                 r
276             });
277             if captured_tys.insert(ty) {
278                 cause.ty = ty;
279                 Some(cause)
280             } else {
281                 None
282             }
283         })
284         .collect();
285
286     let mut bound_vars: SmallVec<[BoundVariableKind; 4]> = smallvec![];
287     let mut counter = 0;
288     // Optimization: If there is only one captured type, then we don't actually
289     // need to fold and reindex (since the first type doesn't change).
290     let type_causes = if captured_tys.len() > 0 {
291         // Optimization: Use `replace_escaping_bound_vars_uncached` instead of
292         // `fold_regions`, since we only have late bound regions, and it skips
293         // types without bound regions.
294         fcx.tcx.replace_escaping_bound_vars_uncached(
295             type_causes,
296             FnMutDelegate {
297                 regions: &mut |br| {
298                     let kind = match br.kind {
299                         ty::BrAnon(_, span) => ty::BrAnon(counter, span),
300                         _ => br.kind,
301                     };
302                     let var = ty::BoundVar::from_usize(bound_vars.len());
303                     bound_vars.push(ty::BoundVariableKind::Region(kind));
304                     counter += 1;
305                     fcx.tcx.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BoundRegion { var, kind }))
306                 },
307                 types: &mut |b| bug!("unexpected bound ty in binder: {b:?}"),
308                 consts: &mut |b, ty| bug!("unexpected bound ct in binder: {b:?} {ty}"),
309             },
310         )
311     } else {
312         type_causes
313     };
314
315     // Extract type components to build the witness type.
316     let type_list = fcx.tcx.mk_type_list(type_causes.iter().map(|cause| cause.ty));
317     let bound_vars = fcx.tcx.mk_bound_variable_kinds(bound_vars.iter());
318     let witness =
319         fcx.tcx.mk_generator_witness(ty::Binder::bind_with_vars(type_list, bound_vars.clone()));
320
321     drop(typeck_results);
322     // Store the generator types and spans into the typeck results for this generator.
323     fcx.inh.typeck_results.borrow_mut().generator_interior_types =
324         ty::Binder::bind_with_vars(type_causes, bound_vars);
325
326     debug!(
327         "types in generator after region replacement {:?}, span = {:?}",
328         witness, body.value.span
329     );
330
331     // Unify the type variable inside the generator with the new witness
332     match fcx.at(&fcx.misc(body.value.span), fcx.param_env).eq(interior, witness) {
333         Ok(ok) => fcx.register_infer_ok_obligations(ok),
334         _ => bug!("failed to relate {interior} and {witness}"),
335     }
336 }
337
338 // This visitor has to have the same visit_expr calls as RegionResolutionVisitor in
339 // librustc_middle/middle/region.rs since `expr_count` is compared against the results
340 // there.
341 impl<'a, 'tcx> Visitor<'tcx> for InteriorVisitor<'a, 'tcx> {
342     fn visit_arm(&mut self, arm: &'tcx Arm<'tcx>) {
343         let Arm { guard, pat, body, .. } = arm;
344         self.visit_pat(pat);
345         if let Some(ref g) = guard {
346             {
347                 // If there is a guard, we need to count all variables bound in the pattern as
348                 // borrowed for the entire guard body, regardless of whether they are accessed.
349                 // We do this by walking the pattern bindings and recording `&T` for any `x: T`
350                 // that is bound.
351
352                 struct ArmPatCollector<'a, 'b, 'tcx> {
353                     interior_visitor: &'a mut InteriorVisitor<'b, 'tcx>,
354                     scope: Scope,
355                 }
356
357                 impl<'a, 'b, 'tcx> Visitor<'tcx> for ArmPatCollector<'a, 'b, 'tcx> {
358                     fn visit_pat(&mut self, pat: &'tcx Pat<'tcx>) {
359                         intravisit::walk_pat(self, pat);
360                         if let PatKind::Binding(_, id, ident, ..) = pat.kind {
361                             let ty =
362                                 self.interior_visitor.fcx.typeck_results.borrow().node_type(id);
363                             let tcx = self.interior_visitor.fcx.tcx;
364                             let ty = tcx.mk_ref(
365                                 // Use `ReErased` as `resolve_interior` is going to replace all the
366                                 // regions anyway.
367                                 tcx.mk_region(ty::ReErased),
368                                 ty::TypeAndMut { ty, mutbl: hir::Mutability::Not },
369                             );
370                             self.interior_visitor.record(
371                                 ty,
372                                 id,
373                                 Some(self.scope),
374                                 None,
375                                 ident.span,
376                             );
377                         }
378                     }
379                 }
380
381                 ArmPatCollector {
382                     interior_visitor: self,
383                     scope: Scope { id: g.body().hir_id.local_id, data: ScopeData::Node },
384                 }
385                 .visit_pat(pat);
386             }
387
388             match g {
389                 Guard::If(ref e) => {
390                     self.visit_expr(e);
391                 }
392                 Guard::IfLet(ref l) => {
393                     self.visit_let_expr(l);
394                 }
395             }
396         }
397         self.visit_expr(body);
398     }
399
400     fn visit_pat(&mut self, pat: &'tcx Pat<'tcx>) {
401         intravisit::walk_pat(self, pat);
402
403         self.expr_count += 1;
404
405         if let PatKind::Binding(..) = pat.kind {
406             let scope = self.region_scope_tree.var_scope(pat.hir_id.local_id).unwrap();
407             let ty = self.fcx.typeck_results.borrow().pat_ty(pat);
408             self.record(ty, pat.hir_id, Some(scope), None, pat.span);
409         }
410     }
411
412     fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
413         match &expr.kind {
414             ExprKind::Call(callee, args) => match &callee.kind {
415                 ExprKind::Path(qpath) => {
416                     let res = self.fcx.typeck_results.borrow().qpath_res(qpath, callee.hir_id);
417                     match res {
418                         // Direct calls never need to keep the callee `ty::FnDef`
419                         // ZST in a temporary, so skip its type, just in case it
420                         // can significantly complicate the generator type.
421                         Res::Def(
422                             DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(_, CtorKind::Fn),
423                             _,
424                         ) => {
425                             // NOTE(eddyb) this assumes a path expression has
426                             // no nested expressions to keep track of.
427                             self.expr_count += 1;
428
429                             // Record the rest of the call expression normally.
430                             for arg in *args {
431                                 self.visit_expr(arg);
432                             }
433                         }
434                         _ => intravisit::walk_expr(self, expr),
435                     }
436                 }
437                 _ => intravisit::walk_expr(self, expr),
438             },
439             _ => intravisit::walk_expr(self, expr),
440         }
441
442         self.expr_count += 1;
443
444         debug!("is_borrowed_temporary: {:?}", self.drop_ranges.is_borrowed_temporary(expr));
445
446         let ty = self.fcx.typeck_results.borrow().expr_ty_adjusted_opt(expr);
447
448         // Typically, the value produced by an expression is consumed by its parent in some way,
449         // so we only have to check if the parent contains a yield (note that the parent may, for
450         // example, store the value into a local variable, but then we already consider local
451         // variables to be live across their scope).
452         //
453         // However, in the case of temporary values, we are going to store the value into a
454         // temporary on the stack that is live for the current temporary scope and then return a
455         // reference to it. That value may be live across the entire temporary scope.
456         //
457         // There's another subtlety: if the type has an observable drop, it must be dropped after
458         // the yield, even if it's not borrowed or referenced after the yield. Ideally this would
459         // *only* happen for types with observable drop, not all types which wrap them, but that
460         // doesn't match the behavior of MIR borrowck and causes ICEs. See the FIXME comment in
461         // tests/ui/generator/drop-tracking-parent-expression.rs.
462         let scope = if self.drop_ranges.is_borrowed_temporary(expr)
463             || ty.map_or(true, |ty| {
464                 // Avoid ICEs in needs_drop.
465                 let ty = self.fcx.resolve_vars_if_possible(ty);
466                 let ty = self.fcx.tcx.erase_regions(ty);
467                 if ty.needs_infer() {
468                     self.fcx
469                         .tcx
470                         .sess
471                         .delay_span_bug(expr.span, &format!("inference variables in {ty}"));
472                     true
473                 } else {
474                     ty.needs_drop(self.fcx.tcx, self.fcx.param_env)
475                 }
476             }) {
477             self.rvalue_scopes.temporary_scope(self.region_scope_tree, expr.hir_id.local_id)
478         } else {
479             let parent_expr = self
480                 .fcx
481                 .tcx
482                 .hir()
483                 .parent_iter(expr.hir_id)
484                 .find(|(_, node)| matches!(node, hir::Node::Expr(_)))
485                 .map(|(id, _)| id);
486             debug!("parent_expr: {:?}", parent_expr);
487             match parent_expr {
488                 Some(parent) => Some(Scope { id: parent.local_id, data: ScopeData::Node }),
489                 None => {
490                     self.rvalue_scopes.temporary_scope(self.region_scope_tree, expr.hir_id.local_id)
491                 }
492             }
493         };
494
495         // If there are adjustments, then record the final type --
496         // this is the actual value that is being produced.
497         if let Some(adjusted_ty) = ty {
498             self.record(adjusted_ty, expr.hir_id, scope, Some(expr), expr.span);
499         }
500
501         // Also record the unadjusted type (which is the only type if
502         // there are no adjustments). The reason for this is that the
503         // unadjusted value is sometimes a "temporary" that would wind
504         // up in a MIR temporary.
505         //
506         // As an example, consider an expression like `vec![].push(x)`.
507         // Here, the `vec![]` would wind up MIR stored into a
508         // temporary variable `t` which we can borrow to invoke
509         // `<Vec<_>>::push(&mut t, x)`.
510         //
511         // Note that an expression can have many adjustments, and we
512         // are just ignoring those intermediate types. This is because
513         // those intermediate values are always linearly "consumed" by
514         // the other adjustments, and hence would never be directly
515         // captured in the MIR.
516         //
517         // (Note that this partly relies on the fact that the `Deref`
518         // traits always return references, which means their content
519         // can be reborrowed without needing to spill to a temporary.
520         // If this were not the case, then we could conceivably have
521         // to create intermediate temporaries.)
522         //
523         // The type table might not have information for this expression
524         // if it is in a malformed scope. (#66387)
525         if let Some(ty) = self.fcx.typeck_results.borrow().expr_ty_opt(expr) {
526             self.record(ty, expr.hir_id, scope, Some(expr), expr.span);
527         } else {
528             self.fcx.tcx.sess.delay_span_bug(expr.span, "no type for node");
529         }
530     }
531 }
532
533 #[derive(Default)]
534 struct SuspendCheckData<'a, 'tcx> {
535     expr: Option<&'tcx Expr<'tcx>>,
536     source_span: Span,
537     yield_span: Span,
538     descr_pre: &'a str,
539     descr_post: &'a str,
540     plural_len: usize,
541 }
542
543 // Returns whether it emitted a diagnostic or not
544 // Note that this fn and the proceeding one are based on the code
545 // for creating must_use diagnostics
546 //
547 // Note that this technique was chosen over things like a `Suspend` marker trait
548 // as it is simpler and has precedent in the compiler
549 fn check_must_not_suspend_ty<'tcx>(
550     fcx: &FnCtxt<'_, 'tcx>,
551     ty: Ty<'tcx>,
552     hir_id: HirId,
553     data: SuspendCheckData<'_, 'tcx>,
554 ) -> bool {
555     if ty.is_unit()
556     // FIXME: should this check `Ty::is_inhabited_from`. This query is not available in this stage
557     // of typeck (before ReVar and RePlaceholder are removed), but may remove noise, like in
558     // `must_use`
559     // || !ty.is_inhabited_from(fcx.tcx, fcx.tcx.parent_module(hir_id).to_def_id(), fcx.param_env)
560     {
561         return false;
562     }
563
564     let plural_suffix = pluralize!(data.plural_len);
565
566     debug!("Checking must_not_suspend for {}", ty);
567
568     match *ty.kind() {
569         ty::Adt(..) if ty.is_box() => {
570             let boxed_ty = ty.boxed_ty();
571             let descr_pre = &format!("{}boxed ", data.descr_pre);
572             check_must_not_suspend_ty(fcx, boxed_ty, hir_id, SuspendCheckData { descr_pre, ..data })
573         }
574         ty::Adt(def, _) => check_must_not_suspend_def(fcx.tcx, def.did(), hir_id, data),
575         // FIXME: support adding the attribute to TAITs
576         ty::Alias(ty::Opaque, ty::AliasTy { def_id: def, .. }) => {
577             let mut has_emitted = false;
578             for &(predicate, _) in fcx.tcx.explicit_item_bounds(def) {
579                 // We only look at the `DefId`, so it is safe to skip the binder here.
580                 if let ty::PredicateKind::Clause(ty::Clause::Trait(ref poly_trait_predicate)) =
581                     predicate.kind().skip_binder()
582                 {
583                     let def_id = poly_trait_predicate.trait_ref.def_id;
584                     let descr_pre = &format!("{}implementer{} of ", data.descr_pre, plural_suffix);
585                     if check_must_not_suspend_def(
586                         fcx.tcx,
587                         def_id,
588                         hir_id,
589                         SuspendCheckData { descr_pre, ..data },
590                     ) {
591                         has_emitted = true;
592                         break;
593                     }
594                 }
595             }
596             has_emitted
597         }
598         ty::Dynamic(binder, _, _) => {
599             let mut has_emitted = false;
600             for predicate in binder.iter() {
601                 if let ty::ExistentialPredicate::Trait(ref trait_ref) = predicate.skip_binder() {
602                     let def_id = trait_ref.def_id;
603                     let descr_post = &format!(" trait object{}{}", plural_suffix, data.descr_post);
604                     if check_must_not_suspend_def(
605                         fcx.tcx,
606                         def_id,
607                         hir_id,
608                         SuspendCheckData { descr_post, ..data },
609                     ) {
610                         has_emitted = true;
611                         break;
612                     }
613                 }
614             }
615             has_emitted
616         }
617         ty::Tuple(fields) => {
618             let mut has_emitted = false;
619             let comps = match data.expr.map(|e| &e.kind) {
620                 Some(hir::ExprKind::Tup(comps)) if comps.len() == fields.len() => Some(comps),
621                 _ => None,
622             };
623             for (i, ty) in fields.iter().enumerate() {
624                 let descr_post = &format!(" in tuple element {i}");
625                 let span = comps.and_then(|c| c.get(i)).map(|e| e.span).unwrap_or(data.source_span);
626                 if check_must_not_suspend_ty(
627                     fcx,
628                     ty,
629                     hir_id,
630                     SuspendCheckData {
631                         descr_post,
632                         expr: comps.and_then(|comps| comps.get(i)),
633                         source_span: span,
634                         ..data
635                     },
636                 ) {
637                     has_emitted = true;
638                 }
639             }
640             has_emitted
641         }
642         ty::Array(ty, len) => {
643             let descr_pre = &format!("{}array{} of ", data.descr_pre, plural_suffix);
644             check_must_not_suspend_ty(
645                 fcx,
646                 ty,
647                 hir_id,
648                 SuspendCheckData {
649                     descr_pre,
650                     plural_len: len.try_eval_usize(fcx.tcx, fcx.param_env).unwrap_or(0) as usize
651                         + 1,
652                     ..data
653                 },
654             )
655         }
656         // If drop tracking is enabled, we want to look through references, since the referrent
657         // may not be considered live across the await point.
658         ty::Ref(_region, ty, _mutability) if fcx.sess().opts.unstable_opts.drop_tracking => {
659             let descr_pre = &format!("{}reference{} to ", data.descr_pre, plural_suffix);
660             check_must_not_suspend_ty(fcx, ty, hir_id, SuspendCheckData { descr_pre, ..data })
661         }
662         _ => false,
663     }
664 }
665
666 fn check_must_not_suspend_def(
667     tcx: TyCtxt<'_>,
668     def_id: DefId,
669     hir_id: HirId,
670     data: SuspendCheckData<'_, '_>,
671 ) -> bool {
672     if let Some(attr) = tcx.get_attr(def_id, sym::must_not_suspend) {
673         tcx.struct_span_lint_hir(
674             rustc_session::lint::builtin::MUST_NOT_SUSPEND,
675             hir_id,
676             data.source_span,
677             DelayDm(|| {
678                 format!(
679                     "{}`{}`{} held across a suspend point, but should not be",
680                     data.descr_pre,
681                     tcx.def_path_str(def_id),
682                     data.descr_post,
683                 )
684             }),
685             |lint| {
686                 // add span pointing to the offending yield/await
687                 lint.span_label(data.yield_span, "the value is held across this suspend point");
688
689                 // Add optional reason note
690                 if let Some(note) = attr.value_str() {
691                     // FIXME(guswynn): consider formatting this better
692                     lint.span_note(data.source_span, note.as_str());
693                 }
694
695                 // Add some quick suggestions on what to do
696                 // FIXME: can `drop` work as a suggestion here as well?
697                 lint.span_help(
698                     data.source_span,
699                     "consider using a block (`{ ... }`) \
700                     to shrink the value's scope, ending before the suspend point",
701                 );
702
703                 lint
704             },
705         );
706
707         true
708     } else {
709         false
710     }
711 }