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