Repository: apple/swift
File: lib/AST/LifetimeDependence.cpp (C++)

Code before:
public:
  static int getResultIndex(AbstractFunctionDecl *afd) {
    return afd->hasImplicitSelfDecl() ? (afd->getParameters()->size() + 1)
                                      : afd->getParameters()->size();
  }

  static int getResultIndex(EnumElementDecl *eed) {
    auto *paramList = eed->getParameterList();
    return paramList ? paramList->size() + 1 : 1;
  }

private:
  void collectLifetimeEntries(DeclAttributes const &attrs) {
    for (auto attr : attrs.getAttributes<LifetimeAttr>()) {
      lifetimeEntries.push_back(attr->getLifetimeEntry());
    }
  }

  void collectParameterInfo(ParameterList const *params) {
    for (auto *param : *params) {
      parameters.push_back(param->toFunctionParam());
      paramLocs.push_back(param->getLoc());
    }
  }

public:
  LifetimeDependenceChecker(AbstractFunctionDecl *afd)
      : functionDC(afd), ctx(afd->getDeclContext()->getASTContext()),
        resultIndex(getResultIndex(afd)),
        depBuilder(/*sourceIndexCap*/ resultIndex),
        isImplicit(afd->isImplicit()), isInit(isa<ConstructorDecl>(afd)),
        hasUnsafeNonEscapableResult(
            afd->getAttrs().hasAttribute<UnsafeNonEscapableResultAttr>()) {
    collectLifetimeEntries(afd->getAttrs());
    collectParameterInfo(afd->getParameters());
    if (auto *decl = afd->getImplicitSelfDecl()) {
      implicitSelfParam = decl->toFunctionParam();
      implicitSelfParamLoc = decl->getLoc();
      // The implicit self parameter is at index parameters.size(). Adding it to
      // the locations array allows us to get the location of any parameter
      // given its index.
      paramLocs.push_back(implicitSelfParamLoc);
      auto *dc = afd->getDeclContext();
      selfTypeInContext = afd->mapTypeIntoEnvironment(dc->getSelfInterfaceType());
    }

    if (auto *accessor = dyn_cast<AccessorDecl>(functionDC);
        accessor && accessor->isCoroutine()) {

      auto yieldTyInContext = accessor->mapTypeIntoEnvironment(
          accessor->getStorage()->getValueInterfaceType());

Review comment (suggestion):
nit: IMO writing the type explicitly instead of `decltype` is more clear here. 

Code after:

  // True if lifetime diganostics have already been performed. Avoids redundant
  // diagnostics, and allows bypassing diagnostics for special cases.
  bool performedDiagnostics = false;

public:
  static int getResultIndex(AbstractFunctionDecl *afd) {
    return afd->hasImplicitSelfDecl() ? (afd->getParameters()->size() + 1)
                                      : afd->getParameters()->size();
  }

  static int getResultIndex(EnumElementDecl *eed) {
    auto *paramList = eed->getParameterList();
    return paramList ? paramList->size() + 1 : 1;
  }

private:
  void collectLifetimeEntries(DeclAttributes const &attrs) {
    for (auto attr : attrs.getAttributes<LifetimeAttr>()) {
      lifetimeEntries.push_back(attr->getLifetimeEntry());
    }
  }

  static auto collectParameterInfo(ParameterList const *params,
                                   DeclContext *DC) {
    decltype(parameterInfos) parameterInfos;
    for (auto *param : *params) {
      parameterInfos.emplace_back(
          param->toFunctionParam(), param->getLoc(),
          DC->mapTypeIntoEnvironment(param->getInterfaceType()));
    }
    return parameterInfos;
  }

public:
  LifetimeDependenceChecker(AbstractFunctionDecl *afd)
      : parameterInfos(collectParameterInfo(afd->getParameters(), afd)),
        afd(afd), ctx(afd->getDeclContext()->getASTContext()),
        sourceFile(afd->getParentSourceFile()),
        resultIndex(getResultIndex(afd)),
        depBuilder(/*sourceIndexCap*/ resultIndex),
        isImplicit(afd->isImplicit()), isInit(isa<ConstructorDecl>(afd)),
        hasUnsafeNonEscapableResult(
            afd->getAttrs().hasAttribute<UnsafeNonEscapableResultAttr>()) {
    collectLifetimeEntries(afd->getAttrs());
    parameterInfos = collectParameterInfo(afd->getParameters(), afd);
    if (auto *selfDecl = afd->getImplicitSelfDecl()) {
      implicitSelfParam =
          ParamInfo{selfDecl->toFunctionParam(), selfDecl->getLoc(),
                    afd->mapTypeIntoEnvironment(
                        afd->getDeclContext()->getSelfInterfaceType())};