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

Code before:
    // Add all indices of ~Escapable parameters as lifetime dependence sources.
    for (size_t i = 0; i < params->size(); i++) {
      auto paramType = params->get(i)->getTypeInContext();
      if (!isDiagnosedNonEscapable(paramType)) {
        continue;
      }
      resultDeps->inheritIndices.set(i);
    }
    return depBuilder.initializeDependenceInfoArray(eed->getASTContext());
  }

protected:
  template<typename ...ArgTypes>
  InFlightDiagnostic diagnose(
    SourceLoc Loc, Diag<ArgTypes...> ID,
    typename detail::PassArgument<ArgTypes>::type... Args) {
    performedDiagnostics = true;
    return ctx.Diags.diagnose(Loc, ID, std::move(Args)...);
  }

  // Is this lifetime information for a function declaration, as opposed to a
  // function type?
  bool isFunctionDecl() const {
    return afd != nullptr;
  }

  // For initializers, the implicit self parameter is ignored and instead shows
  // up as the result type.
  //
  // Note: Do not use this to reserve the self parameter index.
  // LifetimeDependenceInfo always reserves an extra formal parameter
  // index for hasImplicitSelfDecl(), even for initializers. During function
  // type lowering, it is mapped to the metatype parameter. Without reserving
  // the extra formal self parameter, a dependency targeting the formal result
  // index would incorrectly target the SIL metatype parameter.
  bool hasImplicitSelfParam() const {
    return !isInit && implicitSelfParamInfo.has_value();
  }

  // In SIL, implicit initializers and accessors become explicit.
  bool isImplicitOrSIL() const {
    if (isImplicit) {
      return true;
    }
    // TODO: remove this check once SIL prints @lifetime.
    if (sourceFile) {
      // The AST printer makes implicit initializers explicit, but does not
      // print the @lifetime annotations. Until that is fixed, avoid
      // diagnosing this as an error.
      if (sourceFile->Kind == SourceFileKind::SIL) {
        return true;

Review comment (bug):
IIRC I added a few `afd != nullptr` checks in other parts of the lifetime dependence checker to check if we are checking a Decl (mainly in assert conditions). We could replace those checks with this predicate.

Code after:
    TargetDeps *resultDeps = depBuilder.getInferredTargetDeps(resultIndex);
    ASSERT(resultDeps && "enum declaration has a lifetime attribute");

    // Add all indices of ~Escapable parameters as lifetime dependence sources.
    for (size_t i = 0; i < params->size(); i++) {
      auto paramType = params->get(i)->getTypeInContext();
      if (!isDiagnosedNonEscapable(paramType)) {
        continue;
      }
      resultDeps->inheritIndices.set(i);
    }
    return depBuilder.initializeDependenceInfoArray(eed->getASTContext());
  }

protected:
  template<typename ...ArgTypes>
  InFlightDiagnostic diagnose(
    SourceLoc Loc, Diag<ArgTypes...> ID,
    typename detail::PassArgument<ArgTypes>::type... Args) {
    performedDiagnostics = true;
    return ctx.Diags.diagnose(Loc, ID, std::move(Args)...);
  }

  // Is this lifetime information for an abstact function declaration (function,
  // constructor, or destructor) as opposed to a function type?
  bool isLifetimeForDecl() const {
    return afd != nullptr;
  }

  // For initializers, the implicit self parameter is ignored and instead shows
  // up as the result type.
  //
  // Note: Do not use this to reserve the self parameter index.
  // LifetimeDependenceInfo always reserves an extra formal parameter
  // index for hasImplicitSelfDecl(), even for initializers. During function
  // type lowering, it is mapped to the metatype parameter. Without reserving
  // the extra formal self parameter, a dependency targeting the formal result
  // index would incorrectly target the SIL metatype parameter.
  bool hasImplicitSelfParam() const {
    return !isInit && implicitSelfParamInfo.has_value();
  }

  // In SIL, implicit initializers and accessors become explicit.
  bool isImplicitOrSIL() const {
    if (isImplicit) {
      return true;
    }
    // TODO: remove this check once SIL prints @lifetime.
    if (sourceFile) {
      // The AST printer makes implicit initializers explicit, but does not
      // print the @lifetime annotations. Until that is fixed, avoid