-fn are_inner_types_recursive<'tcx>(
- tcx: TyCtxt<'tcx>,
- seen: &mut Vec<Ty<'tcx>>,
- shadow_seen: &mut Vec<ty::AdtDef<'tcx>>,
- representable_cache: &mut FxHashMap<Ty<'tcx>, Representability>,
- ty: Ty<'tcx>,
- sp: Span,
- field_id: Option<hir::HirId>,
- force_result: &mut bool,
-) -> Representability {
- debug!("are_inner_types_recursive({:?}, {:?}, {:?})", ty, seen, shadow_seen);
- match ty.kind() {
- ty::Tuple(fields) => {
- // Find non representable
- fold_repr(fields.iter().map(|ty| {
- is_type_structurally_recursive(
- tcx,
- seen,
- shadow_seen,
- representable_cache,
- ty,
- sp,
- field_id,
- force_result,
- )
- }))
- }
- // Fixed-length vectors.
- // FIXME(#11924) Behavior undecided for zero-length vectors.
- ty::Array(ty, _) => is_type_structurally_recursive(
- tcx,
- seen,
- shadow_seen,
- representable_cache,
- *ty,
- sp,
- field_id,
- force_result,
- ),
- ty::Adt(def, substs) => {
- // Find non representable fields with their spans
- fold_repr(def.all_fields().map(|field| {
- let ty = field.ty(tcx, substs);
- let (sp, field_id) = match field
- .did
- .as_local()
- .map(|id| tcx.hir().local_def_id_to_hir_id(id))
- .and_then(|id| tcx.hir().find(id))
- {
- Some(hir::Node::Field(field)) => (field.ty.span, Some(field.hir_id)),
- _ => (sp, field_id),
- };
-
- let mut result = None;
-
- // First, we check whether the field type per se is representable.
- // This catches cases as in #74224 and #84611. There is a special
- // case related to mutual recursion, though; consider this example:
- //
- // struct A<T> {
- // z: T,
- // x: B<T>,
- // }
- //
- // struct B<T> {
- // y: A<T>
- // }
- //
- // Here, without the following special case, both A and B are
- // ContainsRecursive, which is a problem because we only report
- // errors for SelfRecursive. We fix this by detecting this special
- // case (shadow_seen.first() is the type we are originally
- // interested in, and if we ever encounter the same AdtDef again,
- // we know that it must be SelfRecursive) and "forcibly" returning
- // SelfRecursive (by setting force_result, which tells the calling
- // invocations of are_inner_types_representable to forward the
- // result without adjusting).
- if shadow_seen.len() > seen.len() && shadow_seen.first() == Some(def) {
- *force_result = true;
- result = Some(Representability::SelfRecursive(vec![(sp, field_id)]));
- }
-
- if result == None {
- result = Some(Representability::Representable);
-
- // Now, we check whether the field types per se are representable, e.g.
- // for struct Foo { x: Option<Foo> }, we first check whether Option<_>
- // by itself is representable (which it is), and the nesting of Foo
- // will be detected later. This is necessary for #74224 and #84611.
-
- // If we have encountered an ADT definition that we have not seen
- // before (no need to check them twice), recurse to see whether that
- // definition is SelfRecursive. If so, we must be ContainsRecursive.
- if shadow_seen.len() > 1
- && !shadow_seen
- .iter()
- .take(shadow_seen.len() - 1)
- .any(|seen_def| seen_def == def)
- {
- let adt_def_id = def.did();
- let raw_adt_ty = tcx.type_of(adt_def_id);
- debug!("are_inner_types_recursive: checking nested type: {:?}", raw_adt_ty);
-
- // Check independently whether the ADT is SelfRecursive. If so,
- // we must be ContainsRecursive (except for the special case
- // mentioned above).
- let mut nested_seen: Vec<Ty<'_>> = vec![];
- result = Some(
- match is_type_structurally_recursive(
- tcx,
- &mut nested_seen,
- shadow_seen,
- representable_cache,
- raw_adt_ty,
- sp,
- field_id,
- force_result,
- ) {
- Representability::SelfRecursive(_) => {
- if *force_result {
- Representability::SelfRecursive(vec![(sp, field_id)])
- } else {
- Representability::ContainsRecursive
- }
- }
- x => x,
- },
- );
- }
-
- // We only enter the following block if the type looks representable
- // so far. This is necessary for cases such as this one (#74224):
- //
- // struct A<T> {
- // x: T,
- // y: A<A<T>>,
- // }
- //
- // struct B {
- // z: A<usize>
- // }
- //
- // When checking B, we recurse into A and check field y of type
- // A<A<usize>>. We haven't seen this exact type before, so we recurse
- // into A<A<usize>>, which contains, A<A<A<usize>>>, and so forth,
- // ad infinitum. We can prevent this from happening by first checking
- // A separately (the code above) and only checking for nested Bs if
- // A actually looks representable (which it wouldn't in this example).
- if result == Some(Representability::Representable) {
- // Now, even if the type is representable (e.g. Option<_>),
- // it might still contribute to a recursive type, e.g.:
- // struct Foo { x: Option<Option<Foo>> }
- // These cases are handled by passing the full `seen`
- // stack to is_type_structurally_recursive (instead of the
- // empty `nested_seen` above):
- result = Some(
- match is_type_structurally_recursive(
- tcx,
- seen,
- shadow_seen,
- representable_cache,
- ty,
- sp,
- field_id,
- force_result,
- ) {
- Representability::SelfRecursive(_) => {
- Representability::SelfRecursive(vec![(sp, field_id)])
- }
- x => x,
- },
- );
- }