Repository: apple/swift
File: lib/Sema/TypeCheckDecl.cpp (C++)

Code before:
      AFD->getParameters()->getParams(argTy);

      maybeAddParameterIsolation(infoBuilder, argTy);
      infoBuilder = infoBuilder.withAsync(AFD->hasAsync());
      infoBuilder = infoBuilder.withSendable(AFD->isSendable());
      // 'throws' only applies to the innermost function.
      infoBuilder = infoBuilder.withThrows(AFD->hasThrows(), thrownTy);
      // Defer bodies must not escape.
      if (auto fd = dyn_cast<FuncDecl>(D)) {
        infoBuilder = infoBuilder.withNoEscape(fd->isDeferBody());
        if (fd->hasSendingResult())
          infoBuilder = infoBuilder.withSendingResult();
      }

      // Lifetime dependencies only apply to the outer function type.
      if (!hasSelf && lifetimeDependenceInfo.has_value()) {
        infoBuilder =
            infoBuilder.withLifetimeDependencies(*lifetimeDependenceInfo);
      }

      // For static methods, with no dependencies involving the self parameter,
      // we can attach the same lifetime dependencies to the inner type as well.
      //
      // TODO: Do this for non-static methods etc. once we have closure context
      // dependencies.
      if (hasSelf && lifetimeDependenceInfo.has_value() && AFD->isStatic()) {
        SmallVector<LifetimeDependenceInfo, 2> innerDependenceInfo;
        innerDependenceInfo.reserve(lifetimeDependenceInfo->size());
        ASSERT(AFD->hasImplicitSelfDecl());
        const unsigned innerResultIndex = AFD->getParameters()->size();
        const unsigned selfIndex = innerResultIndex;
        const unsigned outerResultIndex = innerResultIndex + 1;
        const auto cloneIndicesWithoutSelf =
            [&](IndexSubset *indices) -> std::optional<IndexSubset *> {
          if (!indices)
            return indices;

          if (indices->contains(selfIndex))
            return std::nullopt;

          auto bits = indices->getBitVector();
          ASSERT(!bits.back() && "self index bit not set");
          bits.pop_back();
          return IndexSubset::get(Context, bits);
        };
        const auto cloneDependenceInfoWithoutSelf =
            [&](const LifetimeDependenceInfo &outerDep)
            -> std::optional<LifetimeDependenceInfo> {
          const auto outerTargetIndex = outerDep.getTargetIndex();
          if (outerTargetIndex == selfIndex) {
            return std::nullopt;

Review comment (suggestion):
All of this can go away and instead in LifetimeDependence.cpp replace uses of `AFD::hasImplicitSelfDecl` with `AFD::hasImplicitSelfDecl && !AFD::isStatic`

Code after:
      SmallVector<AnyFunctionType::Param, 4> argTy;
      AFD->getParameters()->getParams(argTy);

      maybeAddParameterIsolation(infoBuilder, argTy);
      infoBuilder = infoBuilder.withAsync(AFD->hasAsync());
      infoBuilder = infoBuilder.withSendable(AFD->isSendable());
      // 'throws' only applies to the innermost function.
      infoBuilder = infoBuilder.withThrows(AFD->hasThrows(), thrownTy);
      // Defer bodies must not escape.
      if (auto fd = dyn_cast<FuncDecl>(D)) {
        infoBuilder = infoBuilder.withNoEscape(fd->isDeferBody());
        if (fd->hasSendingResult())
          infoBuilder = infoBuilder.withSendingResult();
      }

      // Lifetime dependencies only apply to the outer function type for
      // instance methods.
      if (lifetimeDependenceInfo.has_value() && !isInstanceMethod) {
        infoBuilder =
            infoBuilder.withLifetimeDependencies(*lifetimeDependenceInfo);
      }

      auto info = infoBuilder.build();

      if (sig && !hasSelf) {
        funcTy = GenericFunctionType::get(sig, argTy, resultTy, info);
      } else {
        funcTy = FunctionType::get(argTy, resultTy, info);
      }
    }

    // (Self) -> (Args...) -> Result
    if (hasSelf) {
      // Substitute in our own 'self' parameter.
      auto selfParam = computeSelfParam(AFD);
      AnyFunctionType::ExtInfoBuilder selfInfoBuilder;
      maybeAddParameterIsolation(selfInfoBuilder, {selfParam});
      if (lifetimeDependenceInfo.has_value() && isInstanceMethod) {
        selfInfoBuilder =
            selfInfoBuilder.withLifetimeDependencies(*lifetimeDependenceInfo);
      }
      auto *accessor = dyn_cast<AccessorDecl>(AFD);
      if (accessor && accessor->isMutateAccessor()) {
        selfInfoBuilder = selfInfoBuilder.withHasInOutResult();
      }
      // FIXME: Verify ExtInfo state is correct, not working by accident.
      auto selfInfo = selfInfoBuilder.build();
      if (sig) {
        funcTy = GenericFunctionType::get(sig, {selfParam}, funcTy, selfInfo);
      } else {
        funcTy = FunctionType::get({selfParam}, funcTy, selfInfo);